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 com.example.stupidgame.ui.theme.StupidGameTheme
//import com.google.firebase.inappmessaging.model.Button

class MainActivity : ComponentActivity() {
    var ACTIVITY = "MainActivity"
    val mQuestions = arrayOf (Question (R.string.question1,true),
        Question (R.string.question2,true),
        Question (R.string.question3,false),
        Question (R.string.question4,false),
        Question (R.string.question5,false))

    var mTrueButton: Button? = null
    var mFalseButton:Button? = null
    var mNextButton:Button? = null
    var mCheatButton:Button? = null
    var mQuestion: TextView? = null
    var mCurrent = 0
    var mIsCheater = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d (ACTIVITY,"onCreate")
        setContentView (R.layout.activity_main)
        mTrueButton = findViewById(R.id.truebutton)
        mTrueButton!!.setOnClickListener {
            GradeAnswer (true)
        }
        mFalseButton = findViewById(R.id.falsebutton)
        mFalseButton!!.setOnClickListener {
            GradeAnswer (false)
        }
        mNextButton = findViewById(R.id.nextbutton)

        mNextButton!!.setOnClickListener {

            mCurrent = (mCurrent+1) % mQuestions.size
            mQuestion!!.setText (mQuestions[mCurrent].mQuestionTextID)


        }

        val resultLauncher = registerForActivityResult (ActivityResultContracts.StartActivityForResult()) {
            //This is the code that will run when the called activity dies
            result ->
            if (result.resultCode==Activity.RESULT_OK) {  //The activity terminated normally, as opposed to crashed.
                val data: Intent? = result.data  //the information returned
                if (data==null) mIsCheater=false
                else mIsCheater = CheatActivity.wasAnswerShown (data) //extracting a boolean from the result
                if (mIsCheater) mQuestion!!.setText (R.string.didcheat)
                else mQuestion!!.setText (R.string.didnotcheat)

                //Why am I using setText rather than text=?
                //text= is for when you're literally assigning a string, but R.string.whatever is
                //not technically a string.  It's an ID number for a string in the strings resource
                //file.  setText looks up the ID, finds the string, and puts the string into the text field.

            }

        }
        mCheatButton = findViewById (R.id.maincheatbutton)
        mCheatButton!!.setOnClickListener {
            val answerIsTrue = mQuestions[mCurrent].mAnswer
            val i = CheatActivity.newIntent (this,answerIsTrue)
            //startActivity (i) //call activity with intent.  Intent contains which new activity is going to be called and what information will passed to it
        /*
        startActivity is what you use to start an activity that will NOT
        return information to the calling activity.

        If you want to start an activity that's going to return information to you
        when it (eventually) dies, you can't use startActivity.  You use resultLauncher

        */
            resultLauncher.launch (i) //Calling an activity that will return information to me
        }


        //an "intent" is a data structure that allows you to pass information between activites

        mQuestion = findViewById (R.id.question)
        mQuestion!!.setText (mQuestions[mCurrent].mQuestionTextID)
    }

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

