package com.abbidot.tracker.deprecated.ui.fragment.forgot

import android.content.Context
import android.graphics.Paint
import android.text.TextUtils
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.FragmentForgetPasswordCnFinishBinding
import com.abbidot.tracker.deprecated.ui.activity.LoginRegisterCNActivity
import com.abbidot.tracker.deprecated.ui.fragment.vm.RegisterCNViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


/**
 * 国内忘记密码-设置新密码
 */
class ForgetPasswordCNFinishFragment : BaseFragment<FragmentForgetPasswordCnFinishBinding>(
    FragmentForgetPasswordCnFinishBinding::inflate
) {

    private val mRegisterCNViewModel: RegisterCNViewModel by viewModels()

    companion object {
        @JvmStatic
        fun newInstance(context: Context) = ForgetPasswordCNFinishFragment().apply {
            mContext = context
        }
    }

    override fun initData() {
        super.initData()

        val activity = activity as LoginRegisterCNActivity
        activity.apply {
            if (mType != ConstantInt.Close) {
                mViewBinding.setNewPasswordReturnLoginCn.visibility = View.GONE
            }
            setTopTitle(getString(R.string.txt_set_new_password))
            setTopContent("")
        }

        mViewBinding.apply {
            //添加下划线
            setNewPasswordReturnLoginCn.paint.flags = Paint.UNDERLINE_TEXT_FLAG
            setOnClickListenerViews(btnFinishSetNewPassword, setNewPasswordReturnLoginCn)
        }
    }

    override fun liveDataObserve() {
        mRegisterCNViewModel.mSetNewPasswordResultLiveData.observe(viewLifecycleOwner) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    lifecycleScope.launch {
                        val activity = activity as LoginRegisterCNActivity
                        if (activity.mType != ConstantInt.Close) {
                            withContext(Dispatchers.IO) {
                                MMKVUtil.clearAll()
                            }
                            XEventBus.post(EventName.ChangePassword)
                        }
                        showToast(R.string.txt_change_successful)
                        activity.switchFragment(LoginRegisterCNActivity.Fragment_Account_Login)
                    }
                }
            })
        }
    }

    private fun setNewPassword() {
        val password = mViewBinding.setNewCnPasswordEd.text.toString()
        if (TextUtils.isEmpty(password)) {
            showToast(R.string.txt_not_null)
            return
        }
        val activity = activity as LoginRegisterCNActivity
        mRegisterCNViewModel.resetNewPassword(this, activity.mPhone, password)
    }

    override fun onClick(v: View?) {
        when (v!!) {
            mViewBinding.btnFinishSetNewPassword -> setNewPassword()
            mViewBinding.setNewPasswordReturnLoginCn -> {
                val activity = activity as LoginRegisterCNActivity
                activity.switchFragment(LoginRegisterCNActivity.Fragment_Account_Login)
            }
        }
    }
}