package com.yidian.shenghuoquan.newscontent.ui.popwindow

import android.app.Activity
import android.graphics.drawable.ColorDrawable
import android.view.*
import android.widget.PopupWindow
import com.yidian.common.utils.YdToastUtil
import com.yidian.nightmode.widget.YdLinearLayout
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.bean.FeedbackBean


class FeedbackPopupWindow(private val activity: Activity, private val parent: View, private val feedbackBean: FeedbackBean) {
    private val view: View = LayoutInflater.from(activity).inflate(R.layout.feedback_popup_window, null)
    private val llyUninterested = view.findViewById<YdLinearLayout>(R.id.lly_uninterested)
    private val llyShield = view.findViewById<YdLinearLayout>(R.id.lly_shield)
    private val llyInferior = view.findViewById<YdLinearLayout>(R.id.lly_inferior)
    private val llyBlock = view.findViewById<YdLinearLayout>(R.id.lly_block)
    private val mPopUpWindow = PopupWindow(view)

    init {
        mPopUpWindow.isFocusable = false
        mPopUpWindow.isTouchable = true
        mPopUpWindow.isOutsideTouchable = true
        setClickListener()
    }

    private fun setClickListener(){
        mPopUpWindow.setOnDismissListener {
            val lp: WindowManager.LayoutParams = activity.window.attributes
            lp.alpha = 1.0f
            activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            activity.window.attributes = lp
        }

        llyUninterested.setOnClickListener {
            val imgUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.90sjimg.com%2Fdesign%2F01%2F40%2F17%2F07%2F58f7227f997dc.png&refer=http%3A%2F%2Fpic.90sjimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1614927945&t=8957cd0434852595d03de955b704416e"
            YdToastUtil.showToast(activity, "将为您减少此类内容推荐", imgUrl)
            dismiss()
        }

        llyShield.setOnClickListener {
            dismiss()
            val feedbackDetailPopupWindow = FeedbackDetailPopupWindow(activity, parent, feedbackBean, FeedbackDetailPopupWindow.SHIELD)
            feedbackDetailPopupWindow.show()
        }

        llyInferior.setOnClickListener {
            dismiss()
            val feedbackDetailPopupWindow = FeedbackDetailPopupWindow(activity, parent, feedbackBean, FeedbackDetailPopupWindow.FEEDBACK)
            feedbackDetailPopupWindow.show()
        }

        llyBlock.setOnClickListener {
            YdToastUtil.showToast(activity, "将为您减少此类内容推荐")
            dismiss()
        }
    }

    fun show(){
        if (activity.isFinishing) {
            return
        }
        mPopUpWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
        mPopUpWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT
        mPopUpWindow.setBackgroundDrawable(ColorDrawable(0x00000000))
        mPopUpWindow.isOutsideTouchable = true
        mPopUpWindow.isFocusable = true
        mPopUpWindow.animationStyle = R.style.PopupWindow_anim_style
        mPopUpWindow.showAtLocation(parent, Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 0)
        val lp: WindowManager.LayoutParams = activity.window.attributes
        lp.alpha = 0.6f
        activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        activity.window.attributes = lp
    }

    fun dismiss() {
        if (mPopUpWindow.isShowing) {
            mPopUpWindow.dismiss()
        }
    }
}