package com.cheng.blzb.ui.fragment.push.list

import android.graphics.Typeface
import android.os.Bundle
import android.view.Gravity
import android.widget.TextView
import com.angcyo.tablayout.DslTabLayout
import com.cheng.blzb.R
import com.cheng.blzb.bean.BidItemEntity
import com.cheng.blzb.bean.BidTypeEntity
import com.cheng.blzb.common.Constants
import com.cheng.blzb.common.EventConstants
import com.cheng.blzb.databinding.FragmentPushListBinding
import com.cheng.blzb.manager.EventReportManager
import com.cheng.blzb.manager.UserConfigManager
import com.cheng.blzb.ui.activity.PublicActivity
import com.cheng.blzb.ui.fragment.bid.BidAdapter
import com.cheng.blzb.ui.fragment.bid.detail.BidDetailFragment
import com.cheng.blzb.ui.fragment.mine.vip.VipFragment
import com.cheng.blzb.ui.fragment.push.subscribe.SubscribeFragment
import com.example.base.decoration.FirstItemOffsetDecoration
import com.example.base.ui.list.ListFragment
import com.example.base.utils.DensityUtils
import com.example.base.widget.PageStatus
import androidx.core.view.isNotEmpty

class PushListFragment : ListFragment<FragmentPushListBinding, PushListViewModel, BidItemEntity>() {
    private val subId by lazy { arguments?.getString("subId") ?: "" }
    private val bidType by lazy { UserConfigManager.getBidTypes() }

    private var item: BidItemEntity? = null

    companion object {
        fun newInstance(subId: String): PushListFragment {
            val args = Bundle()
            args.putString("subId", subId)
            val fragment = PushListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun bindAdapter() = BidAdapter(true)

    override fun noDataClick() {
        PublicActivity.start(requireContext(), SubscribeFragment::class.java)
    }

    override fun initView() {
        super.initView()
        mEmptyView.setNoDataLogo(R.mipmap.ic_empty_data)
        mEmptyView.setNoDataText("暂无数据")
        mEmptyView.setBtnVisible(false)
        mEmptyView.setStatus(PageStatus.NO_DATA)

        binding.mRecyclerView.addItemDecoration(FirstItemOffsetDecoration(DensityUtils.dp2px(7f), FirstItemOffsetDecoration.top))
    }

    override fun initData() {
        super.initData()
        bidType.forEach {
            binding.tabLayout.addView(createTab(it))
        }
    }

    override fun initListener() {
        super.initListener()
        binding.tabLayout.observeIndexChange { fromIndex, toIndex, _, _ ->
            val type = bidType[toIndex]
            mViewModel.params["subId"] = subId
            mViewModel.params["typeId"] = type.id
            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)
            EventReportManager.eventReport(EventConstants.BID_TYPE_CHECK, "home", (binding.tabLayout.getChildAt(toIndex) as TextView).text.toString())
            firstLoad()
        }

        mAdapter.setOnItemClickListener { _, _, i ->
            item = mAdapter.getItem(i)
            mViewModel.checkAuth()
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.authLiveData.observe(this) {
            if (it.auth) {
                PublicActivity.start(
                    requireContext(),
                    BidDetailFragment::class.java,
                    Pair("id", item!!.id),
                    Pair("info_source", item!!.info_source)
                )
            } else {
                PublicActivity.start(requireContext(), VipFragment::class.java, Pair("origin", "view_bid"))
            }
        }
    }

    private fun createTab(type: BidTypeEntity): TextView {
        val tvTab = TextView(requireContext())
        tvTab.text = type.name
        tvTab.gravity = Gravity.CENTER
        tvTab.setBackgroundResource(R.drawable.shape_push_bid_type_default)

        val lp = DslTabLayout.LayoutParams(DensityUtils.dp2px(84f), DensityUtils.dp2px(34f))
        lp.marginStart = if (binding.tabLayout.isNotEmpty()) DensityUtils.dp2px(5f) else DensityUtils.dp2px(12f)
        lp.marginEnd = if (binding.tabLayout.childCount < bidType.size - 1) DensityUtils.dp2px(5f) else DensityUtils.dp2px(12f)
        tvTab.layoutParams = lp

        return tvTab
    }
}