package com.miya.fastcashier.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import androidx.fragment.app.FragmentManager
import com.miya.fastcashier.BaseApplication
import com.miya.fastcashier.R
import com.miya.fastcashier.databinding.ActivitySettingBinding
import com.miya.fastcashier.log.FunctionCenterActivity
import com.miya.fastcashier.ui.dialog.AuthorizePasswordInputDialog
import com.miya.fastcashier.ui.dialog.CommonDialog
import com.miya.fastcashier.utils.click
import com.miya.fastcashier.utils.clickWithTrigger
import es.dmoral.toasty.Toasty

class SettingActivity : BaseActivity() {

    private lateinit var viewBinding: ActivitySettingBinding
    private var alertDialog: CommonDialog? = null

    private var countClick = 0
    private var lastClickTime: Long = 0

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val intent = Intent(context, SettingActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ActivitySettingBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)

        init()
    }

    private fun init() {
        viewBinding.llBack.clickWithTrigger { finish() }
        viewBinding.vLoginOut.clickWithTrigger { loginOut() }
        viewBinding.vUpdatePassword.clickWithTrigger { ResetAuthorizePasswordActivity.start(this) }
        viewBinding.vFunction.click { checkBeforeEnterFunction() }
    }

    private fun checkBeforeEnterFunction() {
        var currentTime  = System.currentTimeMillis()
        if (currentTime - lastClickTime > 1000 && lastClickTime != 0L){
            countClick = 0
        }
        countClick++
        lastClickTime = currentTime
//        HideClickUtil.getInstance().countClick()
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe { integer ->
//                if (integer === HideClickUtil.CLICK_TWO) {
//                    //进入隐藏操作，弹出身份验证对话框
//                    if (verifyDialog != null) {
//                        verifyDialog.show()
//                    }
//                } else if (integer > HideClickUtil.CLICK_ONE && integer < HideClickUtil.CLICK_TWO) {
//                    //吐司提示
//
//                }
//            }
        if (countClick < 5) {
            if (countClick >= 2 && countClick < 5) {
                Toasty.info(
                    applicationContext,
                    getString(R.string.toast_click_again, 5 - countClick)
                ).show()
            }
            return
        }

        showAuthorizedDialog()
    }

    private fun showAuthorizedDialog() {
        val FRAGMENT_TAG = "Authorize_Password"
        val fm: FragmentManager = getSupportFragmentManager()
        val oldFrag = fm.findFragmentByTag(FRAGMENT_TAG)
        if (oldFrag != null) {
            fm.beginTransaction().remove(oldFrag).commitAllowingStateLoss()
        }
        AuthorizePasswordInputDialog.newInstance().show(fm, FRAGMENT_TAG,object :AuthorizePasswordInputDialog.OnAuthorizedFinishListener{
            override fun onAuthorizedFinished(isSucceed: Boolean) {
                if (isSucceed){
                    FunctionCenterActivity.start(this@SettingActivity)
                }
            }

        })
    }

    private fun loginOut() {
        if (alertDialog == null || !alertDialog!!.isShowing) {
            alertDialog = CommonDialog(this)
            alertDialog!!.setCustomMessage(getString(R.string.app_text_login_out))
                .setNegativeStr(getString(R.string.cancel))
                .setPositiveStr(getString(R.string.text_login_out_sure))
                .setMessageGravity(Gravity.CENTER_HORIZONTAL)
                .setOnClickListener(object : CommonDialog.OnDialogClickListener {
                    override fun onNegativeClick() {
                        alertDialog!!.dismiss()
                        alertDialog = null
                    }

                    override fun onPositiveClick() {
                        alertDialog!!.dismiss()
                        alertDialog = null
                        run {
                            BaseApplication.getApplication().resetConfig()
                            LoginActivity.start(
                                this@SettingActivity,
                                Intent.FLAG_ACTIVITY_CLEAR_TASK
                            )
                            finish()
                        }
                    }
                })
                .show()
        }
    }
}