package com.example.mysmartlab

import android.app.ActivityOptions
import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import com.example.mysmartlab.databinding.ActivitySpam2Binding
import kotlin.math.*

class ActivitySpam2 : AppCompatActivity(), GestureDetector.OnGestureListener {

    private lateinit var gestureDetector: GestureDetector
    lateinit var binding: ActivitySpam2Binding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySpam2Binding.inflate(layoutInflater)
        setContentView(binding.root)
binding.btnSkip1.setOnClickListener{
    startActivity(Intent(this, ActivitySpam3::class.java))
}
        gestureDetector = GestureDetector(this)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return if (gestureDetector.onTouchEvent(event!!)) {
            true
        } else {
            return super.onTouchEvent(event)
        }
    }

    override fun onDown(e: MotionEvent): Boolean {
        return false
    }

    override fun onShowPress(e: MotionEvent) {
    }

    override fun onSingleTapUp(e: MotionEvent): Boolean {
        return false
    }

    override fun onScroll(
        e1: MotionEvent?,
        e2: MotionEvent,
        distanceX: Float,
        distanceY: Float
    ): Boolean {
        return false
    }


    override fun onLongPress(e: MotionEvent) {
        return
    }

    override fun onFling(
        e1: MotionEvent?,
        e2: MotionEvent,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        val diffY = e2?.y?.minus(e1?.y ?: 0f) ?: 0f
        val diffX = e2?.x?.minus(e1?.x ?: 0f) ?: 0f
        if (abs(diffX) > abs(diffY)) {
            if (abs(diffX) > 100 && abs(velocityX) > 100) {
                if (diffX < 0) {
                    val option: ActivityOptions = ActivityOptions.makeSceneTransitionAnimation(
                        this, binding.blueKrug, "krugNav"
                    )
                    val editor: SharedPreferences.Editor =
                        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                    editor.putBoolean("isFirstRun", true).commit()
                    editor.apply()
                    startActivity(
                        Intent(this@ActivitySpam2, ActivitySpam3::class.java),
                        option.toBundle()
                    )
                }
                else{
                    startActivity(Intent(this@ActivitySpam2, ActivitySpam1::class.java))
                }
            }
        }
        return true
    }
}
