package com.zxhl.main.page.activity

import android.animation.ValueAnimator
import android.content.Intent
import android.content.res.AssetFileDescriptor
import android.media.MediaPlayer
import android.net.Uri
import android.util.Log
import android.view.KeyEvent
import android.view.View
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.NetConfig
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.main.R
import kotlinx.android.synthetic.main.activity_layout_lottery_box.*

class LotteryBoxActivity : BaseActivity() {
    private var boxid: String? = ""
    private var type: String? = ""
    private var animal: Boolean = false
    private var mMediaPlayer: MediaPlayer? = null
    override fun onClick(v: View?) {

    }

    override fun layoutID(): Int {
        return R.layout.activity_layout_lottery_box
    }

    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

    private var price: String = ""
    private var depotIds: String = ""
    override fun init() {
        id_img_zhiwen.visibility = View.VISIBLE
        mMediaPlayer = MediaPlayer()
        mMediaPlayer?.reset()
        val afd: AssetFileDescriptor = this.assets.openFd("game_bg.mp3")
        mMediaPlayer?.setDataSource(afd.fileDescriptor, afd.startOffset, afd.length)
        mMediaPlayer?.prepareAsync()
        mMediaPlayer?.setOnPreparedListener {
            mMediaPlayer?.start()

        }
        mMediaPlayer?.setOnCompletionListener {
            mMediaPlayer?.start()
        }
        animal = false
        boxid = intent?.data?.getQueryParameter("boxid") ?: ""
        type = intent?.data?.getQueryParameter("type") ?: ""
        price = intent?.data?.getQueryParameter("price") ?: ""
        depotIds = intent?.data?.getQueryParameter("depotIds") ?: ""
        id_ll_maijia_xuzhi?.setOnClickListener {
            JumpUtils.webJump("买家须知", NetConfig.H5.WEB_URL_BUYER_KNOW)
        }
        playStarAnimation()
        id_rl_choujiang?.setOnClickListener {
            if (!id_lottie_star.isAnimating) {
                id_img_zhiwen.visibility = View.GONE
                id_lottie_star?.playAnimation()
            }
        }
    }

    fun playStarAnimation() {
        if (!animal) {
            animal = true
            id_lottie_star?.visibility = View.VISIBLE
            id_lottie_star?.imageAssetsFolder = "liumangxing/images/"
            id_lottie_star?.setAnimation("liumangxing/data.json")
            id_lottie_star?.repeatCount = 0
            id_lottie_star?.pauseAnimation()
            id_lottie_star?.addAnimatorUpdateListener(object :
                ValueAnimator.AnimatorUpdateListener {
                override fun onAnimationUpdate(animation: ValueAnimator?) {
                    if (animation?.animatedFraction == 1f) {
                        Log.e("MXL", "播放星星动画结束")
                        id_lottie_star?.visibility = View.GONE
                        id_rl_choujiang?.visibility = View.GONE
                        playBoxAnimation()

                    }
                }
            })

        }

    }

    fun playBoxAnimation() {
        id_lottie_box?.visibility = View.VISIBLE
        id_lottie_box?.imageAssetsFolder = "baoxiang/images/"
        id_lottie_box?.setAnimation("baoxiang/data.json")
        id_lottie_box?.repeatCount = 0
        id_lottie_box?.playAnimation()
        id_lottie_box?.addAnimatorUpdateListener(object :
            ValueAnimator.AnimatorUpdateListener {
            override fun onAnimationUpdate(animation: ValueAnimator?) {
                //  Log.e("MXL","播放宝箱动画")
                if (animation?.animatedFraction == 1f) {
                    Log.e("MXL", "播放宝箱动画结束"+"di ${depotIds}")
                    id_lottie_box?.visibility = View.GONE
                    animal = false
                    finish()
                    JumpUtils.lotteryReslutJump(boxid, type, price, depotIds)
                }
            }
        })
    }

//    override fun onNewIntent(intent: Intent?) {
//        super.onNewIntent(intent)
//        boxid = intent?.data?.getQueryParameter("boxid") ?: ""
//        type = intent?.data?.getQueryParameter("type") ?: ""
//        price = intent?.data?.getQueryParameter("price") ?: ""
//        depotIds = intent?.data?.getQueryParameter("depotIds") ?: ""
//    }

    override fun onDestroy() {
        super.onDestroy()
        if (mMediaPlayer != null) {
            mMediaPlayer?.stop()
            mMediaPlayer?.release()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return false
    }
}