package com.dahe.gldriver.ui.mine

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.adapter.DriversAdapter
import com.dahe.gldriver.adapter.InviteRecordAdapter
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.DriverBean
import com.dahe.gldriver.bean.InviteRecord
import com.dahe.gldriver.databinding.ActivityInviteDriverBinding
import com.dahe.gldriver.databinding.ActivityInviteRecordBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.utils.CommonPopUtils
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.ConvertUtils
import com.lxj.xpopup.util.KeyboardUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @ClassName InviteDriverActivity
 * @Author john
 * @Date 2024/3/11 16:13
 * @Description 邀请记录
 */
class InviteRecordActivity : BaseActivity<ActivityInviteRecordBinding>(), RefreshCallBack {

    lateinit var adapter: InviteRecordAdapter
    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarColor(R.color.white)
        setTitleBar("邀请记录", true)
        binding.run {
            setRefresh(refresh, this@InviteRecordActivity)
            adapter = recycler.run {
                layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
                addItemDecoration(
                    RecycleViewDivider(
                        LinearLayout.VERTICAL,
                        ConvertUtils.dp2px(16.0f),
                        Color.TRANSPARENT
                    )
                )
                adapter = InviteRecordAdapter()
                adapter as InviteRecordAdapter
            }.apply {
                isStateViewEnable = true
                setStateViewLayout(mContext, R.layout.empty_view)
                addOnItemChildClickListener(R.id.tvInvite) { _, _, postion ->
                    var data = items[postion] as DriverBean
                    CommonPopUtils.getInstance()
                        .showCommCenterPop(
                            mContext,
                            content = """确定邀请 ${data.idcardName} 加入车队?"""
                        ) {
                            DataManager.getInstance().askToDriver(DriverBean().apply {
                                driverId = data.driverId
                            }).subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(BaseObserver(mContext, object : RxHttpCallBack<Int>() {
                                    override fun onSuccess(t: CommonResponseBean<Int>) {
                                        super.onSuccess(t)
                                        showToast("邀请成功,等待对方同意")
                                    }
                                }))
                        }
                }
            }


        }
    }

    override fun initDate() {
        DataManager.getInstance().selectCaptainAskByDriverHistoryList().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<MutableList<InviteRecord>>() {
                override fun onSuccess(t: CommonResponseBean<MutableList<InviteRecord>>) {
                    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()
    }


    /**
     * TODO
     * 获取邀请记录
     */
    private fun getRecord() {

        DataManager.getInstance().selectCaptainAskByDriverHistoryList().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<MutableList<InviteRecord>>() {
                override fun onSuccess(t: CommonResponseBean<MutableList<InviteRecord>>) {
                    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
                    )
                }
            }))
    }
}
