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

import android.content.Context
import android.text.TextUtils
import android.view.View
import androidx.fragment.app.viewModels
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.FragmentRegisterCnFinishBinding
import com.abbidot.tracker.deprecated.ui.activity.LoginRegisterCNActivity
import com.abbidot.tracker.deprecated.ui.fragment.vm.RegisterCNViewModel


/**
 *国内注册完成
 */
class RegisterCNFinishFragment :
    BaseFragment<FragmentRegisterCnFinishBinding>(FragmentRegisterCnFinishBinding::inflate) {

    private val mRegisterCNViewModel: RegisterCNViewModel by viewModels()
//    private val mLoginCNViewModel: LoginCNViewModel by viewModels()

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

    override fun initData() {
        super.initData()
        setOnClickListenerViews(mViewBinding.btnFinishRegisterCn)
    }

    override fun liveDataObserve() {
        mRegisterCNViewModel.mPhoneRegisterResultLiveData.observe(viewLifecycleOwner) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
//                    val data = it.getOrNull()
//                    data?.apply {
//                        val activity = activity as LoginRegisterCNActivity
//                        mLoginCNViewModel.loginSuccess(activity, this)
//                    }
                    showToast(R.string.txt_registered_successfully)
                    val activity = activity as LoginRegisterCNActivity
                    activity.switchFragment(LoginRegisterCNActivity.Fragment_Account_Login)
                }
            })
        }
    }

    /**
     * 注册设置密码
     */
    private fun registerSetPassword() {
        val password = mViewBinding.setLoginCnPasswordEd.text.toString()
        if (TextUtils.isEmpty(password)) {
            showToast(R.string.txt_not_null)
            return
        }
        val activity = activity as LoginRegisterCNActivity
        mRegisterCNViewModel.register(this, activity.mPhone, password)
    }

    override fun onClick(v: View?) {
        when (v!!) {
            mViewBinding.btnFinishRegisterCn -> registerSetPassword()
        }
    }
}