package com.zxhl.main.page.view

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
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.RecyclerView
import com.zxhl.cms.common.Constant
import com.zxhl.cms.net.model.box.DepotEntity
import com.zxhl.cms.net.model.box.LotteryEntity
import com.zxhl.cms.utils.Utils
import com.zxhl.main.R
import com.zxhl.main.page.`interface`.PopupWidowCloseCallBack
import com.zxhl.main.page.activity.OrderDetailActivity
import com.zxhl.main.page.activity.RecoverDetailActivity
import java.io.Serializable


class RecoverPopupWindow : PopupWindow(), RecoverPopupContract.View {
    private var mContext: Activity? = null
    private var view: View? = null
    private var ids: String = ""
    private var price: String = ""
    private var type: String? = ""
    private var num: String? = ""
    private var recoverPrice: String = ""
    private var tvallnum: TextView? = null
    private var mPresenter: RecoverPopupPresenter? = null
    private var listener: PopupWidowCloseCallBack? = null
    private var intentList: List<LotteryEntity>? = ArrayList()
    private var depotList: List<DepotEntity>? = ArrayList()
    private var recovetype:String=""
    fun init(
        mContext: Activity?,
        ids: String,
        price: String,
        type: String?,
        recoverprice: String,
        num: String,
        listener: PopupWidowCloseCallBack,
        intentList: List<LotteryEntity>?,
        depotList: List<DepotEntity>?,
        recove_type:String
    ) {
        this.mContext = mContext
        this.ids = ids
        this.price = price
        this.type = type
        this.recoverPrice = recoverprice
        this.num = num
        this.listener = listener
        this.intentList = intentList
        this.depotList = depotList
        this.recovetype=recove_type
        if (mContext == null) {
            return
        }
        if (mContext.isFinishing) {
            return
        }
        Log.e(
            "MXL",
            "传过来的数值" + "ids:" + ids + "price:" + price + "type:" + type + "recoverprice:" + recoverprice + intentList?.size
        )
        mPresenter = RecoverPopupPresenter(this)
        view = LayoutInflater.from(mContext).inflate(R.layout.popup_layout_recover, null)

        init(view)
        initView()
        showAtLocation(
            view,
            Gravity.CENTER,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
    }

    fun initView() {

        val tvxiaofeijine = view?.findViewById<TextView>(R.id.id_tv_xiaofei_money)
        val tvrecoverprice = view?.findViewById<TextView>(R.id.id_tv_recover_price)
        val tvgoodsnum = view?.findViewById<TextView>(R.id.id_tv_num)

        val tvclose = view?.findViewById<TextView>(R.id.id_tv_canle)
        val tvconfigrecover = view?.findViewById<TextView>(R.id.id_tv_conifg_recover)
        val tvrecove_detail = view?.findViewById<TextView>(R.id.id_tv_recove_detail)
        val id_img_icon = view?.findViewById<ImageView>(R.id.id_img_icon)
        if(TextUtils.equals(recovetype,"1")){
            id_img_icon?.setImageResource(R.drawable.icon_mobi)
        }else{
            id_img_icon?.setImageResource(R.drawable.icon_jinbi)
        }
        tvclose?.setOnClickListener {
            dismiss()
        }
        tvconfigrecover?.setOnClickListener {

            if (!TextUtils.isEmpty(ids)) {
                mPresenter?.recoverGoods(ids,recovetype)
            }


        }
        tvrecove_detail?.setOnClickListener {
            if (type.equals(Constant.Key.ONE_LOTTERY) || type.equals(Constant.Key.FIVE_LOTTERY)) {
                val intent = Intent(mContext, RecoverDetailActivity::class.java)
                intent.putExtra(
                    "intentList",
                    intentList as Serializable
                )
                mContext?.startActivity(intent)
            } else {
                val intent = Intent(mContext, RecoverDetailActivity::class.java)
                intent.putExtra(
                    "depotList",
                    depotList as Serializable
                )
                mContext?.startActivity(intent)
            }

        }
        tvxiaofeijine?.text = "消费金额：" + price

        if (type.equals(Constant.Key.ONE_LOTTERY)) {
            tvgoodsnum?.text = "1"
            tvrecoverprice?.text = recoverPrice
        } else if (type.equals(Constant.Key.FIVE_LOTTERY)) {
            tvgoodsnum?.text = "5"
            tvrecoverprice?.text = recoverPrice.toDoubleOrNull()?.times(5)?.toInt().toString()
        } else {
            tvgoodsnum?.text = num.toString()
            tvrecoverprice?.text = recoverPrice.toDoubleOrNull()?.toInt().toString()
        }


    }

    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.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 recoverSuccess() {
        dismiss()
        if (type == Constant.Key.ONE_LOTTERY || type == Constant.Key.FIVE_LOTTERY|| type == Constant.Key.RE_PUMPING_ONE_LOTTERY || type == Constant.Key.RE_PUMPING_FIVE_LOTTERY) {
            Utils.showToast(mContext, "回收成功")
            mContext?.finish()
        } else {
            listener?.onClosePopupWindow()
        }

    }
}