package com.dahe.gldriver.ui.account.authperson

import android.os.Bundle
import com.dahe.gldriver.R
import com.dahe.gldriver.bean.Material
import com.dahe.gldriver.bean.UpDrivingInfoBean
import com.dahe.gldriver.bean.UpPart
import com.dahe.gldriver.databinding.ActivityAuthPartyBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.utils.OcrUtils

import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ImageLoader
import com.dahe.mylibrary.utils.PickerUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName AuthPartyActivity
 * @Author john
 * @Date 2024/2/1 11:20
 * @Description 党员认证
 */
class AuthPartyActivity : BaseActivity<ActivityAuthPartyBinding>() {

    var upPart = UpPart()
    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        setTitleBar("党员认证", true)
        binding.run {
            tvData.setOnClickListener {
                PickerUtils.getInstance().showDate(this@AuthPartyActivity) {
                    upPart.partyMembershipTime = it
                    tvData.text = it
                }
            }
            tvAddress.setOnClickListener {
                PickerUtils.getInstance()
                    .showAddress(this@AuthPartyActivity) { province, city, county ->
                        upPart.run {
                            this.province = province.name
                            this.city = city.name
                            this.area = county.name
                            this.provinceCode = county.code
                            this.cityCode = city.code
                            this.areaCode = county.code
                            this.provinceCityArea =
                                """${province.name}-${city.name}-${county.name}"""
                        }
                        tvAddress.text = """${province.name}-${city.name}-${county.name}"""
                    }
            }
            ivUp.setOnClickListener {
                OcrUtils.getInstance().noOcrUpPic(mContext, this@AuthPartyActivity) { picPath, _ ->
                    upPart.materialList.add(0,Material(materialUrl = picPath.picPath))
                    ImageLoader.getInstance()
                        .loadRoundImage(mContext, picPath.locPic, 12, ivUp)
                }
            }
            btnOk.setOnClickListener {
                subMit()
            }
        }
    }

    override fun initDate() {

        DataManager.getInstance().getCpcAuthenticationInfo()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext,object :RxHttpCallBack<UpPart>(){
                override fun onSuccess(t: CommonResponseBean<UpPart>) {
                    super.onSuccess(t)
                    t.data?.let {
                        upPart = it
                        binding.run {
                            tvData.text = it.partyMembershipTime
                            tvAddress.text = it.provinceCityArea
                            etAddress.setText(it.address)
                            ImageLoader.getInstance().loadRoundImage(mContext,it.materialList[0].materialUrl,12,ivUp)
                        }
                    }
                }
            }))

    }

    private fun subMit() {
        if (binding.tvData.text.isNullOrEmpty() ||
            binding.tvAddress.text.isNullOrEmpty() ||
            binding.etAddress.text.isNullOrEmpty()||
            upPart.materialList.size==0
        ) {
            showToast("请完善信息")
            return
        }
        upPart.address = binding.etAddress.text.toString()
        DataManager.getInstance().submitToCpcAuthentication(upPart)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<Any>() {
                override fun onSuccess(t: CommonResponseBean<Any>) {
                    super.onSuccess(t)
                    finish()
                }
            }))
    }
}