package com.sambath.sbc.screen.splash

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import android.widget.ImageView
import com.sambath.sbc.R
import com.sambath.sbc.app.App
import com.sambath.sbc.app.ShareActivity
import com.sambath.sbc.base.BaseActivity
import com.sambath.sbc.screen.login.LoginActivity

class SplashScreenActivity : BaseActivity() {
    lateinit var imageLading: ImageView
    lateinit var rotateAnimation: Animation
    val prefHelper = App.injectPrefHelper()
    private val handler by lazy {
        Handler()
    }

    private val runnable by lazy {
        Runnable {
            //imageLading.clearAnimation()

        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        imageLading = findViewById(R.id.image_loading)
        val rotate = RotateAnimation(
            0f,
            180f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f
        )
        rotate.duration = 500
        rotate.repeatCount = 5
        rotate.fillAfter = true
        rotate.startOffset = 0
        //rotate.repeatMode= Animation.INFINITE
        rotate.interpolator = LinearInterpolator()
        imageLading.startAnimation(rotate)

        rotate.setAnimationListener(object : Animation.AnimationListener{
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                    //var nextScreen: Any = LoginActivity::class.java
                    if (prefHelper.getToken().isNotEmpty()) {
                       // nextScreen = ShareActivity::class.java
                        val intent = Intent(this@SplashScreenActivity, ShareActivity::class.java)
                        startActivity(intent)
                    }else{
                        val intent = Intent(this@SplashScreenActivity, LoginActivity::class.java)
                        startActivity(intent)
                    }

                    finishAffinity()

            }

            override fun onAnimationStart(animation: Animation?) {
            }

        }
        )


    }
    override fun onDestroy() {
        //handler.removeCallbacks(runnable)
        super.onDestroy()
    }

    override fun onResume() {
        super.onResume()

        //handler.postDelayed(runnable, 800)

//        Handler().postDelayed({
//            imageLading.clearAnimation()
//
//        }, 1000)

//        val intent = Intent(this, nextScreen as Class<*>)
//        val intent = Intent(this, MainActivity::class.java)
//        Handler().postDelayed({
//            imageLading.clearAnimation()
//            startActivity(intent)
//           // finishAffinity()
//        }, 800)
//
   }
}