package com.base.pdfreaderallpdfreader.ui.document

import android.content.Intent
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.angcyo.tablayout.delegate2.ViewPager2Delegate
import com.base.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.base.BaseActivity
import com.base.pdfreaderallpdfreader.bean.DocumentBean
import com.base.pdfreaderallpdfreader.databinding.ActivityDocumentBinding
import com.base.pdfreaderallpdfreader.ui.search.SearchActivity
import com.base.pdfreaderallpdfreader.utils.BarUtils

class DocumentActivity : BaseActivity<ActivityDocumentBinding>() {

    private lateinit var documentViewModel: DocumentViewModel

    override val binding: ActivityDocumentBinding by lazy {
        ActivityDocumentBinding.inflate(layoutInflater)
    }

    private val allPage: DocumentFragment by lazy {
        DocumentFragment("All")
    }
    private val pdfPage: DocumentFragment by lazy {
        DocumentFragment(DocumentBean.TYPE_PDF)
    }
    private val wordPage: DocumentFragment by lazy {
        DocumentFragment(DocumentBean.TYPE_WORD)
    }
    private val excelPage: DocumentFragment by lazy {
        DocumentFragment(DocumentBean.TYPE_EXCEL)
    }
    private val pptPage: DocumentFragment by lazy {
        DocumentFragment(DocumentBean.TYPE_PPT)
    }

    private var initTab: Int? = null

    private val fragments by lazy {
        mutableListOf(allPage, pdfPage, wordPage, excelPage, pptPage)
    }

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        initTab = intent.extras?.getInt("initTab")

        documentViewModel = ViewModelProvider(this)[DocumentViewModel::class.java]


        val spannableString = SpannableString(this.getString(R.string.pdf_reader))
        spannableString.setSpan(ForegroundColorSpan(Color.parseColor("#E20001")), 0, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
        binding.tvTittle.text = spannableString

        initViewPager2()
        initTabLayout()
    }

    private var pageAdapter: FragmentStateAdapter? = null

    private fun initViewPager2() {
        pageAdapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return fragments.size
            }

            override fun createFragment(position: Int): Fragment {
                return fragments[position]
            }
        }

        binding.viewPager2.run {
            isUserInputEnabled = true
            //https://www.jianshu.com/p/f69bd30cf5b0
            //FragmentStateAdapter 这里必须传人fragment
            adapter = pageAdapter
        }
        binding.viewPager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)

            }

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

    private fun initTabLayout() {
        val allColor = Color.parseColor("#2287FE")
        val pdfColor = Color.parseColor("#F40000")
        val wordColor = Color.parseColor("#2287FE")
        val excelColor = Color.parseColor("#1ABE5D")
        val pptColor = Color.parseColor("#FF9E0E")
        val deselectColor = Color.parseColor("#8F8F8F")

        binding.tabLayoutDocument.apply {
            configTabLayoutConfig {
                tabDeselectColor = deselectColor
                onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
                    val toIndex = selectIndexList.first()
                    binding.viewPager2.currentItem = toIndex
                    when (toIndex) {
                        0 -> {
                            tabSelectColor = allColor
                            tabIndicator.indicatorColor = allColor
                        }

                        1 -> {
                            tabSelectColor = pdfColor
                            tabIndicator.indicatorColor = pdfColor
                        }

                        2 -> {
                            tabSelectColor = wordColor
                            tabIndicator.indicatorColor = wordColor
                        }

                        3 -> {
                            tabSelectColor = excelColor
                            tabIndicator.indicatorColor = excelColor
                        }

                        4 -> {
                            tabSelectColor = pptColor
                            tabIndicator.indicatorColor = pptColor
                        }
                    }
                    dslSelector.updateStyle()
                }
            }
        }
        ViewPager2Delegate.install(binding.viewPager2, binding.tabLayoutDocument)

        initTab?.let { binding.viewPager2.currentItem = it }
    }


    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.ivSearch.setOnClickListener {
            startActivity(Intent(this, SearchActivity::class.java))
        }
    }


}