package com.miya.fastcashier.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.FragmentManager
import com.fastcashier.lib_common.base.BaseActivity
import com.fastcashier.lib_common.ui.dialog.CommonDialog
import com.fastcashier.lib_common.util.click
import com.fastcashier.lib_common.util.clickWithTrigger
import com.miya.fastcashier.MiyaApplication
import com.miya.fastcashier.R
import com.miya.fastcashier.databinding.ActivitySettingBinding
import com.miya.fastcashier.ui.dialog.AuthorizePasswordInputDialog

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) {
            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 {
                            MiyaApplication.getApplication().resetConfig()
                            LoginActivity.start(
                                this@SettingActivity,
                                Intent.FLAG_ACTIVITY_CLEAR_TASK
                            )
                            finish()
                        }
                    }
                })
                .show()
        }
    }
}