package com.mints.goodmoney.ui.activitys

import android.view.KeyEvent
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.AnimationSet
import com.mints.goodmoney.R
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.common.DeviceInfo
import com.mints.goodmoney.ui.activitys.base.BaseActivity
import com.yilan.sdk.ui.little.YLLittleVideoFragment
import kotlinx.android.synthetic.main.activity_splash.*
import net.grandcentrix.tray.AppPreferences

/**
 * 启动页
 */
class SplashActivity : BaseActivity() {

    private val mAlph by lazy { AlphaAnimation(1f, 1f) }
    private val animationSet by lazy { AnimationSet(true) }
    private val ps by lazy { AppPreferences(context) }

    override fun isApplyKitKatTranslucency() = false

    override fun initViewsAndEvents() {
        if (!isTaskRoot) {
            //点击Home键后再点击App图标，会回到原来的界面。今天重新打包后，但是在Debug模式下，一切正常，
            // 但是把打完包的apk（Release）安装在其他手机上时，回到桌面后点击图标会重新打开启动页
            finish()
            return
        }

        // 校验APP签名
        checkAppSign()

        // 预加载好兔视频
        YLLittleVideoFragment.preloadVideo()
    }

    override fun getContentViewLayoutID() = R.layout.activity_splash

    override fun toggleIsBack2Left() = true

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


    override fun onDestroy() {
        super.onDestroy()

        if (mAlph != null) mAlph.cancel()
        if (animationSet != null) {
            animationSet.cancel()
            animationSet.setAnimationListener(null)
        }
    }


    /**
     * 校验App签名
     */
    private fun checkAppSign() {
        val deviceInfo: DeviceInfo = DeviceInfo.instance!!
        if (!deviceInfo.signInfo) {
            finish()
        } else {
            setAnimation()
        }
    }

    /**
     * 动画实现
     */
    private fun setAnimation() {
        //渐变动画
        mAlph.duration = 1000
        mAlph.fillAfter = true

        //动画集合
        animationSet.addAnimation(mAlph)
        splash_root.startAnimation(animationSet)
        animationSet.setAnimationListener(mAnimationListener)
    }

    private val mAnimationListener: Animation.AnimationListener =
            object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation) {}
                override fun onAnimationRepeat(animation: Animation) {}
                override fun onAnimationEnd(animation: Animation) {
                    readyGoOtherActivity()
                }
            }

    private fun readyGoOtherActivity() {
        val customVersion: Int = ps.getInt(Constant.CUSTOM_VERSION, 0)
        val isFirstEnter: Boolean = ps.getBoolean(Constant.ISFIRSTENTER + customVersion, true)
        if (isFirstEnter) {
            readyGoThenKill(GuideActivity::class.java)
        } else {
            readyGoThenKill(MainActivity::class.java)
        }
    }
}