package com.hesiii.i_hear.HearingScreening;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.hesiii.i_hear.Models.Status;
import com.hesiii.i_hear.R;
import com.hesiii.i_hear.databinding.ActivityResultBinding;

import java.util.ArrayList;
import java.util.List;

public class ResultActivity extends AppCompatActivity {

    private ArrayList<Status> statusList;
    private ActivityResultBinding binding;
    private String status;
    private String description;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityResultBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        if (getSupportActionBar() != null ){

            getSupportActionBar().setTitle("Audio Diagram");

        }

        statusList = (ArrayList<Status>) getIntent().getSerializableExtra("resultList");
        status = getIntent().getStringExtra("status");

        binding.yourStatus.setText(status);

        if (status.equals("Normal Hearing Level")) {

            description = "Your hearing level is normal.  You can hear a wide range of sounds with clarity!";

        } else if (status.equals("Mild to Moderate Level")) {

            description = "You are in mild to moderate hearing stage. Communication can be significantly impacted. " +
                    "You may require specialized devices or support!";

        } else {

            description = "You have a profound hearing loss. Communication is very challenging without sign language or cochlear implants. " +
                    "Consider consulting an healthcare professional for personalized solutions!";

        }

        binding.descriptionText.setText(description);

        for (int i = 0; i < statusList.size(); i++) {

            System.out.println(String.valueOf(i+1)+" Result - DB : "+statusList.get(i).getDb()+" / HZ : "+statusList.get(i).getHz());

        }

        // Customization
        binding.resultsLineChart.getDescription().setEnabled(false);
        binding.resultsLineChart.setTouchEnabled(true);
        binding.resultsLineChart.setDragEnabled(true);
        binding.resultsLineChart.setScaleEnabled(true);
        binding.resultsLineChart.setPinchZoom(true);
        binding.resultsLineChart.setDrawGridBackground(true);

        XAxis xAxis = binding.resultsLineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setGranularity(1f);
        xAxis.setAxisMinimum(100f);
        xAxis.setAxisMaximum(2000f);
        xAxis.enableGridDashedLine(10f, 10f, 0f);

        YAxis leftAxis = binding.resultsLineChart.getAxisLeft();
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(120f);
        leftAxis.enableGridDashedLine(10f, 10f, 0f);

        List<Entry> entries = new ArrayList<>();
        for (int i = 0; i < statusList.size(); i++) {

            entries.add(new Entry(statusList.get(i).getHz(), statusList.get(i).getDb()));

        }

        LineDataSet lineDataSet = new LineDataSet(entries, "Hearing Test");
        lineDataSet.setColor(Color.parseColor("#3390FF"));
        lineDataSet.setLineWidth(2f);
        lineDataSet.setCircleColor(Color.parseColor("#3390FF"));
        lineDataSet.setCircleRadius(5f);
        lineDataSet.setDrawCircleHole(true);
        lineDataSet.setDrawValues(true);

        LineData lineData = new LineData(lineDataSet);

        binding.resultsLineChart.setData(lineData);
        binding.resultsLineChart.invalidate(); // Refresh the chart to display the data

    }
}