package com.xyz.foldableappcontinuity

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {

    var scoreView1: TextView? = null
    var scoreView2: TextView? = null

    var team1Score: Int = 0
    var team2Score: Int = 0
    var tempScore1: Int = 0
    var tempScore2: Int = 0
    lateinit var btnInc1 : Button
    lateinit var btnDec1 : Button
    lateinit var btnDec2 : Button
    lateinit var btnInc2 : Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        scoreView1 = findViewById(R.id.team1Score) as TextView
        scoreView2 =  findViewById(R.id.team2Score) as TextView

        if(savedInstanceState != null){
            var text1: String? = savedInstanceState.getString("score1")
            var text2: String? = savedInstanceState.getString("score2")
            scoreView1?.text = text1
            scoreView2?.text = text2
            team1Score = text1?.toInt()!!
            team2Score = text2?.toInt()!!
        }

        btnInc1 = findViewById(R.id.btnIncTeam1)
        btnInc2 = findViewById(R.id.btnIncTeam2)
        btnDec1 = findViewById(R.id.btnDecTeam1)
        btnDec2 = findViewById(R.id.btnDecTeam2)

        btnInc1.setOnClickListener { increamentForTeam1() }
        btnInc2.setOnClickListener { increamentForTeam2() }
        btnDec1.setOnClickListener { deccreamentForTeam1() }
        btnDec2.setOnClickListener { deccreamentForTeam2() }


    }

    override fun onPause() {
        if(team1Score!=0 && team2Score != 0){
            tempScore1 = team1Score
            tempScore2 = team2Score

        }
        super.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        scoreView1 = findViewById(R.id.team1Score) as TextView
        scoreView2 =  findViewById(R.id.team2Score) as TextView

        outState.putString("score1", tempScore1.toString())
        outState.putString("score2", tempScore2.toString())
        super.onSaveInstanceState(outState)
    }


    /**
     * Increase the score of Team 1 by 1 point.
     */
    fun increamentForTeam1() {
        team1Score = team1Score+1
        scoreView1?.text = team1Score.toString()
    }


    /**
     * Decrease the score of Team 1 by 1 point.
     */
    fun deccreamentForTeam1() {
        if(team1Score == 0){
            return
        }
        team1Score = team1Score-1
        scoreView1?.text = team1Score.toString()
    }


    /**
     * Increase the score of Team 2 by 1 point.
     */
    fun increamentForTeam2() {
        team2Score = team2Score+1
        scoreView2?.text = team2Score.toString()

    }

    /**
     * Decrease the score of Team 2 by 1 point.
     */
    fun deccreamentForTeam2() {
        if(team2Score == 0){
            return
        }
        team2Score = team2Score-1
        scoreView2?.text = team2Score.toString()
    }

}

