package com.yidianling.uikit.business.session.view

import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import com.yidianling.common.tools.ToastUtil
import com.yidianling.uikit.custom.http.response.UserQuestInfoBean


/**
 * @author jiucheng
 * @描述:
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/12/10
 */
class CollectInfoPopupWindow(var mContext: Context, var listener: OnSendMsgListener) :
    PopupWindow(mContext) {
    private var collectUserInformationView: CollectUserInformationView
    private var isFromBack = true

    init {
        height = ViewGroup.LayoutParams.MATCH_PARENT
        width = ViewGroup.LayoutParams.MATCH_PARENT
        setBackgroundDrawable(BitmapDrawable())
        collectUserInformationView = CollectUserInformationView(mContext, {
            listener.sendMsg("", ArrayList())
            isFromBack = false
            dismiss()
        }, {
            dismiss()
            listener.finishPage()
        }, { _content, _list ->
            listener.sendMsg(_content, _list)
            isFromBack = false
            dismiss()
        })
        collectUserInformationView.isFocusableInTouchMode = true
        collectUserInformationView.setOnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                showSkipDialog()
                true
            }
            false
        }
        contentView = collectUserInformationView
        isFocusable = true
        isTouchable = true
        isOutsideTouchable = false
    }

    fun setData(list: List<UserQuestInfoBean>, hasCompleteInfoList: List<UserQuestInfoBean>?) {
        if (list.isEmpty()) {
            ToastUtil.toastShort("数据异常，请稍后重试")
            return
        }
        collectUserInformationView.setData(list, hasCompleteInfoList)
    }


    override fun dismiss() {
        if (isFromBack) {
            showSkipDialog()
            isFromBack = false
        } else {
            super.dismiss()
        }
    }


    private fun showSkipDialog() {
        collectUserInformationView.showSkipDialog()
    }

    fun show(v: View) {
        v.post { showAtLocation(v, Gravity.BOTTOM, 0, 0) }
    }

    interface OnSendMsgListener {
        fun sendMsg(content: String, result: List<UserQuestInfoBean>)

        fun finishPage()
    }

}