package com.kdanmobile.reader.screen.view

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageButton
import com.kdanmobile.reader.R
import com.kdanmobile.reader.screen.view.edit.ShapeTabView
import com.kdanmobile.reader.screen.view.edit.StampTabView
import com.kdanmobile.reader.screen.view.edit.TextBoxTabView
import com.kdanmobile.reader.screen.view.edit.TitleButton
import kotlinx.android.synthetic.main.view_viewer_edit.view.*
import kotlinx.android.synthetic.main.view_viewer_edit_tab.view.*

class ViewerEditView : ConstraintLayout {

    private enum class ViewerEditTabType {
        NONE, TEXT_BOX, SIGNATURE, STAMP, SHAPE, FORM
    }

    private var tabType = ViewerEditTabType.NONE
    private var selected: ImageButton? = null
    var onViewerEditTabAddButtonClickListener: OnViewerEditTabAddButtonClickListener? = null

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

        setupTabView()
    }

    private fun setupTabView() {
        viewerEdit_tab.onClickListenerTabTextBox = OnClickListener(this::setupTextBoxView)
        viewerEdit_tab.onClickListenerTabSignature = OnClickListener(this::setupSignatureView)
        viewerEdit_tab.onClickListenerTabStamp = OnClickListener(this::setupStampView)
        viewerEdit_tab.onClickListenerTabShape = OnClickListener(this::setupShapeView)
        viewerEdit_tab.onClickListenerTabForm = OnClickListener(this::setupFormView)

        setupTextBoxView(viewerEdit_tab_textBox)
    }

    private fun getButtonDrawable(tabType: ViewerEditTabType, isSelected: Boolean): Int {
        return when (tabType) {
            ViewerEditTabType.NONE, ViewerEditTabType.TEXT_BOX ->
                if (isSelected) R.drawable.ic_textbox_selected else R.drawable.ic_textbox
            ViewerEditTabType.SIGNATURE ->
                if (isSelected) R.drawable.ic_signature_selected else R.drawable.ic_signature
            ViewerEditTabType.STAMP ->
                if (isSelected) R.drawable.ic_stamp_selected else R.drawable.ic_stamp
            ViewerEditTabType.SHAPE ->
                if (isSelected) R.drawable.ic_shape_selected else R.drawable.ic_shape
            ViewerEditTabType.FORM ->
                if (isSelected) R.drawable.ic_form_selected else R.drawable.ic_form
        }
    }

    private fun setupContent(button: ImageButton, tabType: ViewerEditTabType): Boolean {
        var changed = false
        if (this.tabType != tabType) {
            selected?.setImageDrawable(ContextCompat.getDrawable(context, getButtonDrawable(this.tabType, false)))
            this.tabType = tabType
            selected = button
            button.setImageDrawable(ContextCompat.getDrawable(context, getButtonDrawable(this.tabType, true)))

            viewEdit_layout_tab_content.removeAllViews()
            changed = true
        }
        return changed
    }

    private fun setupTextBoxView(view: View) {
        val changed = setupContent(view as ImageButton, ViewerEditTabType.TEXT_BOX)
        if (changed) {
            val textBoxTabView = TextBoxTabView(context)
            val params = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
            viewEdit_layout_tab_content.addView(textBoxTabView, params)
            textBoxTabView.onTitleButtonClickListener = object: TitleButton.OnTitleButtonClickListener {
                override fun onTitleButtonClick() {
                    onViewerEditTabAddButtonClickListener?.onTabTextBoxAddButtonClick(textBoxTabView)
                }
            }
        }
    }

    private fun setupSignatureView(view: View) {
        //TODO implements tab view and add to viewEdit_layout_tab_content
        val changed = setupContent(view as ImageButton, ViewerEditTabType.SIGNATURE)
        if (changed) {
            LayoutInflater.from(context).inflate(R.layout.view_viewer_edit_tab, viewEdit_layout_tab_content)
        }
    }

    private fun setupStampView(view: View) {
        val changed = setupContent(view as ImageButton, ViewerEditTabType.STAMP)
        if (changed) {
            val stampTabView = StampTabView(context)
            val params = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
            viewEdit_layout_tab_content.addView(stampTabView, params)
            stampTabView.onTitleButtonClickListener = object: TitleButton.OnTitleButtonClickListener {
                override fun onTitleButtonClick() {
                    onViewerEditTabAddButtonClickListener?.onTabStampAddButtonClick(stampTabView)
                }
            }
        }
    }

    private fun setupShapeView(view: View) {
        val changed = setupContent(view as ImageButton, ViewerEditTabType.SHAPE)
        if (changed) {
            val shapeTabView = ShapeTabView(context)
            val params = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
            viewEdit_layout_tab_content.addView(shapeTabView, params)
            shapeTabView.onTitleButtonClickListener = object: TitleButton.OnTitleButtonClickListener {
                override fun onTitleButtonClick() {
                    onViewerEditTabAddButtonClickListener?.onTabShapeAddButtonClick(shapeTabView)
                }
            }
        }
    }

    private fun setupFormView(view: View) {
        //TODO implements tab view and add to viewEdit_layout_tab_content
        val changed = setupContent(view as ImageButton, ViewerEditTabType.FORM)
        if (changed) {
            LayoutInflater.from(context).inflate(R.layout.view_viewer_edit_tab, viewEdit_layout_tab_content)
        }
    }

    interface OnViewerEditTabAddButtonClickListener {
        fun onTabTextBoxAddButtonClick(textBoxTabView: TextBoxTabView)
        fun onTabStampAddButtonClick(stampTabView: StampTabView)
        fun onTabShapeAddButtonClick(shapeTabView: ShapeTabView)
    }
}