package iut.android.pierrepierre

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import iut.android.pierrepierre.model.User

class MoleGridActivity : AppCompatActivity() {
    ///https://developer.android.com/topic/libraries/view-binding#kotlin

    private lateinit var gridViewMole : RecyclerView

    private lateinit var progressBar: ProgressBar

    //User
    private var user : User = User()
    private lateinit var scoreDisplay : TextView

    //sensor
    private val sensorManager by lazy { getSystemService(Context.SENSOR_SERVICE) as SensorManager }
    private lateinit var sensorEventListener: SensorEventListener

    //Countdown
    private lateinit var countDownTimer: CountDownTimer
    private lateinit var DisplaycountDownTimer: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.mole_grid)
        gridViewMole = findViewById(R.id.MoleGrid)
        progressBar = findViewById(R.id.progressBar)
        scoreDisplay = findViewById(R.id.score_display)
        DisplaycountDownTimer = findViewById(R.id.CountDown)
    }


    override fun onResume() {
        super.onResume()


        val MoleList = arrayOf( //12 taupes
            Mole(false),
            Mole(false),
            Mole(false),
            Mole(false),
            Mole(false),
            Mole(false),
            Mole(false),
            Mole(false),
            Mole(false),
            Mole(false),
            Mole(false),
            Mole(false)
        )

        val shakeSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)

        sensorManager.registerListener(object : SensorEventListener {
            override fun onSensorChanged(event: SensorEvent) {
                val x = event.values[0]
                val y = event.values[1]
                val z = event.values[2]

                // Calcul de l'accélération totale
                val acceleration = Math.sqrt((x * x + y * y + z * z).toDouble())

                // Si l'accélération est supérieure à 12, et que la progresse bar est au maximum, la progressBar est réinitialisée à 0
                if (acceleration > 12 && progressBar.progress == 100) {
                    progressBar.progress = 0
                    for (i in MoleList){
                        //FIXME pas d'actualisation de l'état des taupes sur la vue

                        if (!i.isDead()) i.changeState()
                    }
                }
            }

            override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
        }, shakeSensor, SensorManager.SENSOR_DELAY_GAME)

        // utilisation d'un objet anno
        countDownTimer = object : CountDownTimer(90000, 1000) {


            override fun onTick(millisUntilFinished: Long) {
                val min = millisUntilFinished/60000
                val timerMsg = getString(R.string.timer, min, (((millisUntilFinished/1000) - 60 * min).toString()).padStart(2, '0') )
                DisplaycountDownTimer.text = timerMsg
            }

            override fun onFinish() {
                //mettre la fin de parti ici => appel de l'interface de fin
                //TODO affichage du menu de fin de partie
                DisplaycountDownTimer.text = "STOP!"
            }
        }.start()

        gridViewMole.layoutManager = GridLayoutManager(this, 4)
        gridViewMole.adapter = MyGridAdapter(user, MoleList, progressBar, scoreDisplay)
    }

    override fun onPause() {
        super.onPause()
        sensorManager.unregisterListener(sensorEventListener)
        countDownTimer.cancel()
    }
}
