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

import android.app.Activity
import android.graphics.drawable.ColorDrawable
import android.view.*
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import com.yidian.common.cardutils.adapter.OnItemClickListener
import com.yidian.common.widget.YdMaxRecyclerView
import com.yidian.nightmode.widget.YdImageView
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.adapter.FeedbackDetailAdapter
import com.yidian.shenghuoquan.newscontent.bean.FeedbackBean
import com.yidian.utils.ToastUtil


class FeedbackDetailPopupWindow(private val activity: Activity, private val parent: View, private val feedbackBean: FeedbackBean, private val feedbackType: String) {
    companion object{
        const val SHIELD = "shield"
        const val FEEDBACK = "feedback"
    }

    private val view: View = LayoutInflater.from(activity).inflate(R.layout.feedback_detail_popup_window, null)
    private val rvContent = view.findViewById<YdMaxRecyclerView>(R.id.rv_content)
    private val ivBack = view.findViewById<YdImageView>(R.id.iv_back)
    private val mPopUpWindow = PopupWindow(view)
    private val adapter: FeedbackDetailAdapter by lazy { FeedbackDetailAdapter() }

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

    private fun initView(){
        rvContent.layoutManager = LinearLayoutManager(activity)
        rvContent.adapter = adapter
        when(feedbackType){
            SHIELD -> adapter.update(feedbackBean.tag)
            FEEDBACK -> adapter.update(feedbackBean.reason)
        }
    }

    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
        }

        ivBack.setOnClickListener {
            dismiss()
            val feedbackPopupWindow = FeedbackPopupWindow(activity, parent, feedbackBean)
            feedbackPopupWindow.show()
        }

        adapter.setOnItemClickListener(object : OnItemClickListener<String> {
            override fun onClick(view: View?, position: Int, item: String?) {
                item?.let {
                    ToastUtil.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()
        }
    }
}