package com.dahe.gldriver.ui.home

import android.graphics.Color
import android.os.Bundle
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.amap.api.services.route.DistanceSearch
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.GridItemAdapter
import com.dahe.gldriver.adapter.WaybillAdapter
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.bean.GridBean
import com.dahe.gldriver.databinding.FragmentHomeBinding
import com.dahe.gldriver.event.OrderStauEvent
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.ui.waybill.activity.WaybillDetailActivity
import com.dahe.gldriver.ui.waybill.activity.WaybillLoadActivity
import com.dahe.gldriver.ui.waybill.activity.WaybillUnlLoadActivity
import com.dahe.glex.bean.OrderBean
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 com.flyco.tablayout.CommonTabLayout
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

/**
 * @ClassName HomeFragment
 * @Author 用户
 * @Date 2023/9/21 10:13
 * @Description TODO
 */
class HomeFragment : BaseFragment<FragmentHomeBinding>(), RefreshCallBack {

    var adapter: WaybillAdapter? = null

    private val gridDatas = mutableListOf(
        GridBean("司机之家", R.drawable.home_driver),
        GridBean("咨询客服", R.drawable.home_ask),
        GridBean("问卷调查", R.drawable.home_que),
        GridBean("知识竞赛", R.drawable.home_know),
    )

    override fun onFragmentVisibleChange(isVisible: Boolean) {
        if (isVisible) {
        }

    }

    lateinit var mListener: ((Int) -> Unit)

    fun setListener(listener: (Int) -> Unit): HomeFragment {
        this.mListener = listener
        return this
    }


    override fun onFragmentFirstVisible() {
        initRecy()
        setRefresh(binding.refresh, this)
        binding.run {
            gridView.run {
                adapter = activity?.let { GridItemAdapter(it, gridDatas) }
                setOnItemClickListener { adapterView, view, i, l ->

                }
            }
            tvMar.isSelected = true

            ivMyWaybill.setOnClickListener {
                if (this@HomeFragment.mListener != null)
                    this@HomeFragment?.mListener?.invoke(2)
            }
        }
        initData()
    }


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

    private fun initData() {
        DataManager.getInstance().orderList()
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(BaseObserver(mContext, object : RxHttpCallBack<MutableList<OrderBean>>() {
                override fun onSuccess(t: CommonResponseBean<MutableList<OrderBean>>) {
                    super.onSuccess(t)

                    if (mRefreshPage === 1) {
                        adapter?.submitList(t.data)
                    } else {
                        adapter?.addAll(t.data)
                    }
                    setFinishRefresh(
                        binding.refresh,
                        t.total > mRefreshPage * mRefreshCount
                    )
                }
            }))
    }


    @Subscribe
    fun eventStatu(orderStauEvent: OrderStauEvent) {
        if (orderStauEvent.statu == 1) {
            initData()
        }
    }

    private fun initRecy() {
        adapter = binding.recyclerView.run {
            layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
            setHasFixedSize(true)
            addItemDecoration(
                RecycleViewDivider(
                    LinearLayout.VERTICAL,
                    ConvertUtils.dp2px(10.0f),
                    Color.TRANSPARENT
                )
            )
            adapter = WaybillAdapter()
            adapter as WaybillAdapter
        }.apply {
            setOnItemClickListener { _, _, position ->
                ActivityUtils.startActivity(
                    mContext,
                    WaybillDetailActivity::class.java,
                    Bundle().apply { putString(AppConfig.ORDER_ID, items[position].orderId) })


//                ActivityUtils.startActivity(
//                    mContext,
//                    WaybillDetailActivity::class.java,
//                    Bundle().apply { putString(AppConfig.ORDER_ID, items[position].orderId) })
            }
            addOnItemChildClickListener(R.id.btnOk) { adapter, view, position ->

                ActivityUtils.startActivity(
                    mContext,
                    WaybillDetailActivity::class.java,
                    Bundle().apply { putString(AppConfig.ORDER_ID, items[position].orderId) })
//                ActivityUtils.startActivity(mContext, WaybillDetailActivity::class.java)
            }
        }

    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this);
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this);
    }
}