package com.dahe.gldriver.ui.mine.activity

import android.os.Bundle
import android.view.View
import com.dahe.gldriver.R
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.databinding.ActivityPersonInfoBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.ui.account.authperson.AuthPersonActivity
import com.dahe.gldriver.utils.OrderUtils
import com.dahe.gldriver.utils.SPUtils
import com.dahe.glex.bean.UserDetail
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 PersonInfoActivity
 * @Author john
 * @Date 2024/2/18 08:47
 * @Description TODO
 */
class PersonInfoActivity : BaseActivity<ActivityPersonInfoBinding>(), View.OnClickListener {
    var doneInfoCount = 0
    var userDetail: UserDetail? = null
    override fun initView(savedInstanceState: Bundle?) {
        setStatusHeightParams(binding.rlTop)
        setTitleBar("实名认证", true)

        binding.run {
            circleBarView2.run {
                text = "完成度"
                unitText = "%"
                progress = 0
            }
            btnId.setOnClickListener(this@PersonInfoActivity)
            llAgainId.setOnClickListener(this@PersonInfoActivity)
            llSeeId.setOnClickListener(this@PersonInfoActivity)

            btnDriver.setOnClickListener(this@PersonInfoActivity)
            llAgainDriver.setOnClickListener(this@PersonInfoActivity)
            llSeeDriver.setOnClickListener(this@PersonInfoActivity)


            btnQua.setOnClickListener(this@PersonInfoActivity)
            llAgainQua.setOnClickListener(this@PersonInfoActivity)
            llSeeQua.setOnClickListener(this@PersonInfoActivity)


            btnFace.setOnClickListener(this@PersonInfoActivity)
        }
    }

    override fun initDate() {
        DataManager.getInstance().getUserDetail()
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<UserDetail>() {
                override fun onSuccess(t: CommonResponseBean<UserDetail>) {
                    super.onSuccess(t)
                    initViewByData(t.data)
                }
            }))
    }

    override fun onClick(view: View?) {
        super.onClick(view)
        binding.run {
            when (view?.id) {
                btnId.id, llAgainId.id -> ActivityUtils.startActivity(
                    mContext,
                    AuthPersonActivity::class.java,
                    Bundle().apply {
                        putBoolean(AppConfig.IS_ONLY_SHOW, false)
                        putString(AppConfig.USER_DETAIL, Gson().toJson(userDetail))
                    })

                llSeeId.id -> ActivityUtils.startActivity(
                    mContext,
                    AuthPersonActivity::class.java,
                    Bundle().apply {
                        putBoolean(AppConfig.IS_ONLY_SHOW, true)
                        putString(AppConfig.USER_DETAIL, Gson().toJson(userDetail))
                    })

                btnDriver.id, llAgainDriver.id -> {}
                llSeeDriver.id -> {}

                btnQua.id, llAgainQua.id -> {}
                llSeeQua.id -> {}

                btnFace.id -> {}
            }
        }

    }

    private fun initViewByData(data: UserDetail) {
        userDetail = data
        binding.run {
            val userInfo = SPUtils.instance.getUserInfo(mContext)
            var missInfo = ""

            if (data.idcardNo.isNullOrEmpty()) {
                tvInfo.text = "证件缺失将无法进行接单"
                missInfo.plus("身份证、")
            } else {
                doneInfoCount++
                tvInfo.text = """${data.idcardName} ${data.idcardNo}"""
            }
            if (data.licenseNo.isNullOrEmpty()) {
                tvDriverInfo.text = "证件缺失将无法进行接单"
                missInfo.plus("驾驶证、")
            } else {
                doneInfoCount++
                tvDriverInfo.text = """${data.licenseName} ${data.licenseNo}"""
            }
            if (data.qualificationCertificateNo.isNullOrEmpty()) {
                tvQualiInfo.text = "证件缺失将无法进行接单"
                missInfo.plus("从业资格证、")
            } else {
                doneInfoCount++
                tvQualiInfo.text = """${data.qualificationCertificateNo}"""
            }

            if (userInfo?.isRealName == "0") {
                btnFace.text = "未实名认证将无法进行接单"
                missInfo.plus("实名认证缺失")
            } else {
                doneInfoCount++
                tvFaceInfo.text = "实名认证成功"
            }

            btnNext.text = if (doneInfoCount == 4) "已完成" else "继续认证"
            tvContent.text = if (missInfo.isNullOrEmpty()) "认证成功,请接单把!!!" else missInfo


            llId.visibility = if (data.idcardNo.isNullOrEmpty()) View.GONE else View.VISIBLE
            btnId.visibility = if (data.idcardNo.isNullOrEmpty()) View.VISIBLE else View.GONE

            llDriver.visibility = if (data.licenseNo.isNullOrEmpty()) View.GONE else View.VISIBLE
            btnDriver.visibility = if (data.licenseNo.isNullOrEmpty()) View.VISIBLE else View.GONE
            llQua.visibility =
                if (data.qualificationCertificateNo.isNullOrEmpty()) View.GONE else View.VISIBLE
            btnQua.visibility =
                if (data.qualificationCertificateNo.isNullOrEmpty()) View.VISIBLE else View.GONE
            btnFace.visibility = if (userInfo?.isRealName == "0") View.VISIBLE else View.GONE

            circleBarView2.progress = doneInfoCount * 100 / 4

        }
    }
}