package com.example.stupidgame

import android.content.Context
import android.content.Intent
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.collection.emptyLongSet
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class CheatActivity : ComponentActivity() {

    companion object {
        //The companion object is a stupid name for a moderately stupid concept.
        //The companion object is a carryover from Java.  In Java, data and methods can be
        //declared to be "static".  "static" means that there is only one copy of this stuff
        //and all objects of this class share int.  If a static int is set to 5 in one object,
        //it is set to 5 in all objects of this class...because in reality there's only of them.
        //In Kotlin, the word static is removed and all the static stuff is listed together
        //in the companion object.  You don't have to worry about this doing too esoteric.
        //The companion object is just the static stuff, and that's all it is!

        val EXTRA_ANSWER_IS_TRUE = "com.example.stupidgame.extraansweristrue"
        //That big long string is just the name of the variable in the intent.  Why so long?
        //It doesn't have to be that long, but convention is that the intent variable names
        //match the package name with an extra identifier appended.
        val EXTRA_ANSWER_SHOWN = "com.example.stupidgame.extraanswershown"

        fun newIntent (packageContext: Context?, answerIsTrue:Boolean):Intent {

            val i: Intent = Intent (packageContext!!,CheatActivity::class.java)
            //This intent is designed to pass information from MainActivity to CheatActivity
            //When you create an Intent, you specify the class that's calling the activity and the
            //class of the new activity.  In this case, the main activity is being passed by
            //parameter as packageContext
            i.putExtra (EXTRA_ANSWER_IS_TRUE,answerIsTrue)
            //putExtra adds parameters to the intent
            return i
        }

        fun wasAnswerShown (result:Intent?):Boolean {
            return result!!.getBooleanExtra (EXTRA_ANSWER_SHOWN,false)
        }
        //By putting a method into the companion object, I can call the method without actually declaring
        //a CheatActivity variable.  I can just say CheatActivity.wasAnswerShown (...).
        //This is just like a static method in Java.  I don't need to instantiate it to use it.
    }

    val ACTIVITY = "CHEATACTIVITY"

    var mShowAnswer: Button? = null
    var mNotCheat:Button? = null
    var mAnswerIsTrue:Boolean? = null
    var mAnswerTextView: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d (ACTIVITY,"onCreate Valentine")
        setContentView(R.layout.activity_cheat)
        mShowAnswer = findViewById (R.id.cheatbutton)
        mNotCheat = findViewById (R.id.dontcheatbutton)
        mAnswerTextView = findViewById (R.id.answerbox)
        mAnswerIsTrue = intent.getBooleanExtra (EXTRA_ANSWER_IS_TRUE,false)
        //The intent is the mechanism by which one activity passes information to another
        //Think of the intent as storing "parameters" to the activity.  When activity creates
        //another, it also creates an intent with information to give the new activity.
        //In this case, MainActivity is telling CheatActivity whether the statement is
        //true or false.  That's why it's passing a boolean, the statement is either
        //true or false.

        mShowAnswer!!.setOnClickListener {
            setAnswerShownResult (true)
            if (mAnswerIsTrue!!)
                mAnswerTextView!!.setText (R.string.truetext)
            else
                mAnswerTextView!!.setText (R.string.falsetext)
            Handler (Looper.getMainLooper()).postDelayed ( {finish()},5000)
              //Kills the activity and returns to the original activity

        }

        mNotCheat!!.setOnClickListener {
            setAnswerShownResult (false)
            mAnswerTextView!!.setText(R.string.honest)
            Handler (Looper.getMainLooper()).postDelayed ( {finish()},5000)
        }
    }

    fun setAnswerShownResult (isAnswerShown:Boolean) {

        val data:Intent? = Intent()
        data!!.putExtra (EXTRA_ANSWER_SHOWN,isAnswerShown)
        setResult (RESULT_OK,data)
        //setResult DOES NOT KILL THE ACTIVITY.  It just says that when the activity eventually
        //dies, the passed intent will be sent to the calling activity.  In this the intent called "data"
        //will be sent to the MainActivity.  But not this instant.
    }
    override fun onStart() {
        super.onStart()
        Log.d (ACTIVITY,"onStart Valentine")
    }
    override fun onResume() {
        super.onResume()
        Log.d (ACTIVITY,"onResume Valentine")
    }
    override fun onPause() {
        super.onPause()
        Log.d (ACTIVITY,"onPause Valentine")
    }
    override fun onStop() {
        super.onStop()
        Log.d (ACTIVITY,"onStop Valentine")
    }
    override fun onDestroy() {
        super.onDestroy()
        Log.d (ACTIVITY,"onDestroy Valentine")
    }
    override fun onRestart() {
        super.onRestart()
        Log.d (ACTIVITY,"onRestart Valentine")
    }

}