package com.duben.microtribe.ui.widgets

import android.app.Dialog
import android.content.Context
import android.view.*
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.duben.microtribe.R
import com.duben.microtribe.mvp.model.EpisodeBean
import com.duben.microtribe.mvp.model.VEDIO3DOS_FREE
import com.duben.microtribe.mvp.model.VedioBean
import com.duben.microtribe.mvp.model.VideoMultiItemEntity4
import com.duben.microtribe.ui.adapter.VideoEpisodeAdapter
import com.duben.microtribe.utils.CommonUtils

/**
 *  @author Assen
 *  @date 2023/7/5
 *  @desc
 */
class NewVideoEpisodeDialog(
    context: Context,
    val vedioBean: VedioBean?,
    val data: List<VideoMultiItemEntity4>
) :
    Dialog(context, R.style.dialog) {

    private var mRealSeeIndex = 0

    private val lp: WindowManager.LayoutParams

    private val vp2: ViewPager2
    private val tab: TabLayout
    private val ivClose: ImageView

    private val mData = mutableListOf<EpisodeBean>()
    private var adapter: VideoEpisodeAdapter? = null

    init {
        setContentView(R.layout.dialog_video_episode)
        // 设置window属性
        lp = window!!.attributes
        lp.gravity = Gravity.BOTTOM
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        lp.windowAnimations = R.style.DialogAnimBottom
        //		lp.dimAmount = 0; // 去背景遮盖
        // lp.alpha = 1.0f;//透明效果
        window!!.attributes = lp

        // 设置外部不可关闭
        setCancelable(true)
        setCanceledOnTouchOutside(true)
        setOnKeyListener { _, i, _ ->
            i == KeyEvent.KEYCODE_BACK
        }

        vp2 = findViewById(R.id.vp2_episode)
        tab = findViewById(R.id.tab_episode)
        ivClose = findViewById(R.id.close_iv)
        ivClose.setOnClickListener { dismiss() }

        findViewById<TextView>(R.id.title_tv).text = vedioBean?.title
        findViewById<TextView>(R.id.label_tv).text =
            if (vedioBean?.completeStatus == 0) "已完结" else "更新中"

        initVp()
    }

    private fun initVp() {
        if (vedioBean == null) return
        for (i in 0 until vedioBean.vedioTotal) {
            if (data[i].type == VEDIO3DOS_FREE) {
                var playing = false
                if (vedioBean.seeIndex == i) {
                    playing = true
                }
                mData.add(EpisodeBean(false, playing, "" + (i + 1)))
            } else {
                mData.add(EpisodeBean(lock = true, playing = false, title = "" + (i + 1)))
            }
        }
        val subList = CommonUtils.getSubList(30, mData)
        adapter = VideoEpisodeAdapter()
        adapter?.setNewInstance(subList)
        vp2.adapter = adapter
        TabLayoutMediator(tab, vp2) { tab, position ->
            // 初始化Tab
            tab.id = position
            tab.customView =
                getTabView(
                    subList[position][0].title + "-"
                            + subList[position][subList[position].size - 1].title,
                    position
                )
        }.attach()
        tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                updateTab(tab, true)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                updateTab(tab, false)
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })

        if (mRealSeeIndex == 0) {
            tab.getTabAt(vedioBean.seeIndex / 30)?.select()
        } else {
            tab.getTabAt(mRealSeeIndex / 30)?.select()
        }
    }


    private fun getTabView(text: String, position: Int): View {
        val view = LayoutInflater.from(context).inflate(R.layout.item_epsiode_tab, null)
        val viewText = view.findViewById<TextView>(R.id.item_tv)
        viewText.text = text
        return view
    }

    private fun updateTab(tab: TabLayout.Tab?, isSelected: Boolean) {
        tab?.customView?.let {
            val text = it.findViewById<TextView>(R.id.item_tv)
            if (isSelected) {
                text.setTextColor(ContextCompat.getColor(context, R.color.red))
            } else {
                text.setTextColor(ContextCompat.getColor(context, R.color.black))
            }
        }
    }

    fun changeLockStatus(unlockIndex: Int) {
        for (i in 0 until mData.size) {
            mData[i].lock = i > unlockIndex
        }
        adapter?.notifyDataSetChanged()
    }

    fun setCurrentIndex(position: Int) {
        mRealSeeIndex = position
        for (mDatum in mData) {
            mDatum.playing = false
        }
        if (mData.size > position) {
            mData[position].playing = true
            adapter?.notifyDataSetChanged()
        }

    }

    private var mOnEpisodeClickListener: VideoEpisodeAdapter.OnEpisodeClickListener? = null

    fun setOnEpisodeClickListener(onEpisodeClickListener: VideoEpisodeAdapter.OnEpisodeClickListener) {
        mOnEpisodeClickListener = onEpisodeClickListener
        adapter?.setOnEpisodeClickListener(mOnEpisodeClickListener)
    }

}