package com.zswc.creativity.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import com.zswc.creativity.R
import com.zswc.creativity.databinding.FragmentTimeLeftBinding
import com.zswc.creativity.inte.ChoiceListData
import com.zswc.creativity.view.timeview.OnWheelTimeScrollListener
import com.zswc.creativity.view.timeview.WheelTimeView
import com.zswc.creativity.view.timeview.adapter.TimeNumericWheelAdapter
import com.ysnows.base.view.BView
import com.ysnows.base.view.BaseView
import java.lang.String
import java.util.*

/**
 *Created by shihuiyun
 *on 2020/11/23
 */
class TimeLeftView(context: Context, attrs: AttributeSet) :BaseLinearLayout(context, attrs) {

    private var binding: FragmentTimeLeftBinding? = null

    private var view: BView? = null

    init {
        init(context)

    }

    private fun init(context: Context) {
        if (context is BaseView) {
            view = context as BView
        }
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.fragment_time_left, this, true)
        setDate()
    }



    var choiceTimeLeftData: ChoiceListData? =null
    fun  setCallBack(choiceData: ChoiceListData){
        this.choiceTimeLeftData=choiceData

        if(null!=choiceData){

            val c: Calendar = Calendar.getInstance()
            val curYear: Int = c.get(Calendar.YEAR)


            val time = String.format(
                    Locale.CHINA, "%04d-%02d-%02d", binding!!.year.currentItem + curYear,
                    binding!!.month.currentItem + 1, binding!!.day.currentItem + 1,
            )
            choiceData.onRestuse("1",time)

        }
    }

    fun setDate(){
        val c: Calendar = Calendar.getInstance()
        val curYear: Int = c.get(Calendar.YEAR)
        val curMonth: Int = c.get(Calendar.MONTH) + 1 //通过Calendar算出的月数要+1

        val curDate: Int = c.get(Calendar.DATE)

        initYear()
        initMonth()
        initDay(curYear, curMonth)

        binding!!.year.currentItem = 0
        binding!!.month.currentItem = curMonth - 1
        binding!!.day.currentItem = curDate - 1
        binding!!.year.visibleItems = 7
        binding!!.month.visibleItems = 7
        binding!!.day.visibleItems = 7


    }

    /**
     * 初始化年
     */
    private fun initYear() {
        val c: Calendar = Calendar.getInstance()
        val curYear: Int = c.get(Calendar.YEAR)

        val numericWheelAdapter = TimeNumericWheelAdapter(context, curYear, 2050)
        numericWheelAdapter.setLabel(" 年")
        numericWheelAdapter.textSize = 18
        numericWheelAdapter.textColor = resources.getColor(R.color.pay_off)

        binding!!.year.viewAdapter = numericWheelAdapter
        binding!!.year.isCyclic = true
        binding!!.year.addScrollingListener(object : OnWheelTimeScrollListener {
            override fun onScrollingStarted(wheel: WheelTimeView?) {

            }

            override fun onScrollingFinished(wheel: WheelTimeView?) {

                val c: Calendar = Calendar.getInstance()
                val curYear: Int = c.get(Calendar.YEAR)

                val time = String.format(
                        Locale.CHINA, "%04d-%02d-%02d", binding!!.year.currentItem + curYear,
                        binding!!.month.currentItem + 1, binding!!.day.currentItem + 1,
                )

                if(null!=choiceTimeLeftData){

                    choiceTimeLeftData!!.onRestuse("1",time)

                }

            }

        })
    }
    /**
     * 初始化月
     */
    private fun initMonth() {
        val numericWheelAdapter = TimeNumericWheelAdapter(context, 1, 12, "%02d")
        numericWheelAdapter.setLabel(" 月")
        numericWheelAdapter.textSize = 18
        numericWheelAdapter.textColor= resources.getColor(R.color.pay_off)
        binding!!.month.viewAdapter = numericWheelAdapter
        binding!!.month.isCyclic = true
        binding!!.month.addScrollingListener(object : OnWheelTimeScrollListener {
            override fun onScrollingStarted(wheel: WheelTimeView?) {

            }

            override fun onScrollingFinished(wheel: WheelTimeView?) {

                val c: Calendar = Calendar.getInstance()
                val curYear: Int = c.get(Calendar.YEAR)

                val time = String.format(
                        Locale.CHINA, "%04d-%02d-%02d", binding!!.year.currentItem + curYear,
                        binding!!.month.currentItem + 1, binding!!.day.currentItem + 1,
                )
                if(null!=choiceTimeLeftData){

                    choiceTimeLeftData!!.onRestuse("1",time)

                }


            }

        })

    }

    /**
     * 初始化天
     */
    private fun initDay(arg1: Int, arg2: Int) {
        val numericWheelAdapter = TimeNumericWheelAdapter(context, 1, getDay(arg1, arg2), "%02d")
        numericWheelAdapter.setLabel(" 日")
        numericWheelAdapter.textSize = 18
        numericWheelAdapter.textColor= resources.getColor(R.color.pay_off)
        binding!!.day.viewAdapter = numericWheelAdapter
        binding!!.day.isCyclic = true

        binding!!.day.addScrollingListener(object : OnWheelTimeScrollListener {
            override fun onScrollingStarted(wheel: WheelTimeView?) {

            }

            override fun onScrollingFinished(wheel: WheelTimeView?) {

                val c: Calendar = Calendar.getInstance()
                val curYear: Int = c.get(Calendar.YEAR)

                val time = String.format(
                        Locale.CHINA, "%04d-%02d-%02d", binding!!.year.currentItem + curYear,
                        binding!!.month.currentItem + 1, binding!!.day.currentItem + 1,
                )
                if(null!=choiceTimeLeftData){

                    choiceTimeLeftData!!.onRestuse("1",time)

                }


            }

        })

    }

    /**
     *
     * @param year
     * @param month
     * @return
     */
    private fun getDay(year: Int, month: Int): Int {
        var day = 30
        var flag = false
        flag = when (year % 4) {
            0 -> true
            else -> false
        }
        day = when (month) {
            1, 3, 5, 7, 8, 10, 12 -> 31
            2 -> if (flag) 29 else 28
            else -> 30
        }
        return day
    }



}
