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

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.SeekBar
import com.kdanmobile.reader.R
import com.kdanmobile.reader.screen.view.ColorSelectView
import kotlinx.android.synthetic.main.view_media_box_tab_text_box_style.view.*

class TextBoxStyleView : TextBoxTabView {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

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

    override fun initView() {
        LayoutInflater.from(context).inflate(R.layout.view_media_box_tab_text_box_style, 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 = progress.toFloat()
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
            }
        }

        colorChooser_textBox.onColorSelectedListener = object : ColorSelectView.OnColorSelectedListener {
            override fun onColorSelected(color: Int) {
                tv_preview.setTextColor(color)
            }
        }

        tv_preview.setTextColor(colorChooser_textBox.getSelectedColor())
    }
}