package com.kdanmobile.reader.screen.reader.mediabox.stamp

import android.content.Context
import android.graphics.Rect
import android.support.constraint.ConstraintLayout
import android.support.v4.view.PagerAdapter
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kdanmobile.kmpdfkit.annotation.stamp.StampConfig.STANDARD_STAMP_RES
import com.kdanmobile.kmpdfkit.annotation.stamp.TextStampConfig
import com.kdanmobile.kmpdfkit.annotation.stamp.view.KMPDFStampTextView
import com.kdanmobile.reader.R
import com.kdanmobile.reader.screen.data.StampAttribute
import com.kdanmobile.reader.screen.reader.mediabox.common.MediaBoxHeader
import kotlinx.android.synthetic.main.view_media_box_item_stamp_custom.view.*
import kotlinx.android.synthetic.main.view_media_box_item_stamp_standard.view.*
import kotlinx.android.synthetic.main.view_media_box_tab_stamp.view.*
import java.text.SimpleDateFormat
import java.util.*

class StampTabView : ConstraintLayout {

    private var selectedCustomStampIndex = 0
    private var selectedStandardStampIndex = 0

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

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

        viewPager.adapter = StampViewPagerAdapter(arrayListOf(resources.getString(R.string.reader_mediaBox_stamp_tabCustom), resources.getString(R.string.reader_mediaBox_stamp_tabStandard)))
        tabLayout.setupWithViewPager(viewPager)
    }

    fun getStampAttribute() : StampAttribute {
        return when (viewPager.currentItem == 0) {
            true -> {
                val stampType = CustomStampType.values()[selectedCustomStampIndex]
                //  not work?
                val rect = Rect(0, 0, 100, 100)
//                val timeText = "${cb_date.text}${if (cb_date.text.isNotEmpty()) " " else ""}${cb_time.text}"
                val timeText: String
                //  not work?
                val timeType = if (cb_time.isChecked) {
                    if (cb_date.isChecked) {
                        timeText = "${cb_date.text} ${cb_time.text}"
                        KMPDFStampTextView.TimeType.TIME_AND_DATE
                    } else {
                        timeText = "${cb_time.text}"
                        KMPDFStampTextView.TimeType.TIME
                    }
                } else {
                    if (cb_date.isChecked) {
                        timeText = "${cb_date.text}"
                        KMPDFStampTextView.TimeType.DATE
                    } else {
                        timeText = ""
                        KMPDFStampTextView.TimeType.NULL
                    }
                }
                StampAttribute(rect = rect, config = TextStampConfig(stampType.lineColor, stampType.backgroundColor, stampType.textColor, et_mediaBoxItemStampCustom_text.text.toString(), timeText, stampType.shape, timeType))
            }
            false -> {
                StampAttribute(STANDARD_STAMP_RES.values()[selectedStandardStampIndex])
            }
        }
    }

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

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = when (position) {
                0 -> {
                    val customStampView = layoutInflater.inflate(R.layout.view_media_box_item_stamp_custom, container, false)

                    val date = Date()
                    customStampView.cb_date.text = SimpleDateFormat("yyyy/MM/dd").format(date)
                    customStampView.cb_time.text = SimpleDateFormat("hh:mm:ss a").format(date).toLowerCase(Locale.US)

                    val recyclerView = customStampView.recyclerView_shapeCustom
                    recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                    val adapter = CustomStampAdapter(context, CustomStampType.values())
                    adapter.setOnStampClickListener(object : OnStampClickListener {
                        override fun onStampClick(index: Int) {
                            selectedCustomStampIndex = index
                        }
                    })
                    recyclerView.adapter = adapter
                    recyclerView.setHasFixedSize(true)
                    customStampView
                }
                else -> {
                    val standardStampView = layoutInflater.inflate(R.layout.view_media_box_item_stamp_standard, container, false)
                    val recyclerView = standardStampView.recyclerView_shapeStandard
                    recyclerView.layoutManager = GridLayoutManager(context, 3)
                    val adapter = StandardStampAdapter(context, STANDARD_STAMP_RES.values())
                    adapter.setOnStampClickListener(object : OnStampClickListener {
                        override fun onStampClick(index: Int) {
                            selectedStandardStampIndex = index
                        }
                    })
                    recyclerView.adapter = adapter
                    recyclerView.setHasFixedSize(true)
                    standardStampView
                }
            }
            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]
        }
    }
}