package com.dahe.gldriver.ui.mine.fragment

import android.graphics.Color
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.DriversAdapter
import com.dahe.gldriver.adapter.RateAdapter
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.RateBean
import com.dahe.gldriver.databinding.FragmentWaybillListBinding
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.ui.mine.InviteDriverActivity
import com.dahe.mylibrary.base.BaseFragment
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 RateFragment
 * @Author john
 * @Date 2024/3/18 09:34
 * @Description TODO
 */
class RateFragment : BaseFragment<FragmentWaybillListBinding>(), RefreshCallBack {
    var rateType: Int = 0

    lateinit var adapter: RateAdapter
    override fun onFragmentVisibleChange(isVisible: Boolean) {
    }

    override fun onFragmentFirstVisible() {
        setRefresh(binding.refresh,this)
        arguments?.let {
            rateType = it.getInt(AppConfig.RATE_TYPE, 0)
        }

        adapter = binding.recyclerView.run {
            layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
            addItemDecoration(
                RecycleViewDivider(
                    LinearLayout.VERTICAL,
                    ConvertUtils.dp2px(16.0f),
                    Color.TRANSPARENT
                )
            )
            adapter = RateAdapter()
            adapter as RateAdapter
        }.apply {
            isStateViewEnable = true
        }

        binding.btnOk.setOnClickListener {
            ActivityUtils.startActivity(mContext, InviteDriverActivity::class.java)
        }
        initData()
    }

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

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