package com.gh.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.setDrawableEnd
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.entity.SubjectSettingEntity
import com.google.android.flexbox.FlexboxLayout

class ConfigFilterView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private var mSizeTv: TextView
    private var mPopupWindow: PopupWindow? = null

    var newestTv: TextView
    var ratingTv: TextView //目前只在专题页面显示
    var recommendedTv: TextView
    var updateTv: TextView //更新
    var container: View

    var sizeFilterArray: ArrayList<SubjectSettingEntity.Size>? = null

    private var mOnConfigFilterSetupListener: OnConfigFilterSetupListener? = null
    private var mSelectionTvList: ArrayList<TextView>

    init {
        View.inflate(context, R.layout.layout_config_filter, this)

        mSizeTv = findViewById(R.id.size_tv)
        newestTv = findViewById(R.id.newest_tv)
        ratingTv = findViewById(R.id.rating_tv)
        updateTv = findViewById(R.id.updateTv)
        recommendedTv = findViewById(R.id.recommended_tv)
        container = findViewById(R.id.config_controller)

        mSelectionTvList = arrayListOf(newestTv, ratingTv, updateTv, recommendedTv)

        mSizeTv.setOnClickListener {
            mOnConfigFilterSetupListener?.onShowSortSize()
            showSelectionPopupWindow(this, mSizeTv, mSizeTv.text.toString())
        }

        ratingTv.setOnClickListener {
            mOnConfigFilterSetupListener?.onSetupSortType(SortType.RATING)
            updateHighlightedTextView(ratingTv)
        }

        updateTv.setOnClickListener {
            mOnConfigFilterSetupListener?.onSetupSortType(SortType.UPDATE)
            updateHighlightedTextView(updateTv)
        }

        newestTv.setOnClickListener {
            mOnConfigFilterSetupListener?.onSetupSortType(SortType.NEWEST)
            updateHighlightedTextView(newestTv)
        }

        recommendedTv.setOnClickListener {
            mOnConfigFilterSetupListener?.onSetupSortType(SortType.RECOMMENDED)
            updateHighlightedTextView(recommendedTv)
        }
    }

    private fun updateHighlightedTextView(highlightedTv: TextView) {
        for (tv in mSelectionTvList) {
            toggleHighlightedTextView(tv, tv == highlightedTv)
        }
    }

    fun updateAllTextView(sortType: SortType) {
        when (sortType) {
            SortType.RECOMMENDED -> updateHighlightedTextView(recommendedTv)
            SortType.NEWEST -> updateHighlightedTextView(newestTv)
            SortType.RATING -> updateHighlightedTextView(ratingTv)
            SortType.UPDATE -> updateHighlightedTextView(updateTv)
        }
        mSizeTv.setTextColor(R.color.text_757575.toColor(context))
    }

    fun setOnConfigSetupListener(onConfigFilterSetupListener: OnConfigFilterSetupListener) {
        mOnConfigFilterSetupListener = onConfigFilterSetupListener
    }

    private fun toggleHighlightedTextView(targetTextView: TextView, highlightIt: Boolean) {
        if (highlightIt) {
            targetTextView.background = R.drawable.bg_tag_text.toDrawable()
            targetTextView.setTextColor(R.color.text_white.toColor(context))
        } else {
            targetTextView.background = null
            targetTextView.setTextColor(R.color.text_757575.toColor(context))
        }
    }

    fun updatePopupWindow() {
        if (mPopupWindow != null) {
            mPopupWindow?.dismiss()
            showSelectionPopupWindow(this, mSizeTv, mSizeTv.text.toString())
        }
    }

    private fun showSelectionPopupWindow(containerView: View, sizeTv: TextView, sizeText: String) {
        sizeTv.setTextColor(R.color.theme_font.toColor(sizeTv.context))
        sizeTv.setDrawableEnd(R.drawable.ic_filter_arrow_up)

        val inflater = LayoutInflater.from(sizeTv.context)
        val layout = inflater.inflate(R.layout.layout_filter_size, null)
        val popupWindow = PopupWindow(
            layout,
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        ).apply { mPopupWindow = this }

        val flexboxLayout = layout.findViewById<FlexboxLayout>(R.id.flexbox)
        val backgroundView = layout.findViewById<View>(R.id.background)

        sizeFilterArray = if (sizeFilterArray == null) {
            getDefaultSizeFilterArray()
        } else {
            sizeFilterArray?.apply {
                if (firstOrNull()?.text != "全部大小") {
                    add(0, SubjectSettingEntity.Size(min = -1, max = -1, text = "全部大小"))
                }
            }
        }

        backgroundView.setOnClickListener {
            popupWindow.dismiss()
        }

        for (size in sizeFilterArray!!) {
            val item = inflater.inflate(R.layout.item_filter_size, flexboxLayout, false)

            // 单列 4 个，强行设置宽度为屏幕的 1/4
            val width = sizeTv.context.resources.displayMetrics.widthPixels / 4
            val height = item.layoutParams.height

            item.layoutParams = ViewGroup.LayoutParams(width, height)
            flexboxLayout.addView(item)

            val tv = item.findViewById<TextView>(R.id.size_tv)
            tv.text = size.text

            if (sizeText == size.text) {
                toggleHighlightedTextView(tv, true)
            } else {
                toggleHighlightedTextView(tv, false)
            }

            tv.tag = size.text

            item.setOnClickListener {
                toggleHighlightedTextView(tv, true)
                popupWindow.dismiss()
                sizeTv.text = size.text

                mOnConfigFilterSetupListener?.onSetupSortSize(size)
            }
        }

        popupWindow.setOnDismissListener {
            sizeTv.setTextColor(R.color.text_757575.toColor(sizeTv.context))
            sizeTv.setDrawableEnd(R.drawable.ic_filter_arrow_down)
            mPopupWindow = null
        }

        popupWindow.isTouchable = true
        popupWindow.isFocusable = true
        popupWindow.animationStyle = 0
        popupWindow.showAsDropDown(containerView, 0, 0)
    }

    private fun getDefaultSizeFilterArray(): ArrayList<SubjectSettingEntity.Size> {
        return arrayListOf<SubjectSettingEntity.Size>().apply {
            add(SubjectSettingEntity.Size(min = -1, max = -1, text = "全部大小"))
            add(SubjectSettingEntity.Size(min = -1, max = 100, text = "100M以下"))
            add(SubjectSettingEntity.Size(min = 100, max = 300, text = "100-300M"))
            add(SubjectSettingEntity.Size(min = 300, max = 500, text = "300-500M"))
            add(SubjectSettingEntity.Size(min = 500, max = 1000, text = "500M-1G"))
            add(SubjectSettingEntity.Size(min = 1000, max = -1, text = "1G以上"))
        }
    }

    interface OnConfigFilterSetupListener {
        fun onShowSortSize()
        fun onSetupSortSize(sortSize: SubjectSettingEntity.Size)
        fun onSetupSortType(sortType: SortType)
    }

    enum class SortType {
        RECOMMENDED,
        NEWEST,
        RATING,
        UPDATE
    }

}