package com.example.courseworkkotlin

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.courseworkkotlin.databinding.ActivityTemperResultBinding
import com.github.mikephil.charting.data.RadarData
import com.github.mikephil.charting.data.RadarDataSet
import com.github.mikephil.charting.data.RadarEntry
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter

class TemperResult : AppCompatActivity() {
    private lateinit var binding: ActivityTemperResultBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityTemperResultBinding.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 radarEntries: ArrayList<RadarEntry> = ArrayList()

        answers?.forEach {
            if (it < 1) radarEntries.add(RadarEntry(1f))
            else radarEntries.add(RadarEntry(it.toFloat()))
        }

        val dataSet = RadarDataSet(radarEntries, "Tempers").apply {
            color = R.color.colorPrimary
            fillColor = R.color.colorAccent
            lineWidth = 2f
            setDrawFilled(true)
            fillAlpha = 200
        }

        val radarData = RadarData().apply {
            addDataSet(dataSet)
            setDrawValues(false)
        }

        binding.Graph.xAxis.apply {
            xOffset = 0f
            yOffset = 0f
            valueFormatter = IndexAxisValueFormatter(labels)
            textSize = 16f
        }

        binding.Graph.apply {
            data = radarData
            description.isEnabled = false
            legend.isEnabled = false
            yAxis.isEnabled = false
        }
    }
}