package com.kdanmobile.reader.screen.reader.mediabox.common

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_media_box_header.view.*

class MediaBoxHeader : ConstraintLayout {

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

    var onTitleButtonClickListener: OnTitleButtonClickListener? = null
        set(value) {
            field = value
            btn_mediaBoxHeader_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.MediaBoxHeader, 0, 0)
        title = typedArray.getString(R.styleable.MediaBoxHeader_title) ?: ""
        buttonText = typedArray.getString(R.styleable.MediaBoxHeader_buttonText) ?: ""
    }

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

        tv_mediaBoxHeader_title.text = title
        btn_mediaBoxHeader_add.text = buttonText
    }

    interface OnTitleButtonClickListener {
        fun onTitleButtonClick()
    }
}