package com.develop.setting.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.develop.base.ext.navigateTo
import com.develop.base.mvvm.BaseBindingActivity
import com.develop.base.scop.runOnMainThread
import com.develop.common.event.FinishAtyEvent
import com.develop.common.router.Screens
import com.develop.common.tag.POLICY_FROM_MAIN_SETTING
import com.develop.common.utils.TimeDownUtil
import com.develop.setting.databinding.ActivityPrivacePolicyBinding
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import kotlin.math.roundToInt

@Route(path = Screens.Setting.POLICY)
class PrivacyPolicyActivity : BaseBindingActivity<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.ivBack.setOnClickListener {
            finish()
        }
        binding.agreeLayout.setOnClickListener {
            isAgree = !isAgree
            if (isAgree) {
                binding.ivSelected.setBackgroundResource(com.develop.common.R.drawable.ic_selected)
            } else {
                binding.ivSelected.setBackgroundResource(com.develop.common.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
                runOnMainThread {
                    binding.tvAgree.text = "$time ${getString(com.develop.common.R.string.second)}"
                }

            }

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

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

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

}