package com.zswc.creativity.utils

import android.content.Context
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.qmuiteam.qmui.skin.QMUISkinManager
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.inte.ChoseData
import com.zswc.creativity.model.InsurancePlan
import com.zswc.creativity.model.SelectData
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

/**
 *Created by shihuiyun
 *on 2020/9/14
 */
object PopuListUtils {


    fun showQMUIPopup(context: Context, view: View, data: ArrayList<String>, choseData: ChoiceData) {
        var mNormalPopup: QMUIPopup? = null
        val adapter: ArrayAdapter<*> = ArrayAdapter<Any?>(context, R.layout.simple_list_item, data as List<String>)
        val onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, i, l ->

            choseData?.onRestuse(data[i])

            mNormalPopup?.dismiss()
        }
        mNormalPopup = QMUIPopups.listPopup(context,
                QMUIDisplayHelper.dp2px(context, 250),
                QMUIDisplayHelper.dp2px(context, 300),
                adapter,
                onItemClickListener)
                .animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
                .preferredDirection(QMUIPopup.DIRECTION_TOP)
                .shadow(true)
                .offsetYIfTop(QMUIDisplayHelper.dp2px(context, 5))
                .skinManager(QMUISkinManager.defaultInstance(context))
                .onDismiss {

                }
                .show(view)
    }


    fun showQMUIPopup(context: Context, view: View, list: ArrayList<SelectData>, choseData: ChoiceListData) {
        var mNormalPopup: QMUIPopup? = null

        var data = ArrayList<String>()
        for (item in list) {
            data.add(item.name)
        }
        val adapter: ArrayAdapter<*> = ArrayAdapter<Any?>(context, R.layout.simple_list_item, data as List<String>)
        val onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, i, l ->

            choseData?.onRestuse(list[i].code, list[i].name)

            mNormalPopup?.dismiss()
        }
        mNormalPopup = QMUIPopups.listPopup(context,
                QMUIDisplayHelper.dp2px(context, 250),
                QMUIDisplayHelper.dp2px(context, 300),
                adapter,
                onItemClickListener)
                .animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
                .preferredDirection(QMUIPopup.DIRECTION_TOP)
                .shadow(true)
                .offsetYIfTop(QMUIDisplayHelper.dp2px(context, 5))
                .skinManager(QMUISkinManager.defaultInstance(context))
                .onDismiss {

                }
                .show(view)
    }



    fun showQMUIPopup(context: Context, view: View, list: List<InsurancePlan>, choseData: ChoseData) {
        var mNormalPopup: QMUIPopup? = null

        var data = ArrayList<String>()
        for (item in list) {
            data.add(item.schemeName)
        }
        val adapter: ArrayAdapter<*> = ArrayAdapter<Any?>(context, R.layout.simple_list_item, data as List<String>)
        val onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, i, l ->

            choseData?.onRestuse(list[i])

            mNormalPopup?.dismiss()
        }
        mNormalPopup = QMUIPopups.listPopup(context,
                QMUIDisplayHelper.dp2px(context, 250),
                QMUIDisplayHelper.dp2px(context, 300),
                adapter,
                onItemClickListener)
                .animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
                .preferredDirection(QMUIPopup.DIRECTION_TOP)
                .shadow(true)
                .offsetYIfTop(QMUIDisplayHelper.dp2px(context, 5))
                .skinManager(QMUISkinManager.defaultInstance(context))
                .onDismiss {

                }
                .show(view)
    }




    fun setChoseTime(context: Context?, choiceData: ChoiceData?) {
        val startDate = Calendar.getInstance()
        startDate[1980, 0] = 23
        val endDate = Calendar.getInstance()

        endDate[2050, 12] = 31

        val timePickerView = TimePickerBuilder(context, OnTimeSelectListener { date, v ->

            choiceData?.onRestuse(getTime(date))

        }).setRangDate(startDate, endDate)
                .setDate(endDate)
                .setType(booleanArrayOf(true, true, true, false, false, false))
                .build()
        timePickerView.setDate(Calendar.getInstance())
        timePickerView.show()
    }

    fun getTime(date: Date?): String? { //可根据需要自行截取数据显示
        val format = SimpleDateFormat("yyyy-MM-dd")
        return format.format(date)
    }


}
