package com.example.stupidgame

import Question
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.view.KeyEventDispatcher.Component
import com.example.stupidgame.ui.theme.StupidGameTheme

class MainActivity : ComponentActivity() {

    var mTrueButton: Button? = null
    var mFalseButton:Button? = null
    var mNextButton:Button? = null
    var mCheatButton:Button? = null
    var mQuestion: TextView? = null
    var ACTIVITY = "MainActivity"
    val mQuestions = arrayOf (Question (R.string.question1,true),
        Question (R.string.question2,false),
        Question (R.string.question3,true),
        Question (R.string.question4,false),
        Question (R.string.question5,false))
    var mCurrent = 0
    var mIsCheater = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d (ACTIVITY,"onCreate Valentine")
        setContentView(R.layout.activity_main)
        mTrueButton = findViewById(R.id.truebutton)
        mFalseButton = findViewById(R.id.falsebutton)
        mNextButton = findViewById (R.id.nextbutton)
        mCheatButton = findViewById (R.id.maincheatbutton)
        mQuestion = findViewById (R.id.question)
        mQuestion!!.setText (mQuestions[mCurrent].mQuestionTextID)
        mNextButton!!.setOnClickListener {

            mCurrent = (mCurrent+1)%mQuestions.size
            mQuestion!!.setText (mQuestions[mCurrent].mQuestionTextID)
        }
        mTrueButton!!.setOnClickListener {
            GradeAnswer (true)
        }
        mFalseButton!!.setOnClickListener {
            GradeAnswer (false)
        }

        val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
     //this is the code that will run when the activity resumes control

            result ->
        //We are code, essentially, as a paramater to another method.  What's actually happening, is we
            //creating an object and overriding a method in that object.  Since this particular object
            //(whatever it is) only has one method that could possibly be overridden, Kotlin knows
            //that this code will override that one method, so we don't even to bother with the name of
            //that method.  (This is called an "anonymous method"; a method without an actual signature.)
            //But it does take one parameter and since we don't have a method signature we can't declare
            //the parameter.  So in case, we type result -> meaning that we are calling the method's
            //one parameter "result" throughout this method.
            //In this case, the parameter to this is an intent, so result is the intent being passed to this method.
            //When the CheatActivity intends, this method will be called with the intent passed back by
            //CheatActivity.  So "result" will store whether the user cheated or not.

            if (result.resultCode== Activity.RESULT_OK) {  //Did the activity terminate normally?  As opposed to crash?

                var data: Intent? = result.data
                if (data==null) mIsCheater=false
                else mIsCheater = CheatActivity.wasAnswerShown (data);
                if (mIsCheater) mQuestion!!.setText (R.string.didcheat)
                else mQuestion!!.setText (R.string.didnotcheat)
            }




        }

        mCheatButton!!.setOnClickListener {
            val answerIsTrue = mQuestions[mCurrent].mAnswer
            val i = CheatActivity.newIntent (this,answerIsTrue)  //i is now a new intent
            //containing whether the current question is true or false
            Log.d (ACTIVITY, "About To Start")
        //    startActivity (i)  //is what you call when the new activity doesn't have to return anything back to you
            resultLauncher.launch (i)  //is what you call when the new activity will return information to you
        }
    }

    fun GradeAnswer (ans:Boolean) {

        if (ans==mQuestions[mCurrent].mAnswer)
            Toast.makeText (this,R.string.correct,Toast.LENGTH_SHORT).show()
        else
            Toast.makeText (this,R.string.incorrect,Toast.LENGTH_SHORT).show()


    }
    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")
    }

}