package com.zswc.creativity.utils

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.FragmentManager
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.zswc.creativity.R
import com.zswc.creativity.inte.ChoiceData
import com.zswc.creativity.inte.ChoiceListData
import com.zswc.creativity.widget.TimeLeftView
import com.zswc.creativity.widget.TimeRightView
import java.text.SimpleDateFormat
import java.util.*

/**
 * 选择时间
 *Created by shihuiyun
 *on 2020/11/21
 */
object PopuChoseTime {




    fun show(context: Context, view: View, fm: FragmentManager, choiceCallData: ChoiceData) {
        var time1 = ""
        var time2 = ""

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.item_chose_time, null)
        val  ll:LinearLayout = layout.findViewById(R.id.ll)

        val llData: LinearLayout = layout.findViewById(R.id.ll_data)
        val tvData: TextView = layout.findViewById(R.id.tv_data)
        val lineData: View = layout.findViewById(R.id.line_data)
        val llTime: LinearLayout = layout.findViewById(R.id.ll_time)
        val tvTime: TextView = layout.findViewById(R.id.tv_time)
        val lineTime: View = layout.findViewById(R.id.line_time)
        val tvSure: TextView = layout.findViewById(R.id.tv_sure)

        val timeLeft: TimeLeftView = layout.findViewById(R.id.time_left)
        val timeRight: TimeRightView = layout.findViewById(R.id.time_right)

        time1 = ""
        time2 = ""


        timeLeft.setCallBack(object : ChoiceListData {
            override fun onRestuse(code: String?, name: String?) {

                time1 = name!!
                tvData.text = time1


            }
        })
        timeRight.setCallBack(object : ChoiceListData {
            override fun onRestuse(code: String?, name: String?) {

                time2 = name!!
                tvTime.text = time2


            }
        })

        tvSure.onClick {

            if(TextUtils.isEmpty(time1)){
                Toast.makeText(context, "请选择年月日", Toast.LENGTH_SHORT).show()
                return@onClick
            }
            var time=""
            time = if (!TextUtils.isEmpty(time2)) {
                "$time1 $time2"
            } else {
                time1
            }

            //可根据需要自行截取数据显示
            //24小时制 日期格式必须一致才可
            var df = if (!TextUtils.isEmpty(time2)) {
                SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            }else{
                SimpleDateFormat("yyyy-MM-dd")
            }

            val calendar: Calendar = Calendar.getInstance()
            val endTime: String = df.format(calendar.time)

            val i: Int = time.compareTo(endTime)
            if(i<0){
                Toast.makeText(context, "选择的时间不能小于当前时间！", Toast.LENGTH_SHORT).show()
                return@onClick

            }
            Log.e("-shy-", "time= $time")
            if(null!=choiceCallData){
                Log.e("-shy-", "1111 ")
                choiceCallData!!.onRestuse(time)
            }



            mNormalPopup?.dismiss()
        }


        llData.onClick {
            lineData.visibility = View.VISIBLE
            lineTime.visibility = View.INVISIBLE
            timeLeft.visibility = View.VISIBLE
            timeRight.visibility = View.GONE
        }
        llTime.onClick {
            lineTime.visibility = View.VISIBLE
            lineData.visibility = View.INVISIBLE
            timeRight.visibility = View.VISIBLE
            timeLeft.visibility = View.GONE

            if(TextUtils.isEmpty(time2)){
                time2 = timeRight.getTime()
                tvTime.text = time2
            }
        }

        ll.onClick {

            mNormalPopup?.dismiss()
        }

        builder.release();

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                height)
        frameLayout.addView(layout, lp)

        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_AUTO)
        mNormalPopup.show(view)


    }


}
