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.tvLoginType.setOnClickListener(this)
        binding.ok.setOnClickListener(this)
    }

    override fun initDate() {
        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 -> {}
            binding.ok.id -> {
//                setResult(RESULT_OK, Intent().apply { putExtra(AppConfig.CODE, "2024") })
//                finish()
                DataManager.getInstance().login(phone, "2024")
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(BaseObserver(mContext, object : RxHttpCallBack<UserBean>() {
                        override fun onSuccess(t: CommonResponseBean<UserBean>) {
                            super.onSuccess(t)
                            SPUtils.instance.setUserInfo(mContext, Gson().toJson(t.data))


                            ActivityUtils.startActivity(
                                mContext,
                                SelectRoleActivity::class.java
                            )
                            finish()
                            ActivityUtils.finishActivity(LoginActivity::class.java)
                        }
                    }))

            }
        }
    }

    private fun changeLoginType() {
    }

    private fun login() {

    }
}