package com.kdanmobile.reader.screen.reader.mediabox.shape

import android.content.Context
import android.graphics.Color
import android.graphics.PorterDuff
import android.support.constraint.ConstraintLayout
import android.support.v4.view.PagerAdapter
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kdanmobile.kmpdfkit.globaldata.AnnotConfig
import com.kdanmobile.reader.R
import com.kdanmobile.reader.screen.data.ShapeAttribute
import com.kdanmobile.reader.screen.reader.mediabox.common.MediaBoxHeader
import com.kdanmobile.reader.screen.reader.mediabox.common.MediaBoxSeekBar
import kotlinx.android.synthetic.main.view_media_box_item_shape_border.view.*
import kotlinx.android.synthetic.main.view_media_box_item_shape_fill.view.*
import kotlinx.android.synthetic.main.view_media_box_tab_shape.view.*

class ShapeTabView : ConstraintLayout {

    var onMediaBoxHeaderClickListener: MediaBoxHeader.OnTitleButtonClickListener? = null
        set(value) {
            field = value
            titleButton_shape?.onTitleButtonClickListener = field
        }

    var shapeType = AnnotConfig.ShapeAnnotationType.CIRCLE
        set(value) {
            field = value

            btn_circle?.clearColorFilter()
            btn_square?.clearColorFilter()
            btn_line?.clearColorFilter()
            btn_arrow?.clearColorFilter()
            when (shapeType) {
                AnnotConfig.ShapeAnnotationType.CIRCLE -> {
                    btn_circle?.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN)
                }
                AnnotConfig.ShapeAnnotationType.SQUARE -> {
                    btn_square?.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN)
                }
                AnnotConfig.ShapeAnnotationType.LINE -> {
                    btn_line?.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN)
                }
                AnnotConfig.ShapeAnnotationType.ARROW -> {
                    btn_arrow?.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN)
                }
            }
        }

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

        btn_circle.setOnClickListener { _ ->
            shapeType = AnnotConfig.ShapeAnnotationType.CIRCLE
        }

        btn_square.setOnClickListener { _ ->
            shapeType = AnnotConfig.ShapeAnnotationType.SQUARE
        }

        btn_line.setOnClickListener { _ ->
            shapeType = AnnotConfig.ShapeAnnotationType.LINE
        }

        btn_arrow.setOnClickListener { _ ->
            shapeType = AnnotConfig.ShapeAnnotationType.ARROW
        }
        shapeType = AnnotConfig.ShapeAnnotationType.CIRCLE

        viewPager.adapter = ViewPagerAdapter(context, arrayListOf(resources.getString(R.string.reader_mediaBox_shape_tabBorder), resources.getString(R.string.reader_mediaBox_shape_tabFill)))
        tabLayout.setupWithViewPager(viewPager)
    }

    fun getShapeAttribute() : ShapeAttribute {
        val lineColor = colorChooser_shapeBorder.getSelectedColor()
        val lineWidth = seekBar_shapeBorder_thickness.currentValue.toFloat()
        val lineAlpha = seekBar_shapeBorder_opacity.currentValue
        val fillColor = colorChooser_shapeFill.getSelectedColor()
        val fillAlpha = seekBar_shapeFill_opacity.currentValue
        return ShapeAttribute(shapeType, lineColor, lineWidth, lineAlpha, fillColor, fillAlpha)
    }

    private inner class ViewPagerAdapter(context: Context, private val tabs: ArrayList<String>) : PagerAdapter() {
        private val layoutInflater = LayoutInflater.from(context)

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val resourceId = when (position == 0) {
                true -> R.layout.view_media_box_item_shape_border
                false -> R.layout.view_media_box_item_shape_fill
            }
            val view = layoutInflater.inflate(resourceId, container, false)
            if (position == 0) {
                view.seekBar_shapeBorder_opacity.formatter = object : MediaBoxSeekBar.SeekBarFormatter {
                    override fun format(value: Int): String {
                        return Math.round(value / 2.55).toInt().toString()
                    }
                }
            } else {
                view.seekBar_shapeFill_opacity.formatter = object : MediaBoxSeekBar.SeekBarFormatter {
                    override fun format(value: Int): String {
                        return Math.round(value / 2.55).toInt().toString()
                    }
                }
            }
            container.addView(view)
            return view
        }

        override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
            container.removeView(obj as View)
        }

        override fun getCount(): Int {
            return tabs.size
        }

        override fun isViewFromObject(view: View, obj: Any): Boolean {
            return view == obj
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return tabs[position]
        }
    }
}