package com.develop.setting.init.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.develop.food.base.common.POLICY_FROM_MAIN_SETTING
import com.develop.food.base.event.FinishAtyEvent
import com.develop.food.base.router.Screens
import com.develop.food.base.router.navigateTo
import com.develop.food.base.ui.BaseActivity
import com.develop.food.base.utils.ThreadUtils
import com.develop.food.base.utils.TimeDownUtil
import com.develop.setting.R
import com.develop.setting.databinding.ActivityPrivacePolicyBinding
import org.greenrobot.eventbus.Subscribe
import kotlin.math.roundToInt

@Route(path = Screens.Setting.POLICY)
class PrivacyPolicyActivity : BaseActivity<ActivityPrivacePolicyBinding>() {
    private var timeDownUtil: TimeDownUtil? = null
    private var isAgree = false
    private var isFinish = false
    override fun createViewBinding(inflater: LayoutInflater): ActivityPrivacePolicyBinding {
        return ActivityPrivacePolicyBinding.inflate(inflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (intent.extras?.getBoolean(POLICY_FROM_MAIN_SETTING) == true) {
            binding.ivSelected.visibility = View.GONE
            binding.tvAgree.visibility = View.GONE
            binding.agreeLayout.visibility = View.GONE
        }
        countForPolicyTime()
        binding.topBar.listenBackClick {
            finish()
        }
        binding.topBar.setTopBarTitle(R.string.privacy_policy)
        binding.agreeLayout.setOnClickListener {
            isAgree = !isAgree
            if (isAgree) {
                binding.ivSelected.setBackgroundResource(R.drawable.ic_selected)
            } else {
                binding.ivSelected.setBackgroundResource(R.drawable.ic_unselected)
            }
        }
        binding.tvAgree.setOnClickListener {
            if (isAgree && isFinish) {
                navigateTo(Screens.Setting.WIFI)
            }
        }

    }

    private fun countForPolicyTime() {
        timeDownUtil = object : TimeDownUtil(5 * 1000, 1000) {
            override fun onTick(millisUntilFinished: Long) {

                val time = (millisUntilFinished.toDouble() / 1000).roundToInt() + 1
                ThreadUtils.runOnMainThread {
                    binding.tvAgree.text = "$time ${getString(R.string.second)}"
                }

            }

            override fun onFinish() {
                isFinish = true
                ThreadUtils.runOnMainThread {
                    binding.tvAgree.setBackgroundResource(R.drawable.shape_60pxf4a12e_corners)
                    binding.tvAgree.text = getString(R.string.i_agree)
                }
            }
        }
        timeDownUtil?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        timeDownUtil?.cancel()
    }

    @Subscribe
    fun onFinishAtyEvent(event: FinishAtyEvent) {
        finish()
    }

}