package com.kdanmobile.reader.screen.view.edit

import android.content.Context
import android.graphics.Typeface
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.SeekBar
import com.kdanmobile.reader.R
import com.kdanmobile.reader.screen.data.TextBoxAttribute
import kotlinx.android.synthetic.main.view_viewer_edit_tab_text_box.view.*

class TextBoxTabView : ConstraintLayout {

    var onTitleButtonClickListener: TitleButton.OnTitleButtonClickListener? = null
        set(value) {
            field = value
            titleButton_textBox?.onTitleButtonClickListener = field
        }

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

        textAttrView_textBox.onTextAttrClickListener = object : TextAttrView.OnTextAttrClickListener {

            private fun getTypeface(): Int {
                return when (textAttrView_textBox.isTextBold) {
                    true -> when (textAttrView_textBox.isTextItalic) {
                        true -> Typeface.BOLD_ITALIC
                        false -> Typeface.BOLD
                    }
                    false -> when (textAttrView_textBox.isTextItalic) {
                        true -> Typeface.ITALIC
                        false -> Typeface.NORMAL
                    }
                }
            }

            override fun onBoldClick(isSelected: Boolean) {
                tv_preview.setTypeface(null, getTypeface())
            }

            override fun onItalicClick(isSelected: Boolean) {
                tv_preview.setTypeface(null, getTypeface())
            }

            override fun onAlignmentClick(gravity: Int) {
                tv_preview.gravity = gravity
            }
        }

        seekBar_textBox_textSize.onSeekBarChangeListener = object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
//                tv_preview.textSize = seekBar_textBox_textSize.currentValue.toFloat()
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
//                tv_preview.textSize = seekBar_textBox_textSize.currentValue.toFloat()
            }
        }
    }

    fun getTextBoxAttribute(): TextBoxAttribute {
        val isBold = textAttrView_textBox.isTextBold
        val isItalic = textAttrView_textBox.isTextItalic
        val textAlignment = textAttrView_textBox.alignment
        val textColor = 0xff000000.toInt()
        val textSize = seekBar_textBox_textSize.currentValue
        return TextBoxAttribute(isBold, isItalic, textAlignment, textColor, textSize)
    }
}