package com.cheng.blzb.ui.fragment.bid.publish.goods

import android.graphics.Typeface
import android.widget.TextView
import com.cheng.blzb.R
import com.cheng.blzb.bean.MerchantGoodsEntity
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.FragmentMyGoodsBinding
import com.cheng.blzb.event.SupplyEvent
import com.cheng.blzb.manager.DialogEnum
import com.cheng.blzb.ui.activity.PublicActivity
import com.cheng.blzb.ui.dialog.TipDialog
import com.cheng.blzb.ui.fragment.merchant.detail.MerchantGoodsDetailFragment
import com.example.base.common.RxBus
import com.example.base.extensions.onClick
import com.example.base.ui.BaseFragment
import com.example.base.widget.EmptyView
import com.example.base.widget.PageStatus

class MyGoodsFragment: BaseFragment<FragmentMyGoodsBinding, MyGoodsViewModel>() {
    private val allGoodsList = mutableListOf<MerchantGoodsEntity>()

    private val mAdapter by lazy { MyGoodsAdapter() }
    private val mEmptyView by lazy { EmptyView(requireContext()) }

    private var currentPosition = 0

    private var page = 1

    override fun initView() {
        super.initView()
        binding.mRecyclerView.adapter = mAdapter
        mEmptyView.setNoDataText("暂无数据")
        mEmptyView.setNoDataLogo(R.mipmap.ic_empty_seat)
        mAdapter.setEmptyView(mEmptyView)
    }

    override fun initData() {
        super.initData()
        mViewModel.getGoodsList(page)
    }

    override fun initListener() {
        super.initListener()
        binding.tabLayout.observeIndexChange { fromIndex, toIndex, _, _ ->
            currentPosition = toIndex
            if (fromIndex != -1) {
                (binding.tabLayout.getChildAt(fromIndex) as TextView).typeface = Typeface.DEFAULT
                (binding.tabLayout.getChildAt(fromIndex) as TextView).setBackgroundResource(R.drawable.shape_push_bid_type_default)
            }
            (binding.tabLayout.getChildAt(toIndex) as TextView).typeface = Constants.douyinsansB
            (binding.tabLayout.getChildAt(toIndex) as TextView).setBackgroundResource(R.drawable.shape_push_bid_type_checked)
            setData()
        }

        binding.mRefreshLayout.setOnRefreshListener {
            page = 1
            mViewModel.getGoodsList(page)
        }

        binding.mRefreshLayout.setOnLoadMoreListener {
            page++
            mViewModel.getGoodsList(page)
        }

        mAdapter.setOnItemChildClickListener { _, view, position ->
            val item = mAdapter.getItem(position)
            when(view.id) {
                R.id.btn_delete -> {
                    val f = TipDialog.newInstance("温馨提示", "您确定要删除供应服务吗？")
                    f.setOnSelectListener {
                        if (it == DialogEnum.CLICK_OK) {
                            mViewModel.deleteGoods(item.id)
                        }
                    }
                    f.show(childFragmentManager, TipDialog::class.java.simpleName)
                }

                R.id.btn_edit -> {
                    PublicActivity.start(requireContext(), PublishGoodsMessageFragment::class.java, Pair("item", item))
                }

                R.id.btn_view -> {
                    PublicActivity.start(requireActivity(), MerchantGoodsDetailFragment::class.java, Pair("id", item.id))
                }
            }
        }

        binding.btnNext.onClick {
            PublicActivity.start(requireActivity(), PublishGoodsMessageFragment::class.java)
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.goodsLiveData.observe(this) { list ->
            if (page == 1) allGoodsList.clear()
            allGoodsList.addAll(list)
            if (page == 1) {
                binding.mRefreshLayout.finishRefresh()
            } else {
                binding.mRefreshLayout.finishLoadMore()
                binding.mRefreshLayout.setNoMoreData(list.size < 20)
            }
            binding.mRefreshLayout.setEnableLoadMore(list.size == 20)
            setData()
        }

        mViewModel.deleteLiveData.observe(this) {
            binding.mRefreshLayout.autoRefresh()
        }

        val supplyEvent = RxBus.defaultInstance.toObservable(SupplyEvent::class.java).subscribe {
            binding.mRefreshLayout.autoRefresh()
        }
        addDisposable(supplyEvent)
    }

    private fun setData() {
        when(currentPosition) {
            0 -> mAdapter.setList(allGoodsList)
            1 -> mAdapter.setList(allGoodsList.filter { it.type == "1" })
            2 -> mAdapter.setList(allGoodsList.filter { it.type == "2" })
            3 -> mAdapter.setList(allGoodsList.filter { it.type == "3" })
        }
        if (mAdapter.data.isNotEmpty()) {
            mEmptyView.setStatus(PageStatus.GONG)
        } else {
            mEmptyView.setStatus(PageStatus.NO_DATA)
        }
    }
}