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

import android.app.Activity
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.annotation.RequiresApi
import com.yidianling.common.tools.ToastUtil
import com.yidianling.uikit.custom.http.response.question.QuestionsInfoBean


/**
 * @author jiucheng
 * @描述: 前置信息收集
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/12/10
 */
class CollectInfoPopupWindow(var mContext: Activity, var listener: OnSendMsgListener) :
    PopupWindow(mContext) {
    private var collectUserInformationView: QuestionInformationView
    private var isFromBack = true

    init {
        height = ViewGroup.LayoutParams.MATCH_PARENT
        width = ViewGroup.LayoutParams.MATCH_PARENT
        setBackgroundDrawable(BitmapDrawable())
        collectUserInformationView = QuestionInformationView(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(questionInfo: QuestionsInfoBean) {
        collectUserInformationView.setData(questionInfo)
    }


    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<QuestionsInfoBean>)

        fun finishPage()
    }

}