package com.ym.game.view

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.drawable.ColorDrawable
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.ym.xynt.R
import com.ym.game.adapter.DailyCashAdapter
import com.ym.game.contract.DailyCashContract
import com.ym.game.listener.IDialogViewCloseCallback
import com.ym.game.presenter.DailyCashPresenter
import com.ym.library.module.*
import com.ym.library.rxbus.RxBusConstant
import com.ym.library.rxbus.RxBusUtil
import com.ym.library.rxbus.Subscribe
import com.ym.library.rxbus.ThreadMode
import com.ym.library.utils.EventUtils
import com.ym.library.utils.LogUtils

class DailyCashWithdrawPopupWindow : PopupWindow(), DailyCashContract.View{
    private val TAG = "Popupwindow"
    private var mListener: IDialogViewCloseCallback? = null
    private var view: View? = null
    private var mContext: Activity? = null
    private var mPresenter: DailyCashPresenter? = null

    private var wateringTaskAdpater: DailyCashAdapter? = null
    var tv_tomAwardNum: TextView? = null
    fun init(mContext: Activity?, mListener: IDialogViewCloseCallback) :DailyCashWithdrawPopupWindow?{
        this.mListener = mListener
        this.mContext = mContext
        if (mContext == null) {
            return null
        }
        if (mContext.isFinishing) {
            return null
        }
        RxBusUtil.getDefault().register(this)
        mPresenter = DailyCashPresenter(this)
        view =
            LayoutInflater.from(mContext).inflate(R.layout.layout_popup_daily_cash_with_draw, null)
        EventUtils.onEvent("fuli_show", "天天提现弹窗展示")
        init(view)
        initView()
        mPresenter?.getDailyCashList()
        showAtLocation(
            view,
            Gravity.CENTER,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        return this
    }

    private fun initView() {
        val recyclerWatering =
            view?.findViewById<RecyclerView>(R.id.recycler_popup_watering)
        view?.findViewById<ImageView>(R.id.iv_popup_close)?.setOnClickListener { dismiss() }
        wateringTaskAdpater = DailyCashAdapter(mContext, 1,this)
        recyclerWatering?.adapter = wateringTaskAdpater
    }


    @SuppressLint()
    @Subscribe(code = RxBusConstant.RX_MAIN_DIALY_UPDATE, threadMode = ThreadMode.MAIN)
    fun updateList() {
        mPresenter?.getDailyCashList()
    }

    fun dissmissview(){
        dismiss()
    }

    override fun dismiss() {
        mListener?.onCloseViewSuccess(true)
        super.dismiss()
        RxBusUtil.getDefault().unregister(this)
        LogUtils.i(TAG, "dismiss")
    }

    private 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_drops_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 DailyCashResult(result: List<DialyCashDataEntity>?) {
        wateringTaskAdpater?.clear()
        wateringTaskAdpater?.appendToList(result)
        wateringTaskAdpater?.notifyDataSetChanged()

    }


}