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 androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ym.library.listener.OnRecycleItemClickListener
import com.ym.game.adapter.SpeedWaterTaskAdapter
import com.ym.game.contract.SpeedWaterContract
import com.ym.game.listener.IDialogViewCloseCallback
import com.ym.game.presenter.SpeedWaterPresenter
import com.ym.library.module.NewsEntity
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.xynt.R
import com.ym.library.listener.IAdVideoListener
import com.ym.library.module.TaskListEntity
import com.ym.library.module.WateringTaskEntity
import com.ym.library.utils.*

class SpeedWaterPopupWindow : PopupWindow(), SpeedWaterContract.View,
    OnRecycleItemClickListener<NewsEntity> {
    private var mListener: IDialogViewCloseCallback? = null
    private var view: View? = null
    private var mContext: Activity? = null
    private var mPresenter: SpeedWaterPresenter? = null

    private var speedadapter: SpeedWaterTaskAdapter? = null

    fun init(mContext: Activity?, mListener: IDialogViewCloseCallback) {
        this.mListener = mListener
        this.mContext = mContext
        if (mContext == null) {
            return
        }
        if (mContext.isFinishing) {
            return
        }
        RxBusUtil.getDefault().register(this)
        mPresenter = SpeedWaterPresenter(this)
        view = LayoutInflater.from(mContext).inflate(R.layout.layout_popup_speedwater, null)
        EventUtils.onEvent("show_jiasu_task_dialog", "领加速剂任务")
        init(view)
        initView()
        mPresenter?.getAcceleratorsTask()
        showAtLocation(
            view,
            Gravity.CENTER,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
    }

    private fun initView() {
        val recyclerWatering = view?.findViewById<RecyclerView>(R.id.rl_task_speed_water)
        view?.findViewById<ImageView>(R.id.iv_popup_close)?.setOnClickListener { dismiss() }
        recyclerWatering?.layoutManager = LinearLayoutManager(mContext)
        speedadapter = SpeedWaterTaskAdapter(mContext, 1, this)
        recyclerWatering?.adapter = speedadapter
    }


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

    override fun onItemClick(view: View, position: Int, data: NewsEntity) {
        // 0未完成 1已领取 2可领取
        when (data.status) {
            0 -> {

                // 跳转页面 1转盘 2高额水滴任务 3首页 4激励视频 直客任务链接"
                // if()
                //  appmodel_id
                // 应用类模块跳转 跳转到幸运农田首页 65
                //应用类模块跳转到幸运农田转盘抽奖 63
                //应用类模块跳转到幸运农田开宝箱 64
                //应用类模块跳转到幸运农田福利中心 66
                if (data.opentype == 7) {
                    if (data.appmodel_id == 65) {
                        dismiss()
                    } else if (data.appmodel_id == 63) {
                        dismiss()
                        TurntableDialog.showTurntable(mContext, mListener!!)
                    } else if (data.appmodel_id == 64) {
                        dismiss()
                    } else if (data.appmodel_id == 66) {
                        dismiss()
                        JumpUtils.jumpWelfareActivity()
                    }
                } else if (data.opentype == 1) {
                    if (data.sid != null) {
                        // EventUtils.onEvent("welfare_task_undone_click", "${data.sid}")
                        showAd(data.sid)
                        EventUtils.onEvent("jiasu_undone_click_total")
                    } else {
                        JumpUtils.adJump(data, mContext)
                    }

                } else if (data.opentype == 2) {
                    //直客链接
                    JumpUtils.adJump(data, (mContext as Activity))
                }

//                when (data.opentype) {
//
//                    1 -> {
//
//                    }
//                    2 -> {
//                        dismiss()
//                        // WateringTaskPopupwindow().init(mContext)
//                    }
//                    3 -> dismiss()
//                    6 -> {
//
////                        JumpUtils.h5Jump(
////                            "每日福利",
////                            Constant.Param.WelfareCenter,
////                            false,
////                            mContext as Activity
////                        )
//                    }
//                    4 -> {
//                        EventUtils.onEvent("click_receive_coin_task_video_btn", "领金币任务视频")
//
//                        AdUtils.playRewardAd((mContext as Activity),"receive_coin_task",object : IAdVideoListener {
//                            override fun onAdClose() {
//
//                            }
//
//                            override fun onError(errorMsg: String?) {
//
//                            }
//                        })
//                    }
//
//                }
            }
        }
    }

    fun showAd(sId: Int) {
        AdUtils.playRewardAd(mContext as Activity, "speed_water_task", object : IAdVideoListener {
            override fun onAdClose() {
                mPresenter?.getTaskComplete(sId)

            }

            override fun onError(errorMsg: String?) {

            }
        })
    }

    override fun dismiss() {
        mListener?.onCloseViewSuccess(true)
        super.dismiss()
        RxBusUtil.getDefault().unregister(this)
    }

    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 AcceleratorsTaskResult(result: List<WateringTaskEntity>) {
//        speedadapter?.clear()
//        speedadapter?.appendToList(result)
//        speedadapter?.notifyDataSetChanged()
    }

    override fun taskListResult(result: TaskListEntity?) {
        speedadapter?.clear()
        speedadapter?.appendToList(result?.userTask)
        speedadapter?.notifyDataSetChanged()

    }
}