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.FragmentTimeRightBinding
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 TimeRightView(context: Context, attrs: AttributeSet) :BaseLinearLayout(context,attrs) {

    private var binding: FragmentTimeRightBinding? = 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_right, this, true)
        setTime()
    }



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

//        if(null!=choiceData){
//
//            val time = String.format(
//                    Locale.CHINA, "%02d-%02d",  binding!!.hour.currentItem, binding!!.mins.currentItem
//            )
//            choiceData.onRestuse("2",time)
//
//        }
    }


    fun getTime(): kotlin.String {

        return  String.format(
                Locale.CHINA, "%02d:%02d:%02d",
                binding!!.hour.currentItem, binding!!.mins.currentItem,
                binding!!.second.currentItem
        )
    }

    private fun setTime() {
        val c: Calendar = Calendar.getInstance()
        val curHour: Int = c.get(Calendar.HOUR_OF_DAY)
        val curMin: Int = c.get(Calendar.MINUTE)
        val curSecond: Int = c.get(Calendar.SECOND)

        initHour()
        initMins()
        initSecond()
        // 设置当前时间
        binding!!.hour.currentItem = curHour
        binding!!.mins.currentItem = curMin
        binding!!.second.currentItem = curSecond
        binding!!.hour.visibleItems = 7
        binding!!.mins.visibleItems = 7
        binding!!.second.visibleItems = 7
    }

    /**
     * 初始化时
     */
    private fun initHour() {
        val numericWheelAdapter = TimeNumericWheelAdapter(context, 0, 23, "%02d")
        numericWheelAdapter.setLabel(" 时")
        numericWheelAdapter.textSize = 18
        numericWheelAdapter.textColor= resources.getColor(R.color.pay_off)
        binding!!.hour.viewAdapter = numericWheelAdapter
        binding!!.hour.isCyclic = true

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

            }

            override fun onScrollingFinished(wheel: WheelTimeView?) {
                val time = String.format(
                        Locale.CHINA, "%02d:%02d:%02d",
                        binding!!.hour.currentItem, binding!!.mins.currentItem,
                        binding!!.second.currentItem
                )

                if(null!=choiceTimeRightData){

                    choiceTimeRightData!!.onRestuse("2",time)

                }

            }

        })


    }
    /**
     * 初始化分
     */
    private fun initMins() {

        val numericWheelAdapter = TimeNumericWheelAdapter(context, 0, 59, "%02d")
        numericWheelAdapter.setLabel(" 分")
        numericWheelAdapter.textSize = 18
        numericWheelAdapter.textColor= resources.getColor(R.color.pay_off)
        binding!!.mins.viewAdapter = numericWheelAdapter
        binding!!.mins.isCyclic = true

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

            }

            override fun onScrollingFinished(wheel: WheelTimeView?) {
                val time = String.format(
                        Locale.CHINA, "%02d:%02d:%02d",
                        binding!!.hour.currentItem, binding!!.mins.currentItem,
                        binding!!.second.currentItem
                )
                if(null!=choiceTimeRightData){

                    choiceTimeRightData!!.onRestuse("2",time)

                }

            }

        })


    }


    /**
     * 初始化秒
     */
    private fun initSecond() {

        val numericWheelAdapter = TimeNumericWheelAdapter(context, 0, 59, "%02d")
        numericWheelAdapter.setLabel(" 秒")
        numericWheelAdapter.textSize = 18
        numericWheelAdapter.textColor= resources.getColor(R.color.pay_off)
        binding!!.second.viewAdapter = numericWheelAdapter
        binding!!.second.isCyclic = true

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

            }

            override fun onScrollingFinished(wheel: WheelTimeView?) {
                val time = String.format(
                        Locale.CHINA, "%02d:%02d:%02d",
                        binding!!.hour.currentItem, binding!!.mins.currentItem,
                        binding!!.second.currentItem
                )
                if(null!=choiceTimeRightData){

                    choiceTimeRightData!!.onRestuse("2",time)

                }

            }

        })


    }


}
