package com.cheng.blzb.ui.fragment.mine.coupon

import android.graphics.Typeface
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.cheng.blzb.R
import com.cheng.blzb.databinding.FragmentCouponBinding
import com.cheng.blzb.ui.base.BasePageAdapter
import com.cheng.blzb.ui.fragment.mine.coupon.list.CouponListFragment
import com.example.base.extensions.getColor
import com.example.base.extensions.gone
import com.example.base.extensions.onClick
import com.example.base.extensions.visible
import com.example.base.ui.BaseFragment

class CouponFragment : BaseFragment<FragmentCouponBinding, CouponViewModel>() {
    private val fragmentList by lazy { mutableListOf<Fragment>() }
    private val pageAdapter by lazy { BasePageAdapter(childFragmentManager, listOf("未使用", "已过期"), fragmentList) }

    override fun initView() {
        super.initView()
        mTitleBar?.background = null
        mTitleBar?.titleView?.setTextColor(getColor(R.color.color_1a1a1a))
        setBackColor(R.color.black)

        fragmentList.add(CouponListFragment.newInstance("1", "1"))
        fragmentList.add(CouponListFragment.newInstance("1", "2"))

        binding.viewPager.adapter = pageAdapter
    }

    override fun initData() {
        super.initData()
    }

    override fun initListener() {
        super.initListener()

        binding.tvUnused.onClick {
            binding.viewPager.setCurrentItem(0)
        }

        binding.tvExpired.onClick {
            binding.viewPager.setCurrentItem(1)
        }

        binding.viewPager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                setTabStyle(position)
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
    }

    override fun initObserve() {
        super.initObserve()
    }

    private fun setTabStyle(position: Int) {
        if (position == 0) {
            binding.tvUnused.textSize = 16f
            binding.tvUnused.setTextColor(getColor(R.color.color_1a1a1a))
            binding.tvUnused.typeface = Typeface.DEFAULT_BOLD
            binding.ivUnusedIndicator.visible()

            binding.tvExpired.textSize = 14f
            binding.tvExpired.setTextColor(getColor(R.color.color_999999))
            binding.tvExpired.typeface = Typeface.DEFAULT
            binding.ivExpiredIndicator.gone()
        } else {
            binding.tvExpired.textSize = 16f
            binding.tvExpired.setTextColor(getColor(R.color.color_1a1a1a))
            binding.tvExpired.typeface = Typeface.DEFAULT_BOLD
            binding.ivExpiredIndicator.visible()

            binding.tvUnused.textSize = 14f
            binding.tvUnused.setTextColor(getColor(R.color.color_999999))
            binding.tvUnused.typeface = Typeface.DEFAULT
            binding.ivUnusedIndicator.gone()
        }
    }
}