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.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 { ToastUtils.showLong("点击修改密码") }
    }

    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 {
                            MMKV.defaultMMKV().clearAll()
                            AccountService.clear()
                            val intent = Intent(this@SettingActivity, LoginActivity::class.java)
                            startActivity(intent)
                            finish()
                        }
                    }
                })
                .show()
        }
    }
}