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

import android.graphics.Color
import android.os.Bundle
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.CarsAdapter
import com.dahe.gldriver.bean.CarBean
import com.dahe.gldriver.databinding.ActivityCarsManBinding
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.AuthDrivingActivity
import com.dahe.gldriver.utils.CommonPopUtils
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.recycleviewswipe.RecycleViewDivider
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.ConvertUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName CarsManActivity
 * @Author john
 * @Date 2024/2/1 08:59
 * @Description 车辆管理
 */
class CarsManActivity : BaseActivity<ActivityCarsManBinding>() {

    lateinit var adapter: CarsAdapter
    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        setTitleBar("车辆管理", true)


        binding.run {
            adapter = recycler.run {
                layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
                addItemDecoration(
                    RecycleViewDivider(
                        LinearLayout.VERTICAL,
                        ConvertUtils.dp2px(16.0f),
                        Color.TRANSPARENT
                    )
                )
                adapter = CarsAdapter()
                adapter as CarsAdapter
            }.apply {
                isStateViewEnable = true
                addOnItemChildClickListener(R.id.tvUnbind) { adapter, view, position ->
                    CommonPopUtils.getInstance()
                        .showCommCenterPop(
                            mContext,
                            content = """您确定解绑:${items[position].vehicleNum}么?"""
                        ) {
                            DataManager.getInstance().unBindCar(items[position])
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(
                                    BaseObserver(mContext,
                                        object : RxHttpCallBack<Any>() {
                                            override fun onSuccess(t: CommonResponseBean<Any>) {
                                                super.onSuccess(t)
                                                unBindCar(items[position].carId)
                                            }
                                        })
                                )
                        }
                }
            }

            btnOk.setOnClickListener {
                ActivityUtils.startActivity(mContext, AuthDrivingActivity::class.java)
            }
        }
    }

    override fun initDate() {
        DataManager.getInstance().carList()
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                BaseObserver(mContext, object : RxHttpCallBack<MutableList<CarBean>>() {
                    override fun onSuccess(t: CommonResponseBean<MutableList<CarBean>>) {
                        super.onSuccess(t)
                        if (t.data.size == 0) {
                            adapter.submitList(null)
                            adapter.setStateViewLayout(mContext, R.layout.empty_view)
                        } else {
                            adapter?.submitList(t.data)
                        }


                    }
                })
            )
    }

    private fun unBindCar(carId: String) {
        DataManager.getInstance().unBindCar(CarBean().apply {
            this.carId = carId
        })
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<Any>() {
                override fun onSuccess(t: CommonResponseBean<Any>) {
                    super.onSuccess(t)
                    initDate()
                }
            }))
    }
}