package com.example.courseworkkotlin

import android.content.Intent
import android.os.Bundle
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import com.example.courseworkkotlin.databinding.ActivityProfileResultBinding
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.github.mikephil.charting.utils.ColorTemplate

class ProfileResult : AppCompatActivity() {
    private lateinit var binding: ActivityProfileResultBinding

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

        displayResult()

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

    private fun displayResult() {
        val labels: Array<String> = arrayOf(
            "Фізика", "",
            "Математика", "",
            "Електро- та радіотехніка", "",
            "Техніка", "",
            "Хімія", "",
            "Біологія", "",
            "Медицина", "",
            "Географія", "",
            "Історія", "",
            "Філологія", "",
            "Мистецтво", "",
            "Педагогіка", "",
            "Психологія, філософія", "",
            "Бізнес", "",
            "Сфера обслуговувуння", "",
            "Військова справа", "",
            "Спорт", ""
        )
        val answers = intent.getIntArrayExtra("answers")

        val  barEntries: ArrayList<BarEntry> = ArrayList()

        answers?.forEachIndexed { index, answer -> barEntries.add(BarEntry(index*2f, answer.toFloat())) }

        val barDataSet = BarDataSet(barEntries, "profileAnswers").apply {
            colors = ColorTemplate.MATERIAL_COLORS.toList()
            valueTextColor = Color.BLACK
            setDrawValues(false)
        }

        val barData = BarData(barDataSet)

        binding.Graph.xAxis.apply {
            yOffset = 0f
            xOffset = 0f
            position = XAxis.XAxisPosition.BOTTOM
            setDrawGridLines(false)
            textSize = 16f
            valueFormatter = IndexAxisValueFormatter(labels)
        }

        binding.Graph.apply {
            setFitBars(true)
            setDrawBarShadow(false)
            data = barData
            description.isEnabled = false
            legend.isEnabled = false
            setVisibleXRangeMaximum(3.8f)
            scaleY = 1f
            axisRight.isEnabled = false
            setDrawGridBackground(false)
        }
    }
}