package com.rdiot.yx485.ui.login

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.drake.net.utils.TipUtils
import com.drake.net.utils.scopeDialog
import com.rdiot.yx485.BuildConfig
import com.rdiot.yx485.R
import com.rdiot.yx485.base.BaseFragment
import com.rdiot.yx485.base.LocalData
import com.rdiot.yx485.base.LoginViewModelFactory
import com.rdiot.yx485.databinding.FraLoginPwdBinding
import com.rdiot.yx485.net.loginViaPwd
import com.rdiot.yx485.ui.login.model.LoginRecord
import com.rdiot.yx485.ui.login.model.LoginType
import com.rdiot.yx485.ui.login.model.LoginViewModel
import com.rdiot.yx485.ui.main.MainActivity
import com.rdiot.yx485.ui.web.WebActivity
import com.rdiot.yx485.util.ClickLimitListener
import com.rdiot.yx485.util.clearGoTo
import com.rdiot.yx485.util.finishGoTo
import com.rdiot.yx485.util.setClickLimitListener
import kotlinx.coroutines.delay

/**
 * 密码登录
 * @author mR2hao
 * @date 2022/11/15
 */
class LoginPwdFragment : BaseFragment<FraLoginPwdBinding>(), ClickLimitListener {
    override fun getLayoutId() = R.layout.fra_login_pwd
    private val loginViewModel: LoginViewModel by activityViewModels { LoginViewModelFactory() }

    override fun initView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) {
        binding.vm = loginViewModel
        loginViewModel.phone.set(LocalData.lastLoginRecord?.phone)
        loginViewModel.pwd.set(LocalData.lastLoginRecord?.pwd)


        binding.nb.setRightClickListener {
//            findNavController().navigate(R.id.action_loginSmsFragment_to_loginPwdFragment)
            loginViewModel.isRead.postValue(false)
            findNavController().popBackStack()
        }

        binding.llResetPwd.setClickLimitListener(this)
        binding.btnLogin.setClickLimitListener(this)
        binding.tvSa.setClickLimitListener(this)
        binding.tvPp.setClickLimitListener(this)
        binding.llReadPolicy.setClickLimitListener(this)
    }


    override fun onClickLimit(view: View) {
        when (view) {
            binding.llResetPwd -> {
                loginViewModel.phone.get()?.let { phone ->
                    loginViewModel.clear(phone.length != 11)
                }
                findNavController().navigate(R.id.action_loginPwdFragment_to_resetPwdFragment)
            }
            binding.btnLogin -> {
                loginViewModel.isRead.value?.let { isRead ->
                    if (isRead) {
                        scopeDialog(cancelable = BuildConfig.DEBUG) {
                            //防闪屏
                            delay(500)
                            loginViaPwd(
                                loginViewModel.phone.get(),
                                loginViewModel.pwd.get()
                            ).await().saveLocal()
                            (act as LoginActivity).isTokenInvalid = false
                            //保存登录记录
                            LoginRecord(
                                loginViewModel.phone.get(),
                                loginViewModel.pwd.get(),
                                LoginType.PWD
                            ).save()
                            act.finishGoTo(MainActivity::class.java)

                        }
                    } else {
                        TipUtils.toast(getString(R.string.please_read))
                    }

                }
            }

            binding.llReadPolicy -> {
                loginViewModel.isRead.postValue(!loginViewModel.isRead.value!!)
            }

            binding.tvSa -> {
                act.clearGoTo(
                    WebActivity.getNewWebIntent(
                        act,
                        "https://app.yongxulvjian.com/#/ServiceAgreement",
                        "用户协议"
                    )
                )
            }

            binding.tvPp -> {
                act.clearGoTo(
                    WebActivity.getNewWebIntent(
                        act,
                        "https://app.yongxulvjian.com/#/PrivacyPolicy",
                        "隐私政策"
                    )
                )
            }

        }
    }
}