package com.kdanmobile.reader.screen.view

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import com.kdanmobile.reader.R
import kotlinx.android.synthetic.main.view_color_select.view.*

class ColorSelectView : ConstraintLayout {

    interface OnColorSelectedListener {
        fun onColorSelected(color: Int?)
    }

    var onColorSelectedListener: OnColorSelectedListener? = null

    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() {
        LayoutInflater.from(context).inflate(R.layout.view_color_select, this)

        colorSelect_column1.setOnClickListener(this::setColorSelect)
        colorSelect_column2.setOnClickListener(this::setColorSelect)
        colorSelect_column3.setOnClickListener(this::setColorSelect)
        colorSelect_column4.setOnClickListener(this::setColorSelect)
        colorSelect_column5.setOnClickListener(this::setColorSelect)
        colorSelect_column6.setOnClickListener(this::setColorSelect)
        colorSelect_column7.setOnClickListener(this::setColorSelect)
        colorSelect_column8.setOnClickListener(this::setColorSelect)

        setColorSelect(colorSelect_column1)
    }

    private fun setColorSelect(view: View) {
        val colorOvalView = view as ColorOvalView

        colorSelect_column1.isSelected = (colorOvalView == colorSelect_column1)
        colorSelect_column2.isSelected = (colorOvalView == colorSelect_column2)
        colorSelect_column3.isSelected = (colorOvalView == colorSelect_column3)
        colorSelect_column4.isSelected = (colorOvalView == colorSelect_column4)
        colorSelect_column5.isSelected = (colorOvalView == colorSelect_column5)
        colorSelect_column6.isSelected = (colorOvalView == colorSelect_column6)
        colorSelect_column7.isSelected = (colorOvalView == colorSelect_column7)
        colorSelect_column8.isSelected = (colorOvalView == colorSelect_column8)

        onColorSelectedListener?.onColorSelected(colorOvalView.color)
    }
}