package com.kdanmobile.reader.screen.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.HorizontalScrollView
import com.kdanmobile.reader.R
import kotlinx.android.synthetic.main.view_color_select.view.*

class ColorSelectView : HorizontalScrollView {

    interface OnColorSelectedListener {
        fun onColorSelected(color: Int)
    }

    var onColorSelectedListener: OnColorSelectedListener? = null

    private var selectedColor: Int = android.R.color.black

    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)
        colorSelect_column9.setOnClickListener(this::setColorSelect)
        colorSelect_column10.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)
        colorSelect_column9.isSelected = (colorOvalView == colorSelect_column9)
        colorSelect_column10.isSelected = (colorOvalView == colorSelect_column10)

        onColorSelectedListener?.onColorSelected(colorOvalView.color)
        selectedColor = colorOvalView.color
    }

    fun getSelectedColor() : Int {
        return selectedColor
    }

    fun setSelectedColor(color: Int) : Boolean {
        val columnArray = arrayOf(
                colorSelect_column1,
                colorSelect_column2,
                colorSelect_column3,
                colorSelect_column4,
                colorSelect_column5,
                colorSelect_column6,
                colorSelect_column7,
                colorSelect_column8,
                colorSelect_column9,
                colorSelect_column10
        )
        for (column in columnArray) {
            if (column.color == color) {
                setColorSelect(column)
                return true
            }
        }
        return false
    }

    fun getColor(index: Int) : Int {
        return when (index) {
            0 -> colorSelect_column1.color
            1 -> colorSelect_column2.color
            2 -> colorSelect_column3.color
            3 -> colorSelect_column4.color
            4 -> colorSelect_column5.color
            5 -> colorSelect_column6.color
            6 -> colorSelect_column7.color
            7 -> colorSelect_column8.color
            8 -> colorSelect_column9.color
            else -> colorSelect_column10.color
        }
    }
}