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

import android.content.Intent
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.DriverTeamsAdapter
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.CaptainBean
import com.dahe.gldriver.bean.DriverBean
import com.dahe.gldriver.databinding.ActivityDriverCarTeamBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.callback.RefreshCallBack
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 CarTeamActivity
 * @Author john
 * @Date 2024/2/6 16:38
 * @Description 司机-车队管理-车队列表
 */
class DriverTeamsActivity : BaseActivity<ActivityDriverCarTeamBinding>(), RefreshCallBack {
    lateinit var adapter: DriverTeamsAdapter

    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        setTitleBar("车队管理", true)

        binding.run {
            setRefresh(refresh, this@DriverTeamsActivity)
            adapter = recycler.run {
                layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
                addItemDecoration(
                    RecycleViewDivider(
                        LinearLayout.VERTICAL,
                        ConvertUtils.dp2px(16.0f),
                        Color.TRANSPARENT
                    )
                )
                adapter = DriverTeamsAdapter()
                adapter as DriverTeamsAdapter
            }.apply {
                isStateViewEnable = true
                setStateViewLayout(mContext, R.layout.empty_view)
                setOnItemClickListener { adapter, view, position ->
                    ActivityUtils.startActivityForResult(
                        this@DriverTeamsActivity,
                        DriverTeamDetailActivity::class.java,
                        Bundle().apply { putString(AppConfig.ID, items[position].captainId) },
                        1
                    )
                }
            }
        }


    }

    override fun initDate() {
        DataManager.getInstance().selectCaptainListInDriver()
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<MutableList<CaptainBean>>() {
                override fun onSuccess(t: CommonResponseBean<MutableList<CaptainBean>>) {
                    super.onSuccess(t)
                    if (mRefreshPage === 1) {
                        if (t.data.size == 0) {
                            adapter.submitList(null)
                            adapter.setStateViewLayout(mContext, R.layout.empty_view)
                        } else {
                            adapter?.submitList(t.data)
                        }
                    } else {
                        adapter?.addAll(t.data)
                    }
                    setFinishRefresh(
                        binding.refresh,
                        t.total > mRefreshPage * mRefreshCount
                    )
                }
            }))
    }

    override fun getRefreshDate(stat: Int, page: Int, count: Int) {
        initDate()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == 1) {
            var code = data?.getBooleanExtra(AppConfig.REFRSH, false)
            mRefreshPage = 1
            initDate()
        }
    }

}