package com.example.courseworkkotlin

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.courseworkkotlin.databinding.ActivityProfileBinding

class Profile : AppCompatActivity() {
    private lateinit var binding: ActivityProfileBinding

    private var questionId = 0
    private var discipline = 0
    private var results = IntArray(17)
    private lateinit var questions: Array<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityProfileBinding.inflate(layoutInflater)
        setContentView(binding.root)

        questions = resources.getStringArray(R.array.Profile_Questions)

        binding.ButtonDoublePlus.setOnClickListener { processQuestion(2) }
        binding.ButtonPlus.setOnClickListener { processQuestion(1) }
        binding.ButtonZero.setOnClickListener { processQuestion(0) }
        binding.ButtonMinus.setOnClickListener { processQuestion(-1) }
        binding.ButtonDoubleMinus.setOnClickListener { processQuestion(-2) }

        binding.home.setOnClickListener { startActivity(Intent(this, HomeScreen::class.java)) }

        trackQuestionNumber()
        setQuestion()
    }

    private fun trackQuestionNumber() {
        binding.tracker.text = String.format("%s/%s", questionId + 1, questions.size)
    }

    private fun setQuestion() {
        binding.ProfileQuestion.text = questions[questionId]
    }

    private fun processQuestion(amount: Int) {
        if (questionId < questions.size - 1) {
            questionId ++

            results[discipline] += amount

            discipline = (discipline + 1) % 17

            if (discipline == 16) discipline = 0
            else discipline ++

            setQuestion()
            trackQuestionNumber()
        } else {
            val intent = Intent(this, ProfileResult::class.java).apply { putExtra("answers", results) }
            Log.d("answers", results.contentToString())
            startActivity(intent)
        }
    }
}