package com.example.base.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.base.R
import org.jetbrains.anko.find
import org.jetbrains.anko.textColor
import java.util.Calendar

class MonthView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) {
    private var ivPreYear: ImageView? = null
    private var ivNextYear: ImageView? = null

    private val monthList by lazy { initMonthList() }
    private val adapter by lazy { MonthAdapter() }

    private val calendar by lazy { Calendar.getInstance() }
    private val startCalendar by lazy { Calendar.getInstance() }
    private val endCalendar by lazy { Calendar.getInstance() }

    private var onMonthSelectedListener: ((year: Int, month: Int) -> Unit)? = null

    init {
        initView()
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initView() {
        val view = View.inflate(context, R.layout.layout_month_view, null)
        ivPreYear = view.find<ImageView>(R.id.iv_pre_year)
        ivNextYear = view.find<ImageView>(R.id.iv_next_year)
        val rvMonth = view.find<RecyclerView>(R.id.rv_month)

        rvMonth.adapter = adapter
        adapter.setOnItemClickListener { _, _, index ->
            adapter.data.forEach {
                it.isSelected = false
            }
            adapter.data[index].isSelected = true
            calendar.set(Calendar.MONTH, adapter.data[index].index)
            onMonthSelectedListener?.invoke(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH))
            adapter.notifyDataSetChanged()
        }

        ivPreYear?.setOnClickListener {
            if (calendar.get(Calendar.YEAR) > startCalendar.get(Calendar.YEAR)) {
                calendar.add(Calendar.YEAR, -1)
                onMonthSelectedListener?.invoke(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH))
            }
            setNaviBtn()
        }
        ivNextYear?.setOnClickListener {
            if (calendar.get(Calendar.YEAR) < endCalendar.get(Calendar.YEAR)) {
                calendar.add(Calendar.YEAR, 1)
                onMonthSelectedListener?.invoke(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH))
            }
            setNaviBtn()
        }
        addView(view)
    }

    private fun initMonthList(): ArrayList<Month> {
        val list: ArrayList<Month> = ArrayList()
        for (i in 0..11) {
            list.add(Month(i, calendar.get(Calendar.MONTH) == i))
        }
        return list
    }

    class MonthAdapter : BaseQuickAdapter<Month, BaseViewHolder>(R.layout.item_month_view) {
        @SuppressLint("SetTextI18n")
        override fun convert(holder: BaseViewHolder, item: Month) {
            val tvMonth = holder.getView<TextView>(R.id.text)
            tvMonth.text = "${item.index + 1}月"
            tvMonth.textColor = if (item.isSelected) Color.WHITE else Color.BLACK
            tvMonth.background = if (item.isSelected) ContextCompat.getDrawable(context, R.drawable.shape_month_view_text_bg) else null
        }

    }

    class Month(val index: Int, var isSelected: Boolean = false)

    @SuppressLint("NotifyDataSetChanged")
    fun setDate(currentDate: Long = System.currentTimeMillis()) {
        calendar.timeInMillis = currentDate
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        adapter.setList(monthList)
        adapter.notifyDataSetChanged()
        setNaviBtn()
    }

    fun setRange(startDate: Long, endDate: Long) {
        startCalendar.timeInMillis = startDate
        startCalendar.set(Calendar.DAY_OF_MONTH, 1)

        endCalendar.timeInMillis = endDate
        endCalendar.set(Calendar.DAY_OF_MONTH, 1)
        setNaviBtn()
    }

    private fun setNaviBtn() {
        ivNextYear?.visibility = if (calendar.get(Calendar.YEAR) >= endCalendar.get(Calendar.YEAR)) View.INVISIBLE else View.VISIBLE
        ivPreYear?.visibility = if (calendar.get(Calendar.YEAR) <= startCalendar.get(Calendar.YEAR)) View.INVISIBLE else View.VISIBLE
    }

    fun setOnMonthSelectedListener(listener: ((year: Int, month: Int) -> Unit)?) {
        onMonthSelectedListener = listener
    }
}