package com.example.stupidgame

import android.content.Intent
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class CheatActivity : ComponentActivity() {

    companion object {
        /*
        In Java, do you remember the difference between static items and non-static items?
        With non-static items every object has their own copy of the item.  With static items,
        there is only one copy of the item (data field) and all objects of that class share it.  (If you
        change it in one object, it's changed in all of them.)  You can even access the static items
        when no object has been instantiated from the class.  If the Dog class lists A and B has non-
        static items and C as a static item, if you declare Dog d1, d2; you can access d1.A and d1.B
        and d2.A and d2.B  but you can access Dog.C if you want to, since it's not stored in any
        specific object ("stored in the class" is the vernacular).  The "companion object" in Kotlin
        contains what would be the static stuff in Java.
         */
        val EXTRA_ANSWER_IS_TRUE = "com.example.stupidgame.extraansweristrue"

        fun newIntent (packageContext: Context?,answerIsTrue:Boolean): Intent {
/* I put newIntent inside CheatActivity since what's where CheatActivity's stuff
is.  the Context is the activity I'm calling from.  I create an intent with the activity I'm
calling from, the new activity I'm calling.  And then variables that the new activity will need.
There is no CheatActivity yet, when I call newIntent.  I put the method in here, though,
because it's part of CheatActivity's stuff.
 */
            val i: Intent = Intent (packageContext!!,CheatActivity::class.java)
            i.putExtra (EXTRA_ANSWER_IS_TRUE,answerIsTrue)
            return i
        }
    }

    var ACTIVITY = "CheatActivity"
    var mAnswerIsTrue:Boolean? = null
    var mAnswerTextView: TextView? = null
    var mShowAnswer: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d (ACTIVITY,"onCreate")
        setContentView(R.layout.activity_cheat)
        mAnswerIsTrue = intent.getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false)
        //extracts a boolean from the intent with the given name and with a default value of false
        mAnswerTextView = findViewById (R.id.answerbox)
        mShowAnswer = findViewById (R.id.cheatbutton)
        mShowAnswer!!.setOnClickListener {
            if (mAnswerIsTrue!!)
                mAnswerTextView!!.setText (R.string.truetext)
            else
                mAnswerTextView!!.setText (R.string.falsetext)
            Handler(Looper.getMainLooper()).postDelayed (
                {finish()},5000
            )
            //finish() is the method that kills an activity directly.
            //The Handler and Looper stuff say that the finish command
            //just execute after a delay of 5000 milliseconds
            }


    }
override fun onStart () {
    super.onStart()
    Log.d (ACTIVITY,"onStart")
}
override fun onResume () {
    super.onResume()
    Log.d (ACTIVITY,"onResume")
}
override fun onPause () {
    super.onPause()
    Log.d (ACTIVITY,"onPause")
}
override fun onStop () {
    super.onStop()
    Log.d (ACTIVITY,"onStop")
}
override fun onDestroy () {
    super.onDestroy()
    Log.d (ACTIVITY,"onDestroy")
}
override fun onRestart () {
    super.onRestart()
    Log.d (ACTIVITY,"onRestart")
}
}