package com.develop.food.base.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.develop.base.R
import com.develop.base.databinding.CookCardViewBinding
import com.develop.food.base.utils.ResDimension


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.viewSelectAngle.visibility = View.VISIBLE
            binding.tvChangeValue.isSelected = true
            binding.tvTitle.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()
        when (cardType) {
            1 -> {
                setCardType(CardType.TEMPERATURE, cardSelected)
            }
            2 -> {
                setCardType(CardType.COOK_TIME, cardSelected)
            }
            3 -> {
                setCardType(CardType.SPEED, cardSelected)
            }
            4 -> {
                setCardType(CardType.DIRECTION, 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 = ResDimension(R.dimen.convert_36px)
        } else {
            binding.tvChangeValue.paint.textSize = ResDimension(R.dimen.convert_54px)
        }
        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)
            }
        }
    }

    fun upSelectedView(selected: Boolean) {
        if (selected) {
            binding.viewBackground.isSelected = true
            binding.tvChangeValue.isSelected = true
            binding.tvTitle.isSelected = true
            binding.viewIcon.isSelected = true
            binding.viewSelectAngle.visibility = View.VISIBLE
        } else {
            binding.viewBackground.isSelected = false
            binding.tvChangeValue.isSelected = false
            binding.tvTitle.isSelected = false
            binding.viewIcon.isSelected = false
            binding.viewSelectAngle.visibility = View.INVISIBLE
        }
    }


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