package com.develop.food.base.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import com.develop.base.R
import com.develop.base.databinding.DirectionViewBinding


class DirectionView : FrameLayout {

    private var binding: DirectionViewBinding


    var onDirectionClickListener: OnDirectionClickListener? = null

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

    }

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

    }

    init {
        val root = View.inflate(context, R.layout.direction_view, this)
        binding = DirectionViewBinding.bind(root)
        binding.tvTurnLeft.setOnClickListener {
            updateDirectionView(Direction.RIGHT)
            onDirectionClickListener?.onDirectionClick(Direction.RIGHT)
        }

        binding.tvTurnRight.setOnClickListener {
            updateDirectionView(Direction.LEFT)
            onDirectionClickListener?.onDirectionClick(Direction.LEFT)
        }
    }

    fun updateDirectionView(direction: Direction) {
        if (direction == Direction.RIGHT) {
            binding.ivDirection.rotationY = 180f
            binding.tvTurnRight.setTextColor(0xffffffff.toInt())
            binding.tvTurnLeft.setTextColor(0xffE60012.toInt())

        } else {
            binding.ivDirection.rotationY = 0f
            binding.tvTurnRight.setTextColor(0xffE60012.toInt())
            binding.tvTurnLeft.setTextColor(0xffffffff.toInt())
        }
    }

    interface OnDirectionClickListener {
        fun onDirectionClick(direction: Direction)
    }

    fun isCanChangeDirection(isCanChange:Boolean){
        binding.tvTurnRight.isClickable = isCanChange
        binding.tvTurnLeft.isClickable = isCanChange
    }

    enum class Direction {
        LEFT,
        RIGHT
    }
}