package com.gh.gamecenter.common.view

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.databinding.ViewSegmentedIconFilterBinding
import com.gh.gamecenter.common.utils.DarkModeUtils
import com.gh.gamecenter.common.utils.dip2px

class SegmentedIconFilterView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null): FrameLayout(context, attrs) {
    private var textSize = 12F
    private var containerBackground: Drawable? = null
    private var indicatorBackground: Drawable? = null
    private var containerPadding = 2F.dip2px()

    private val binding: ViewSegmentedIconFilterBinding
    private var currentIndex = 0

    var itemList = listOf<String>()
    var onCheckedAction: ((Int) -> Unit)? = null

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_segmented_icon_filter, this, true)
        binding = ViewSegmentedIconFilterBinding.bind(view)
        initView(attrs)
    }

    fun setContainerBackground(drawable: Drawable?) {
        binding.motionLayout.background = drawable
    }

    private fun initView(attrs: AttributeSet?) {
        if (attrs != null) {
            val ta = context.obtainStyledAttributes(attrs, R.styleable.SegmentedFilterView)
            containerBackground =
                ta.getDrawable(R.styleable.SegmentedFilterView_containerBackground)
            indicatorBackground =
                ta.getDrawable(R.styleable.SegmentedFilterView_indicatorBackground)
            containerPadding = ta.getDimensionPixelSize(
                R.styleable.SegmentedFilterView_containerPadding,
                2F.dip2px()
            )
            textSize = ta.getFloat(R.styleable.SegmentedFilterView_sfv_textSize, 12F)
            ta.recycle()
        }

        binding.motionLayout.run {
            setPadding(containerPadding, containerPadding, containerPadding, containerPadding)
            containerBackground?.let {
                background = it
            }
        }
        indicatorBackground?.let {
            binding.indicator.background = it
        }
        binding.filter1Tv.run {
            textSize = this@SegmentedIconFilterView.textSize
            setOnClickListener {
                if (!isAnimating()){
                    onCheckedAction?.invoke(0)
                }
            }
        }
        binding.filter2Tv.run {
            textSize = this@SegmentedIconFilterView.textSize
            setOnClickListener {
                if (!isAnimating()){
                    onCheckedAction?.invoke(1)
                }
            }
        }
        binding.filter3Tv.run {
            textSize = this@SegmentedIconFilterView.textSize
            setOnClickListener {
                if (!isAnimating()) {
                    onCheckedAction?.invoke(2)
                }
            }
        }
    }

    fun isAnimating() = binding.motionLayout.progress != 0F && binding.motionLayout.progress != 1F

    fun setItemList(itemList: List<String>, defaultCheckedPosition: Int = 0) {
        this.itemList = itemList
        itemList.forEachIndexed { index, name ->
            when (index) {
                0 -> binding.filter1Tv.text = name
                1 -> binding.filter2Tv.text = name
                2 -> binding.filter3Tv.text = name
            }
        }
        setCurrentItem(defaultCheckedPosition)
    }

    fun setCurrentItem(position: Int, duration: Int = 100) {
        val isDarkModeOn = DarkModeUtils.isDarkModeOn(context)
        binding.motionLayout.run {
            when (position) {
                0 -> {
                    if (!isDarkModeOn && currentState != R.id.filter1) {
                        transitionToState(R.id.filter1, duration)
                    } else if (isDarkModeOn && currentState != R.id.filterDark1) {
                        transitionToState(R.id.filterDark1, duration)
                    }
                    currentIndex = 0
                }
                1 -> {
                    if (!isDarkModeOn && currentState != R.id.filter2) {
                        transitionToState(R.id.filter2, duration)
                    } else if (isDarkModeOn && currentState != R.id.filterDark2) {
                        transitionToState(R.id.filterDark2, duration)
                    }
                    currentIndex = 1
                }
                2 -> {
                    if (!isDarkModeOn && currentState != R.id.filter3) {
                        transitionToState(R.id.filter3, duration)
                    } else if (isDarkModeOn && currentState != R.id.filterDark3) {
                        transitionToState(R.id.filterDark3, duration)
                    }
                    currentIndex = 2
                }
            }
        }
    }

    fun updateView(isDarkModeOn: Boolean) {
        binding.motionLayout.run {
            when (currentIndex) {
                0 -> jumpToState(if (isDarkModeOn) R.id.filterDark1 else R.id.filter1)
                1 -> jumpToState(if (isDarkModeOn) R.id.filterDark2 else R.id.filter2)
                2 -> jumpToState(if (isDarkModeOn) R.id.filterDark3 else R.id.filter3)
            }
        }
    }
}