package com.kdanmobile.reader.screen.reader.mediabox.textbox

import android.content.Context
import android.graphics.PorterDuff
import android.support.constraint.ConstraintLayout
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import com.kdanmobile.reader.R
import kotlinx.android.synthetic.main.view_media_box_item_text_attr.view.*

class TextAttrView : ConstraintLayout {

    interface OnTextAttrClickListener {
        fun onBoldClick(isSelected: Boolean)

        fun onItalicClick(isSelected: Boolean)

        fun onAlignmentClick(gravity: Int)
    }
    var onTextAttrClickListener: OnTextAttrClickListener? = null

    var isTextBold = false
        set(value) {
            field = value
            when (value) {
                true -> btn_text_bold?.setColorFilter(selectedColor, PorterDuff.Mode.SRC_IN)
                false -> btn_text_bold?.clearColorFilter()
            }
        }
    var isTextItalic = false
        set(value) {
            field = value
            when (value) {
                true -> btn_text_italic?.setColorFilter(selectedColor, PorterDuff.Mode.SRC_IN)
                false -> btn_text_italic?.clearColorFilter()
            }
        }
    var alignment = Gravity.LEFT
        set(value) {
            field = value
            when (value) {
                Gravity.LEFT -> {
                    btn_text_align_left.setColorFilter(selectedColor, PorterDuff.Mode.SRC_IN)
                    btn_text_align_center.clearColorFilter()
                    btn_text_align_right.clearColorFilter()
                }
                Gravity.CENTER -> {
                    btn_text_align_left.clearColorFilter()
                    btn_text_align_center.setColorFilter(selectedColor, PorterDuff.Mode.SRC_IN)
                    btn_text_align_right.clearColorFilter()
                }
                Gravity.RIGHT -> {
                    btn_text_align_left.clearColorFilter()
                    btn_text_align_center.clearColorFilter()
                    btn_text_align_right.setColorFilter(selectedColor, PorterDuff.Mode.SRC_IN)
                }
            }
        }
    private val selectedColor = ContextCompat.getColor(context, R.color.reader_viewer_edit_tab_color_selected)

    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_media_box_item_text_attr, this)

        btn_text_bold.setOnClickListener {
            isTextBold = !isTextBold
            onTextAttrClickListener?.onBoldClick(isTextBold)
        }
        btn_text_italic.setOnClickListener {
            isTextItalic = !isTextItalic
            onTextAttrClickListener?.onItalicClick(isTextItalic)
        }

        btn_text_align_left.setOnClickListener {
            alignment = Gravity.LEFT
            onTextAttrClickListener?.onAlignmentClick(alignment)
        }
        btn_text_align_center.setOnClickListener {
            alignment = Gravity.CENTER
            onTextAttrClickListener?.onAlignmentClick(alignment)
        }
        btn_text_align_right.setOnClickListener {
            alignment = Gravity.RIGHT
            onTextAttrClickListener?.onAlignmentClick(alignment)
        }

        isTextBold = false
        isTextItalic = false
        alignment = Gravity.LEFT
    }
}