package com.example.smartlab

import android.app.Activity
import android.app.ActivityOptions
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.example.smartlab.databinding.ActivityOnBoard1Binding
import kotlin.math.*
import java.util.*

class OnBoard1Activity : AppCompatActivity(), GestureDetector.OnGestureListener {

    private lateinit var gestureDetector: GestureDetector
    lateinit var binding: ActivityOnBoard1Binding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityOnBoard1Binding.inflate(layoutInflater)
        setContentView(binding.root)

        gestureDetector = GestureDetector(this)
    }

    fun overOnBoard(view: View) {
        val intent: Intent = Intent(this, RegestrationActivity::class.java)
        val editor: SharedPreferences.Editor =
            getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
        editor.putBoolean("isFirstRun", false).commit()
        editor.apply()
        startActivity(intent)
    }

    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(
        p0: MotionEvent?,
        e0: MotionEvent,
        distanceX: Float,
        distanceY: Float
    ): Boolean {
        return false
    }

    override fun onLongPress(e: MotionEvent) {
        return
    }

    override fun onFling(
        p0: MotionEvent?,
        e1: MotionEvent,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        val diffY = p0!!.y - e1.y
        val diffX = p0!!.x - e1.x
        if (abs(diffX) > abs(diffY)) {
            if (abs(diffX) > 100 && abs(velocityX) > 100) {
                if (diffX < 0) {
                    val option: ActivityOptions = ActivityOptions.makeSceneTransitionAnimation(
                        this, binding.blueSircle, "sircleNav"
                    )
                    val editor: SharedPreferences.Editor =
                        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                    editor.putBoolean("isFirstRun", true).commit()
                    editor.apply()
                    startActivity(
                        Intent(this@OnBoard1Activity, OnBoard2Activity::class.java),
                        option.toBundle()
                    )
                }
            }
        }
        return true
    }
}