package com.example.tfat.myapplication.controlbar

import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import android.view.LayoutInflater
import android.widget.ImageButton
import com.example.tfat.myapplication.R

class ControlBarView : LinearLayout {

    private lateinit var scene : ImageButton
    private lateinit var layer1 : ImageButton
    private lateinit var layer2 : ImageButton
    private lateinit var layer3 : ImageButton
    private lateinit var layer4 : ImageButton
    private lateinit var layerBackground : ImageButton

    constructor(context : Context) : super(context) {
        initView()
    }

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

    constructor(context: Context, attrs: AttributeSet, defStyle: Int): super(context, attrs, defStyle) {
        initView()
    }

    private fun initView() {
        val inflater = LayoutInflater.from(context)
        var rootView = inflater.inflate(R.layout.control_bar_view, this, true)
        scene = rootView.findViewById(R.id.scene_btn)
        layer1 = rootView.findViewById(R.id.layer_1)
        layer2 = rootView.findViewById(R.id.layer_2)
        layer3 = rootView.findViewById(R.id.layer_3)
        layer4 = rootView.findViewById(R.id.layer_4)
        layerBackground = rootView.findViewById(R.id.layer_background)
    }

    fun setBtnSelected(layer : Int) {
        when (layer) {
            0 -> {
                layerBackground.setBackgroundResource(R.drawable.layer_icon_selected_background)
                layer4.setBackgroundResource(R.drawable.layer_icon_background)
                layer3.setBackgroundResource(R.drawable.layer_icon_background)
                layer2.setBackgroundResource(R.drawable.layer_icon_background)
                layer1.setBackgroundResource(R.drawable.layer_icon_background)
                scene.setBackgroundResource(R.drawable.layer_icon_background)
            }
            1 -> {
                layerBackground.setBackgroundResource(R.drawable.layer_icon_background)
                layer4.setBackgroundResource(R.drawable.layer_icon_background)
                layer3.setBackgroundResource(R.drawable.layer_icon_background)
                layer2.setBackgroundResource(R.drawable.layer_icon_background)
                layer1.setBackgroundResource(R.drawable.layer_icon_selected_background)
                scene.setBackgroundResource(R.drawable.layer_icon_background)
            }
            2 -> {
                layerBackground.setBackgroundResource(R.drawable.layer_icon_background)
                layer4.setBackgroundResource(R.drawable.layer_icon_background)
                layer3.setBackgroundResource(R.drawable.layer_icon_background)
                layer2.setBackgroundResource(R.drawable.layer_icon_selected_background)
                layer1.setBackgroundResource(R.drawable.layer_icon_background)
                scene.setBackgroundResource(R.drawable.layer_icon_background)
            }
            3 -> {
                layerBackground.setBackgroundResource(R.drawable.layer_icon_background)
                layer4.setBackgroundResource(R.drawable.layer_icon_background)
                layer3.setBackgroundResource(R.drawable.layer_icon_selected_background)
                layer2.setBackgroundResource(R.drawable.layer_icon_background)
                layer1.setBackgroundResource(R.drawable.layer_icon_background)
                scene.setBackgroundResource(R.drawable.layer_icon_background)
            }
            4 -> {
                layerBackground.setBackgroundResource(R.drawable.layer_icon_background)
                layer4.setBackgroundResource(R.drawable.layer_icon_selected_background)
                layer3.setBackgroundResource(R.drawable.layer_icon_background)
                layer2.setBackgroundResource(R.drawable.layer_icon_background)
                layer1.setBackgroundResource(R.drawable.layer_icon_background)
                scene.setBackgroundResource(R.drawable.layer_icon_background)
            }
            5 -> {
                layerBackground.setBackgroundResource(R.drawable.layer_icon_background)
                layer4.setBackgroundResource(R.drawable.layer_icon_background)
                layer3.setBackgroundResource(R.drawable.layer_icon_background)
                layer2.setBackgroundResource(R.drawable.layer_icon_background)
                layer1.setBackgroundResource(R.drawable.layer_icon_background)
                scene.setBackgroundResource(R.drawable.layer_icon_selected_background)
            }
        }
    }

    fun initState() {
        layerBackground.setBackgroundResource(R.drawable.layer_icon_background)
        layer4.setBackgroundResource(R.drawable.layer_icon_background)
        layer3.setBackgroundResource(R.drawable.layer_icon_background)
        layer2.setBackgroundResource(R.drawable.layer_icon_background)
        layer1.setBackgroundResource(R.drawable.layer_icon_background)
        scene.setBackgroundResource(R.drawable.layer_icon_selected_background)
    }

    fun setonClickLayerSceneBtn(onClickListener: OnClickListener) {
        scene.setOnClickListener(onClickListener)
    }

    fun setonClickLayer1Btn(onClickListener: OnClickListener) {
        layer1.setOnClickListener(onClickListener)
    }
    fun setonClickLayer2Btn(onClickListener: OnClickListener) {
        layer2.setOnClickListener(onClickListener)
    }
    fun setonClickLayer3Btn(onClickListener: OnClickListener) {
        layer3.setOnClickListener(onClickListener)
    }
    fun setonClickLayer4Btn(onClickListener: OnClickListener) {
        layer4.setOnClickListener(onClickListener)
    }
    fun setonClickLayerBackgroundBtn(onClickListener: OnClickListener) {
        layerBackground.setOnClickListener(onClickListener)
    }
}