package com.adealink.weparty.account.login.component

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.account.constant.AccountLoginAuthCancelError
import com.adealink.weparty.account.constant.showLoginFailedTip
import com.adealink.weparty.account.databinding.LayoutLoginWayBinding
import com.adealink.weparty.account.login.BaseLoginActivity
import com.adealink.weparty.account.login.data.ThirdType
import com.adealink.weparty.account.login.viewmodel.LoginViewModel
import com.adealink.weparty.account.viewModel.AccountViewModelFactory
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.module.account.Account


class LoginWayComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutLoginWayBinding,
    private val loginWays: Set<ThirdType>,
) : ViewComponent(lifecycleOwner) {

    private val loginViewModel by activityViewModels<LoginViewModel> { AccountViewModelFactory() }
//    private val smsCountrySelectViewModel by viewModels<SmsCountrySelectViewModel>()


    override fun onCreate() {
        super.onCreate()
        initViews()
        observeViewModel()

//        smsCountrySelectViewModel.getAllCountryList()
    }

    private fun initViews() {
        binding.phoneLogin.onClick {
            Router.build(requireActivity(), Account.Phone.INPUT)
                .start()
        }

        binding.ivLoginEmail.onClick(wait = 1000) {
            Router.build(requireActivity(), Account.Email.PATH_LOGIN)
                .start()
        }

    }

    private fun login(authType: ThirdType) {
        activity?.let {
            (it as? BaseActivity)?.showLoading()
            loginViewModel.startAuthLogin(authType, it)
        }
    }

    private fun observeViewModel() {
//        smsCountrySelectViewModel.countryListLD.observe(this) {
//            if (it.isEmpty()) {
//                binding.phoneLogin.gone()
//                return@observe
//            }
//
//            val simCountryCode = getSimCountryCode() ?: ""
//            if (simCountryCode.isEmpty()) {
//                binding.phoneLogin.show()
//                return@observe
//            }
//
//            if (smsCountrySelectViewModel.containsCountry(simCountryCode) || simCountryCode == "CN") {
//                binding.phoneLogin.show()
//                return@observe
//            }
//
//            binding.phoneLogin.gone()
//        }

        loginViewModel.loginLD.observe(viewLifecycleOwner) { rlt ->
            (activity as? BaseActivity)?.dismissLoading()
            val authType = rlt.authType
            when (val result = rlt.result) {
                is Rlt.Success -> {
                    (activity as? BaseLoginActivity)?.handleLoginSuccess(
                        result.data,
                        authType,
                    )
                }

                is Rlt.Failed -> {
                    when (result.error) {
                        //取消授权
                        is AccountLoginAuthCancelError -> {
                            return@observe
                        }
                    }
                    showLoginFailedTip(result)
                }
            }
        }
    }

}