package com.ym.game.adapter

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.text.Html
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.ym.xynt.R
import com.ym.library.module.WithDrawEntity2
import com.ym.game.view.MainDialog
import com.ym.library.base.BaseRecyclerAdapter
import com.ym.library.module.ExchangeEntity
import com.ym.library.module.GameDuiHuanShopEntity
import com.ym.library.net.BaseObserver
import com.ym.library.net.GameApiClient
import com.ym.library.net.RxSchedulers
import com.ym.library.rxbus.RxBusConstant
import com.ym.library.rxbus.RxBusUtil
import com.ym.library.utils.EventUtils
import com.ym.library.utils.Utils
import com.ym.modulecommon.AppliContext
import com.ym.modulecommon.base.NoDoubleClickListener

class CashExchangeAdapter :
    BaseRecyclerAdapter<GameDuiHuanShopEntity, CashExchangeAdapter.ViewHolder> {
    private var mContext: Activity? = null
    private var dialog: Dialog? = null

    constructor(
        content: Activity?,
        mDialog: Dialog
    ) : super() {
        mContext = content
        dialog = mDialog
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        if (mList.size > 0) {
            var bean = mList[position]



        }


/*        var bean = mList[position]
//        if (bean.type == 4) {
//            holder.item_mark?.visibility = View.VISIBLE
//            holder.item_mark?.setImageResource(R.mipmap.icon_fuli)
//        } else if (bean.type == 1) {
//            holder.item_mark?.visibility = View.VISIBLE
//            holder.item_mark?.setImageResource(R.mipmap.icon_xinshou)
//        } else {
//            holder.item_mark?.visibility = View.GONE
//        }
        if (holder.item_icon!=null&&bean.id!=null){
            setImage(holder.item_icon!!, bean.id)
        }
        holder.item_cash?.text = bean.cashStr
        holder.item_unit?.text = bean.unit
        holder.item_progress?.max = bean.targetNum!!
        holder.item_progress?.setProgress(bean.completeNum!!)
        holder.item_jindu?.text = "${bean.completeNum}/${bean.targetNum}"
        if (position==1&&bean.type == 1){
            if (holder.item_duihuan != null) {
                initShotAnim(holder.item_duihuan!!)
            }
        }
        holder.item_duihuan?.addClickScale()
        holder.item_duihuan?.setOnClickListener (object :NoDoubleClickListener(){
            override fun onNoDoubleClick(view: View?) {

                    duihuan(bean.id, bean.type!!, bean.cashStr)
                    if (bean.type == 1) {
                        EventUtils.onEvent("click_xinshou0.3", "新手0.3")
                    } else if (bean.type == 4) {
                        EventUtils.onEvent("click_fuli0.3", "福利0.3")
                    } else {
                        when (bean.id) {
                            1 -> {
                                EventUtils.onEvent("click_fanqie", "番茄" + bean.cash)
                            }
                            2 -> {
                                EventUtils.onEvent("click_nangua", "南瓜" + bean.cash)
                            }
                            3 -> {
                                EventUtils.onEvent("click_luobo", "萝卜" + bean.cash)
                            }
                            4 -> {
                                EventUtils.onEvent("click_yumi", "玉米" + bean.cash)
                            }
                        }
                    }

            }

        })

        if (!TextUtils.isEmpty(bean.desc)) {
            holder.item_chazhi?.text = Html.fromHtml(bean.desc)
        }*/
    }

    fun duihuan(id: Int, type: Int, jine: String) {
        GameApiClient.gameApi.dowd(id).compose(
            RxSchedulers.observableIO2Main()
        ).subscribe(object : BaseObserver<ExchangeEntity>() {
            override fun onSuccess(result: ExchangeEntity?) {
                RxBusUtil.getDefault()
                    .send(RxBusConstant.RX_WITH_DRAW_LIST)
                if (result != null) {
                    MainDialog?.showWithDraw(
                        mContext,
                        result?.status ?: 0,
                        result?.title ?: "",
                        result?.reason ?: "",
                        result?.buttonText ?: "",
                        result?.buttonStatus ?: 0,
                        id,
                        type,
                        jine,
                        false,
                        dialog!!
                    )
                }
            }

            override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {

                Utils.showToast(mContext, "" + errorMsg)
            }
        })
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(AppliContext.get())
                .inflate(R.layout.item_with_draw_list, parent, false)
        )
    }

    class ViewHolder : RecyclerView.ViewHolder {
        var item_list: RecyclerView?
         var item_duihuan: LinearLayout?
        var item_money_tv: TextView?
//        var item_progress: ProgressBar?
//        var item_chazhi: TextView?
//        var item_jindu: TextView?
//        var item_cash: TextView?
//        var item_unit: TextView?

        constructor(
            itemView: View
        ) : super(itemView) {
            item_list = itemView.findViewById<RecyclerView>(R.id.item_recycler_fruit_list)
            item_duihuan = itemView.findViewById<LinearLayout>(R.id.item_click_duihuan)
            item_money_tv = itemView.findViewById<TextView>(R.id.cash_item_money_tv)
//            item_unit = itemView.findViewById<TextView>(R.id.id_tv_unit)
//            item_icon = itemView.findViewById<ImageView>(R.id.id_img_icon)
//            //item_mark = itemView.findViewById<ImageView>(R.id.id_img_mark)
//            item_duihuan = itemView.findViewById<ImageView>(R.id.id_img_duihuan)
//            item_progress = itemView.findViewById<ProgressBar>(R.id.progess_with_draw_item)
        }
    }


    /**
     * 添加点击缩放效果
     */
    fun View.addClickScale(scale: Float = 0.9f, duration: Long = 150) {
        this.setOnTouchListener { _, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    this.animate().scaleX(scale).scaleY(scale).setDuration(duration).start()
                }
                MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                    this.animate().scaleX(1f).scaleY(1f).setDuration(duration).start()
                }
            }
            // 点击事件处理，交给View自身
            this.onTouchEvent(event)
        }
    }

    var animatorSet: AnimatorSet? = null
    private fun initShotAnim(iv: ImageView) {
        animatorSet = AnimatorSet()
        val animatorX: ObjectAnimator = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 0.8f, 1f)
        val animatorY: ObjectAnimator = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 0.8f, 1f)
        animatorX.setRepeatCount(-1)
        animatorY.setRepeatCount(-1)
        animatorSet?.play(animatorX)?.with(animatorY)
        animatorSet?.setDuration(600)?.start()
    }
//    private fun  stopAnimation(iv: ImageView){
//
//    }

}