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

import android.graphics.Typeface
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.angcyo.tablayout.DslTabLayout
import com.angcyo.tablayout.delegate.ViewPager1Delegate
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.common.EventConstants
import com.cheng.blzb.databinding.FragmentPushBinding
import com.cheng.blzb.event.SubscriptionEvent
import com.cheng.blzb.manager.EventReportManager
import com.cheng.blzb.manager.UserConfigManager
import com.cheng.blzb.ui.activity.PublicActivity
import com.cheng.blzb.ui.base.BasePageAdapter
import com.cheng.blzb.ui.fragment.push.list.PushListFragment
import com.cheng.blzb.ui.fragment.push.subscribe.SubscribeFragment
import com.example.base.common.RxBus
import com.example.base.extensions.onClick
import com.example.base.ui.BaseFragment
import com.example.base.utils.DensityUtils
import com.example.base.widget.PageStatus


/**
 * 推送
 */
class PushFragment : BaseFragment<FragmentPushBinding, PushViewModel>() {
    private val tabText = mutableListOf<String>()
    private val fragmentList by lazy { mutableListOf<Fragment>() }
    private val pageAdapter by lazy {
        BasePageAdapter(
            childFragmentManager,
            tabText,
            fragmentList,
            FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        )
    }

    override fun initView() {
        super.initView()
        binding.viewPager.adapter = pageAdapter
        ViewPager1Delegate.install(binding.viewPager, binding.tabLayout)

        binding.emptyView.setNoDataLogo(R.mipmap.ic_empty_data)
        binding.emptyView.setNoDataText("没有订阅相关商机，请前往订阅设置添加")
        binding.emptyView.setBtnText("前往订阅设置")
        binding.emptyView.setBtnVisible(true)
        binding.emptyView.setStatus(PageStatus.NO_DATA)
        binding.emptyView.noDataBtn {
            PublicActivity.start(requireContext(), SubscribeFragment::class.java)
        }
    }

    override fun initData() {
        super.initData()
        if (UserConfigManager.getBidTypes().isEmpty()) {
            mViewModel.getBidTypeList()
        } else {
            mViewModel.getSubscriptionList()
        }
    }

    override fun initListener() {
        super.initListener()
        binding.ivAdd.onClick {
            PublicActivity.start(requireContext(), SubscribeFragment::class.java)
            EventReportManager.eventReport(EventConstants.JUMP_TO_SUBSCRIBE, "home", "")
        }

        binding.tabLayout.observeIndexChange { fromIndex, toIndex, _, _ ->
            if (fromIndex != -1) {
                (binding.tabLayout.getChildAt(fromIndex) as TextView).typeface = Typeface.DEFAULT
            }
            (binding.tabLayout.getChildAt(toIndex) as TextView).typeface = Constants.almmsht
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.subscribeLiveData.observe(this) {
            fragmentList.clear()
            binding.tabLayout.removeAllViews()
            if (it.isNotEmpty()) {
                if (it.size == 1) {
                    binding.tabLayout.addView(createTab("专业招标平台"))
                    fragmentList.add(PushListFragment.newInstance(it[0].id))
                } else {
                    it.forEach { item ->
                        binding.tabLayout.addView(createTab(item.name))
                        fragmentList.add(PushListFragment.newInstance(item.id))
                    }
                }
                binding.emptyView.setStatus(PageStatus.GONG)
            } else {
                binding.emptyView.setStatus(PageStatus.NO_DATA)
            }
            pageAdapter.notifyDataSetChanged()
        }

        mViewModel.typeLiveData.observe(this) {
            UserConfigManager.saveBidTypes(it)
            mViewModel.getSubscriptionList()
        }

        val subscriptionEvent = RxBus.defaultInstance.toObservable(SubscriptionEvent::class.java).subscribe {
            if (UserConfigManager.getBidTypes().isEmpty()) {
                mViewModel.getBidTypeList()
            } else {
                mViewModel.getSubscriptionList()
            }
        }
        addDisposable(subscriptionEvent)
    }

    private fun createTab(name: String): TextView {
        tabText.add(name)

        val tvTab = TextView(requireContext())
        tvTab.text = name
        tvTab.typeface = Constants.almmsht
        tvTab.gravity = Gravity.CENTER

        val lp = DslTabLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, DensityUtils.dp2px(26f))
        lp.marginStart = DensityUtils.dp2px(12f)
        lp.marginEnd = DensityUtils.dp2px(12f)
        tvTab.layoutParams = lp

        return tvTab
    }
}