package com.miya.fastcashier.ui

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import com.blankj.utilcode.util.ToastUtils
import com.miya.fastcashier.BaseApplication
import com.miya.fastcashier.R
import com.miya.fastcashier.databinding.ActivitySettingBinding
import com.miya.fastcashier.service.AccountService
import com.miya.fastcashier.ui.dialog.CommonDialog
import com.miya.fastcashier.utils.clickWithTrigger
import com.tencent.mmkv.MMKV

class SettingActivity : BaseActivity() {

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

    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) }
    }

    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()
        }
    }
}