package com.zxhl.main.page.view

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.drawable.ColorDrawable
import android.util.Log
import android.view.*
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zxhl.cms.AppContext
import com.zxhl.cms.net.model.box.GoodList
import com.zxhl.cms.net.model.box.GoodsPoolEntity
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.RecyclerViewSpacesItemDecoration
import com.zxhl.main.R
import com.zxhl.main.page.adapter.GoodsPoolAdapter
import java.util.HashMap

class ProtectionPoolPopupWindow : PopupWindow(), ProtectionPoolContract.View {
    private var mContext: Activity? = null
    private var mPresenter: ProtectionPoolPresenter? = null
    private var view: View? = null
    private var BoxId: String? = ""
    private var adapter: GoodsPoolAdapter? = null
    private var tvallnum: TextView? = null
    private var allList: ArrayList<GoodList>? = null
    private var chuanshuoList: List<GoodList>? = null
    private var shishiList: List<GoodList>? = null
    private var xiyouList: List<GoodList>? = null
    private var putongList: List<GoodList>? = null
    fun init(mContext: Activity?, id: String?) {
        this.mContext = mContext
        BoxId = id
        if (mContext == null) {
            return
        }
        if (mContext.isFinishing) {
            return
        }
        mPresenter = ProtectionPoolPresenter(this)
        view = LayoutInflater.from(mContext).inflate(R.layout.layout_popup_protection_poll, null)

        init(view)
        initView()
        mPresenter?.getgoods(BoxId)
        showAtLocation(
                view,
                Gravity.CENTER,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        )
    }

    fun initView() {
        val rlpool = view?.findViewById<RecyclerView>(R.id.id_rl_pool)
        tvallnum = view?.findViewById<TextView>(R.id.id_tv_all_num)
        val tvall = view?.findViewById<TextView>(R.id.id_tv_all)
        val tvchuanshuo = view?.findViewById<TextView>(R.id.id_tv_chuanshuo)
        val tvshishi = view?.findViewById<TextView>(R.id.id_tv_shishi)
        val tvxiyou = view?.findViewById<TextView>(R.id.id_tv_xiyou)
        val tvputong = view?.findViewById<TextView>(R.id.id_tv_putong)
        val imgclose = view?.findViewById<ImageView>(R.id.id_img_close)
        imgclose?.setOnClickListener {
            dismiss()
        }
        tvall?.setOnClickListener {
            tvall?.setBackgroundResource(R.drawable.shape_ff2e31_r12_bg)
            tvall?.setTextColor(AppContext.get().resources.getColor(R.color.color_ff2e31))
            tvchuanshuo?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvchuanshuo?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvshishi?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvshishi?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvxiyou?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvxiyou?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvputong?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvputong?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            setAllAdapter()
        }
        tvchuanshuo?.setOnClickListener {
            tvall?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvall?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvchuanshuo?.setBackgroundResource(R.drawable.shape_ff2e31_r12_bg)
            tvchuanshuo?.setTextColor(AppContext.get().resources.getColor(R.color.color_ff2e31))
            tvshishi?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvshishi?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvxiyou?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvxiyou?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvputong?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvputong?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            setChuanshuoAdapter()
        }
        tvshishi?.setOnClickListener {
            tvall?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvall?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvchuanshuo?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvchuanshuo?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvshishi?.setBackgroundResource(R.drawable.shape_ff2e31_r12_bg)
            tvshishi?.setTextColor(AppContext.get().resources.getColor(R.color.color_ff2e31))
            tvxiyou?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvxiyou?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvputong?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvputong?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            setShishiAdapter()
        }
        tvxiyou?.setOnClickListener {
            tvall?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvall?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvchuanshuo?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvchuanshuo?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvshishi?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvshishi?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvxiyou?.setBackgroundResource(R.drawable.shape_ff2e31_r12_bg)
            tvxiyou?.setTextColor(AppContext.get().resources.getColor(R.color.color_ff2e31))
            tvputong?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvputong?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            setXiYouAdapter()
        }
        tvputong?.setOnClickListener {
            tvall?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvall?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvchuanshuo?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvchuanshuo?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvshishi?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvshishi?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvxiyou?.setBackgroundResource(R.drawable.shape_f6f6f6_r12)
            tvxiyou?.setTextColor(AppContext.get().resources.getColor(R.color.color_666666))
            tvputong?.setBackgroundResource(R.drawable.shape_ff2e31_r12_bg)
            tvputong?.setTextColor(AppContext.get().resources.getColor(R.color.color_ff2e31))
            setPutongAdapter()
        }
        val widthScreen = mContext?.windowManager?.defaultDisplay?.width
        val widthDecoration = (widthScreen!! - Utils.dip2px(15 * 2) - Utils.dip2px(160 * 2)) / 4
        val gridLayoutManager: GridLayoutManager = object : GridLayoutManager(mContext, 2) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        val stringIntegerHashMap: HashMap<String, Int> = HashMap()
        stringIntegerHashMap[RecyclerViewSpacesItemDecoration.TOP_DECORATION] =
            Utils.dip2px(7) //top间距
        stringIntegerHashMap[RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION] =
            Utils.dip2px(7) //底部间距
        stringIntegerHashMap[RecyclerViewSpacesItemDecoration.LEFT_DECORATION] =
            widthDecoration //左间距
        stringIntegerHashMap[RecyclerViewSpacesItemDecoration.RIGHT_DECORATION] =
            widthDecoration //右间距
        rlpool?.addItemDecoration(
            RecyclerViewSpacesItemDecoration(
                stringIntegerHashMap
            )
        )
        adapter = GoodsPoolAdapter(mContext)
        rlpool?.layoutManager = gridLayoutManager
        rlpool?.adapter = adapter
    }

    fun init(view: View?) {
        // 设置外部可点击
        this.isOutsideTouchable = true
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        view?.setOnTouchListener(object : View.OnTouchListener {
            @SuppressLint("ClickableViewAccessibility")
            override fun onTouch(p0: View?, event: MotionEvent?): Boolean {
                val height = view?.findViewById<RelativeLayout>(R.id.layout_root)?.getTop()
                val y = event?.getY()?.toInt()
                if (event?.getAction() === MotionEvent.ACTION_UP) {
                    if (y != null) {
                        if (height != null && y < height) {
                            dismiss()
                        }
                    }
                }
                return true
            }

        })
        /* 设置弹出窗口特征 */ // 设置视图
        this.contentView = view
        // 设置弹出窗体的宽和高
        this.height = RelativeLayout.LayoutParams.MATCH_PARENT
        this.width = RelativeLayout.LayoutParams.MATCH_PARENT
        // 设置弹出窗体可点击
        this.isFocusable = true
        // 实例化一个ColorDrawable颜色为半透明
        val dw = ColorDrawable(-0x60000000)
        // 设置弹出窗体的背景
        setBackgroundDrawable(dw)
        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.animationStyle = R.style.main_photo_anim
        isClippingEnabled = false
    }

    override fun setData(result: GoodsPoolEntity?) {
        tvallnum?.text = "共${result?.all}款(${result?.good}个传说款/史诗款/稀有款)"
        chuanshuoList = result?.list?.l3
        shishiList = result?.list?.l2
        xiyouList = result?.list?.l1
        putongList = result?.list?.l0
        allList = ArrayList()
        allList?.clear()
        if (chuanshuoList != null) {
            allList?.addAll(chuanshuoList!!)
        }
        if (shishiList != null) {
            allList?.addAll(shishiList!!)
        }
        if (xiyouList != null) {
            allList?.addAll(xiyouList!!)
        }
        if (putongList != null) {
            allList?.addAll(putongList!!)
        }
        adapter?.clear()
        adapter?.appendToList(allList)
        adapter?.notifyDataSetChanged()
    }

    fun setAllAdapter() {
        adapter?.clear()
        adapter?.appendToList(allList)
        adapter?.notifyDataSetChanged()
    }

    fun setChuanshuoAdapter() {
        adapter?.clear()
        adapter?.appendToList(chuanshuoList)
        adapter?.notifyDataSetChanged()
    }

    fun setShishiAdapter() {
        adapter?.clear()
        adapter?.appendToList(shishiList)
        adapter?.notifyDataSetChanged()
    }

    fun setXiYouAdapter() {
        adapter?.clear()
        adapter?.appendToList(xiyouList)
        adapter?.notifyDataSetChanged()
    }

    fun setPutongAdapter() {
        adapter?.clear()
        adapter?.appendToList(putongList)
        adapter?.notifyDataSetChanged()
    }
}