package com.swago.home

import android.Manifest
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Typeface
import android.widget.Toast
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.launcher.ARouter
import com.permissionx.guolindev.PermissionX
import com.swago.baseswago.GradientIndicator
import com.swago.baseswago.SwagoAdapter
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.dialog.BenefitBagDialog
import com.swago.baseswago.fragment.BaseXFragment
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.DpPxUtil
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.home.databinding.FragmentHomeBinding
import com.swago.home.innerhome.FollowFragment
import com.swago.home.innerhome.HotFragment
import com.swago.home.innerhome.NewFragment
import com.swago.home.search.SearchActivity
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 *@date 2021/11/20 10:31
 *description:
 */
class HomeFragment : BaseXFragment<FragmentHomeBinding>() {

    private val listFragments = arrayListOf(HotFragment(), NewFragment(), FollowFragment())
    private val listTitles = arrayListOf(
        AppContext.getContext().resources.getString(R.string.home_hot),
        AppContext.getContext().resources.getString(R.string.home_new),
        AppContext.getContext().resources.getString(R.string.home_follow)
    )


    override fun loadData() {
    }

    override fun initOther() {
        binding.viewPager.offscreenPageLimit = 3
        val viewPagerAdapter = SwagoAdapter(listFragments, childFragmentManager, listTitles)
        binding.viewPager.adapter = viewPagerAdapter

        val commonNavigator = CommonNavigator(context)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return listTitles.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView = SimplePagerTitleView(context)
                simplePagerTitleView.text = listTitles[index]
                val padding = DpPxUtil.dip2px(15f).toFloat()
                simplePagerTitleView.textSize = 16f
                simplePagerTitleView.typeface = Typeface.createFromAsset(AppContext.getContext().assets,"bold.otf")
                simplePagerTitleView.setPadding(padding.toInt(), 0, padding.toInt(), 0)
                simplePagerTitleView.normalColor = Color.parseColor("#828C9D")
                simplePagerTitleView.selectedColor = Color.parseColor("#131B23")
                simplePagerTitleView.setOnClickListener {
                    binding.viewPager.currentItem = index
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.setColors(Color.parseColor("#131B23"))
                indicator.mode = GradientIndicator.MODE_WRAP_CONTENT
                indicator.lineWidth = DpPxUtil.dip2px(12f).toFloat()
                indicator.xOffset = DpPxUtil.dip2px(5f).toFloat()
                indicator.yOffset = DpPxUtil.dip2px(14f).toFloat()
                indicator.lineHeight = DpPxUtil.dip2px(2f).toFloat()
                return indicator
            }

        }

        binding.magicIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(binding.magicIndicator, binding.viewPager)

        binding.ivSearch.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                startActivity(Intent(activity,SearchActivity::class.java))
            }
        })
        binding.tvStartLive.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                binding.tvStartLive.isEnabled = false
                PermissionX.init(this@HomeFragment)
                    .permissions(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO)
                    .request { allGranted, grantedList, deniedList ->
                        if (allGranted) {
                            ARouter.getInstance().build(ARouteConstant.Room.anchor).navigation()
                        } else {
                            binding.tvStartLive.isEnabled = true
                            Toast.makeText(
                                AppContext.getContext(),
                                "These permissions are denied: $deniedList",
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }
            }
        })

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

            override fun onPageSelected(position: Int) {
                when(position){
                    0 -> {
                        (listFragments[position] as HotFragment).loadData()
                    }
                    1 -> {
                        (listFragments[position] as NewFragment).loadData()
                    }
                    2 -> {
                        (listFragments[position] as FollowFragment).loadData()
                    }
                }
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
    }

    override fun initLiveData() {
    }




    override fun onResume() {
        super.onResume()
        binding.tvStartLive.isEnabled = true
    }
}