package com.quansu.heifengwuliu.utils

import android.content.Context
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
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.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.inte.ChoiceData
import com.quansu.heifengwuliu.inte.ChoiceListData
import com.quansu.heifengwuliu.model.SelectData

/**
 *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)
    }


}
