package com.develop.common.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.develop.base.ext.resId2Dimension
import com.develop.common.R
import com.develop.common.databinding.CookCardViewBinding


class CookCardView : ConstraintLayout {

    var binding: CookCardViewBinding
    private var currentType = CardType.TEMPERATURE
    private var cardSelected = false
    var onCookCardViewListener: OnCookCardViewListener? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initAttrs(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initAttrs(context, attrs)
    }

    init {
        val root = View.inflate(context, R.layout.cook_card_view, this)
        binding = CookCardViewBinding.bind(root)
        binding.root.setOnClickListener {
            binding.viewBackground.isSelected = true
            binding.tvChangeValue.isSelected = true
            binding.tvTitle.isSelected = true
            binding.tvConfigValue.isSelected = true
            onCookCardViewListener?.onCookSelected(currentType)
        }
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        var cardType: Int
        var cardSelected: Boolean
        context.obtainStyledAttributes(attrs, R.styleable.CookCardView).apply {
            cardType = getInt(R.styleable.CookCardView_cardType, 0)
            cardSelected = getBoolean(R.styleable.CookCardView_cardSelected, false)
        }.recycle()
        binding.waterSprayShow.visibility = GONE
        binding.waterSprayShow.updateSelectedStatus(false)
        when (cardType) {
            1 -> {
                setCardType(CardType.TEMPERATURE, cardSelected)
            }
            2 -> {
                setCardType(CardType.COOK_TIME, cardSelected)
            }
            3 -> {
                setCardType(CardType.SPEED, cardSelected)
            }
            4 -> {
                setCardType(CardType.DIRECTION, cardSelected)
            }
            5 -> {
                setCardType(CardType.WATER_SPARY, cardSelected)
            }
        }
    }


    interface OnCookCardViewListener {
        fun onCookSelected(type: CardType)
    }

    fun setConfigValue(value: String) {
        binding.tvConfigValue.apply {
            visibility = View.VISIBLE
            text = value
        }
    }

    fun updateChangeValue(changeValue: String) {
        if (currentType == CardType.DIRECTION) {
            binding.tvChangeValue.paint.textSize =
                com.develop.base.R.dimen.convert_32px.resId2Dimension()
        } else {
            binding.tvChangeValue.paint.textSize =
                com.develop.base.R.dimen.convert_40px.resId2Dimension()
        }
        binding.tvChangeValue.text = changeValue
    }

    fun setCardType(cardType: CardType, selected: Boolean = false) {
        currentType = cardType
        cardSelected = selected
        upSelectedView(cardSelected)
        when (cardType) {
            CardType.TEMPERATURE -> {
                binding.viewIcon.setBackgroundResource(R.drawable.cook_temp_selector)
                binding.tvTitle.text = context.getString(R.string.temperature)
            }
            CardType.COOK_TIME -> {
                binding.viewIcon.setBackgroundResource(R.drawable.cook_time_selector)
                binding.tvTitle.text = context.getString(R.string.time)
            }
            CardType.SPEED -> {
                binding.viewIcon.setBackgroundResource(R.drawable.cook_speed_selector)
                binding.tvTitle.text = context.getString(R.string.speed)
            }
            CardType.DIRECTION -> {
                binding.viewIcon.setBackgroundResource(R.drawable.cook_direction_selector)
                binding.tvTitle.text = context.getString(R.string.direction)
            }
            CardType.WATER_SPARY -> {
                binding.viewIcon.setBackgroundResource(R.drawable.cook_water_selector)
                binding.tvTitle.text = context.getString(R.string.water_spary)
                binding.waterSprayShow.visibility = VISIBLE
            }
        }
    }

    fun upSelectedView(selected: Boolean) {
        if (selected) {
            binding.viewBackground.isSelected = true
            binding.tvChangeValue.isSelected = true
            binding.tvTitle.isSelected = true
            binding.viewIcon.isSelected = true
            binding.tvConfigValue.isSelected = true
        } else {
            binding.viewBackground.isSelected = false
            binding.tvChangeValue.isSelected = false
            binding.tvTitle.isSelected = false
            binding.viewIcon.isSelected = false
            binding.tvConfigValue.isSelected = false
        }
        binding.waterSprayShow.updateSelectedStatus(selected)

    }


    enum class CardType {
        TEMPERATURE,
        COOK_TIME,
        SPEED,
        DIRECTION,
        WATER_SPARY
    }
}