package com.example.helloworld

import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
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.helloworld.ui.theme.HelloWorldTheme

class MainActivity : ComponentActivity() {
    var mAnswer: TextView? = null
//"Traditionally" widget variables had names beginning with m,
//back when Android apps were written in Java.  This is because
//Android Studio has a button somewhere that will automatically
//generate basic sets and gets for any variable beginning with m.
//Kotlin doesn't have sets and gets, so the m tradition is no longer
//necessary, but I still do it out of habit.
    var mWord: EditText? = null
    var mButt: Button? = null
    var correctanswer = 0
    var guesses = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView (R.layout.activity_main)
        mAnswer = findViewById (R.id.spiderman)
        mAnswer!!.movementMethod = ScrollingMovementMethod()
        mWord = findViewById (R.id.batman)
        mButt = findViewById (R.id.superman)
        correctanswer = (Math.random()*1000+1).toInt() //random int between 1 and 1000
        mButt!!.setOnClickListener { //This method will run whenever the button is clicked
            var guess = mWord!!.text.toString().toInt()
            guesses++
            if (guess < correctanswer)
                mAnswer!!.text = mAnswer!!.text.toString()+"${guess} is too low.\n"
            else if (guess > correctanswer)
                mAnswer!!.text = mAnswer!!.text.toString()+"${guess} is too high.\n"
            else {
                mAnswer!!.text = mAnswer!!.text.toString()+"${guess} is correct.\n"
                mAnswer!!.text = mAnswer!!.text.toString()+"\nIt took you ${guesses} tries.\n"
                mButt!!.setOnClickListener  { }
                mButt!!.isEnabled = false
                mWord!!.isEnabled = false
            }
            mWord!!.setText ("")
        }

    }
}

//In Android apps, your application is made up of one or more activities.

//This is our main activity, which means the activity that runs first when the app starts.
//the actual identity of the main activity is stored in the Manifest.