package com.mints.goodmoney.ui.activitys

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.mints.goodmoney.MintsApplication
import com.mints.goodmoney.R
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.manager.UserManager
import com.mints.goodmoney.ui.activitys.base.BaseActivity
import com.mints.goodmoney.ui.adapter.ViewPagerAdapter
import com.mints.library.utils.CommonUtils
import kotlinx.android.synthetic.main.activity_guide.*
import net.grandcentrix.tray.AppPreferences
import java.util.*

class GuideActivity : BaseActivity() {

    private val animatorSet by lazy { AnimatorSet() }
    private var mImageViews = ArrayList<ImageView>()
    private val guideImgs = intArrayOf(R.mipmap.bg_guide1, R.mipmap.bg_guide2, R.mipmap.bg_guide3, R.mipmap.bg_guide4)

    override fun getContentViewLayoutID() = R.layout.activity_guide

    override fun isApplyKitKatTranslucency() = false

    override fun initViewsAndEvents() {
        initView()
        btn_guide_start.setOnClickListener {
            readyGoMain()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        animatorSet.cancel()
        vp_guide_viewpager.setOnPageChangeListener(null)
    }

    private fun readyGoMain() {
        val instance = AppPreferences(MintsApplication.getContext())
        val customVersion: Int = instance.getInt(Constant.CUSTOM_VERSION, 0)
        instance.put(Constant.ISFIRSTENTER + customVersion, false)

        val channel = CommonUtils.getAppMetaData(MintsApplication.getContext(), "CHANNEL_NAME")
        if (TextUtils.equals(channel, Constant.CHANNEL_SHARE) && !UserManager.getInstance().userIsLogin()) {
            readyGo(LoginActivity::class.java)
        } else {
            readyGoThenKill(MainActivity::class.java)
        }
    }

    private fun initView() {
        mImageViews.clear()
        for (i in guideImgs.indices) {
            val imageView = ImageView(this)
            imageView.setBackgroundResource(guideImgs[i])
            //把imageView添加到集合中
            mImageViews.add(imageView)
        }
        val viewPagerAdapter = ViewPagerAdapter(mImageViews)
        vp_guide_viewpager.setAdapter(viewPagerAdapter)
        vp_guide_viewpager.setOnPageChangeListener(mOnPagerChangeListener)
    }

    /**
     * viewpager界面切换监听
     */
    private val mOnPagerChangeListener: OnPageChangeListener = object : OnPageChangeListener {
        //当界面切换完成的时候调用的方法
        override fun onPageSelected(position: Int) {
            pageIndicatorView.setSelection(position)
            if (position == mImageViews.size - 1) {
                val alphaAnimator = ObjectAnimator.ofFloat(btn_guide_start, "alpha", 0f, 1f)
                alphaAnimator.repeatCount = 0
                animatorSet.play(alphaAnimator)
                animatorSet.duration = 1500
                animatorSet.start()
                btn_guide_start.setVisibility(View.VISIBLE)
            } else {
                btn_guide_start.setVisibility(View.INVISIBLE)
            }
        }

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
        override fun onPageScrollStateChanged(arg0: Int) {}
    }
}