package com.dahe.gldriver.ui.account

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.databinding.ActivityCodeBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.ui.HomeActivity
import com.dahe.gldriver.utils.SPUtils
import com.dahe.glex.bean.UserBean
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ActivityUtils
import com.google.gson.Gson
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName LoginActivity
 * @Author 用户
 * @Date 2024/1/17 08:51
 * @Description TODO
 */
class CodeLoginActivity : BaseActivity<ActivityCodeBinding>(), View.OnClickListener {
    lateinit var phone: String
    override fun initView(savedInstanceState: Bundle?) {
        phone = intent.extras!!.getString(AppConfig.PHONE, "")
        binding.run {
            tvLoginType.setOnClickListener(this@CodeLoginActivity)
            ok.setOnClickListener(this@CodeLoginActivity)
            ivBack.setOnClickListener(this@CodeLoginActivity)
            tvPhone.text = """验证码已发送至${phone}"""
        }
    }

    override fun initDate() {
        // TODO: 获取验证码，暂时注释
//        DataManager.getInstance().getCode(phone)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<UserBean>() {
//                override fun onSuccess(t: CommonResponseBean<UserBean>) {
//                    super.onSuccess(t)
//                }
//            }))
    }

    override fun onClick(view: View?) {
        super.onClick(view)
        when (view?.id) {
            binding.tvLoginType.id -> {
                initDate()
            }

            binding.ivBack.id -> {
                finish()
            }

            binding.ok.id -> {
//                setResult(RESULT_OK, Intent().apply { putExtra(AppConfig.CODE, "2024") })
//                finish()
                DataManager.getInstance().login(phone, "2024")
                    .flatMap {
                        //两个接口token字段名称不一致，先进行保存
                        SPUtils.instance.setUserToken(mContext, it.data.access_token)
                        DataManager.getInstance().getUserInfo()
                    }
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(BaseObserver(mContext, object : RxHttpCallBack<UserBean>(this) {
                        override fun onSuccess(t: CommonResponseBean<UserBean>) {
                            super.onSuccess(t)
                            SPUtils.instance.setUserInfo(mContext, Gson().toJson(t.data))
                            ActivityUtils.startActivity(
                                mContext,
                                HomeActivity::class.java
                            )
                            ActivityUtils.finishToActivity(LoginActivity::class.java, true)
                        }
                    }))

            }
        }
    }

    private fun changeLoginType() {
    }

    private fun login() {

    }
}