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

import android.graphics.Color
import android.graphics.Typeface
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.angcyo.tablayout.delegate.ViewPager1Delegate
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.FragmentMyPublishBinding
import com.cheng.blzb.ui.activity.PublicActivity
import com.cheng.blzb.ui.base.BasePageAdapter
import com.cheng.blzb.ui.fragment.bid.publish.bid.MyBidFragment
import com.cheng.blzb.ui.fragment.bid.publish.goods.MyGoodsFragment
import com.cheng.blzb.ui.fragment.mine.auth.CompanyAuthFragment
import com.example.base.extensions.onClick
import com.example.base.extensions.visible
import com.example.base.ui.BaseFragment

class MyPublishFragment: BaseFragment<FragmentMyPublishBinding, MyPublishViewModel>() {
    private val type by lazy { arguments?.getInt("type") ?: 1 }

    private val tabText = listOf("招标信息", "合作信息")
    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()
        mTitleBar?.setBackgroundColor(Color.WHITE)

        initFragment()
        binding.viewPager.adapter = pageAdapter
        binding.viewPager.offscreenPageLimit = fragmentList.size
        binding.tabLayout.setupViewPager(ViewPager1Delegate.install(binding.viewPager, binding.tabLayout))
    }

    private fun initFragment() {
        fragmentList.add(MyBidFragment())
        fragmentList.add(MyGoodsFragment())
    }

    override fun initData() {
        super.initData()
        mViewModel.getCompanyInfo()

        binding.viewPager.setCurrentItem(if (type == 1) 0 else 1, false)
    }

    override fun initListener() {
        super.initListener()
        binding.tabLayout.observeIndexChange { fromIndex, toIndex, _, _ ->
            binding.viewPager.currentItem = toIndex
            if (fromIndex != -1) {
                (binding.tabLayout.getChildAt(fromIndex) as TextView).typeface = Typeface.DEFAULT
            }
            (binding.tabLayout.getChildAt(toIndex) as TextView).typeface = Constants.douyinsansB
        }

        binding.layoutAuthTip.onClick {
            PublicActivity.start(requireActivity(), CompanyAuthFragment::class.java)
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.companyLiveData.observe(this) {
            if (it == null || it.status == "3") {
                binding.layoutAuthTip.visible()
            }
        }
    }
}