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

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

class TitleButton : ConstraintLayout {

    private var title = ""
    private var buttonText = ""

    var onTitleButtonClickListener: OnTitleButtonClickListener? = null
        set(value) {
            field = value
            btn_add?.setOnClickListener{field?.onTitleButtonClick()}
        }

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initAttr(attrs)
        initView()
    }

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
        initAttr(attrs)
        initView()
    }

    private fun initAttr(attributeSet: AttributeSet) {
        val typedArray = context.theme.obtainStyledAttributes(attributeSet, R.styleable.TitleButton, 0, 0)
        title = typedArray.getString(R.styleable.TitleButton_title) ?: ""
        buttonText = typedArray.getString(R.styleable.TitleButton_buttonText) ?: ""
    }

    private fun initView() {
        LayoutInflater.from(context).inflate(R.layout.view_viewer_edit_item_title_button, this)

        tv_title.text = title
        btn_add.text = buttonText
    }

    interface OnTitleButtonClickListener {
        fun onTitleButtonClick()
    }
}