package com.kaviz.heal.Activities;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
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.utils.ColorTemplate;
import com.kaviz.heal.API.ApiRequestData;
import com.kaviz.heal.API.RetroServer;
import com.kaviz.heal.Model.ResponseModelOne;
import com.kaviz.heal.R;
import com.kaviz.heal.databinding.ActivityGraphsAndPredictionBinding;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GraphsAndPredictionActivity extends AppCompatActivity {

    private ActivityGraphsAndPredictionBinding binding;

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

        if (getSupportActionBar() != null) {

            getSupportActionBar().setTitle(getResources().getString(R.string.graphs_future_predictions));

        }

        getGraphsAndPrediction();


    }

    private void getGraphsAndPrediction() {

        binding.progressBar.setVisibility(View.VISIBLE);

        ApiRequestData apiRequestData = RetroServer.connectRetrofit().create(ApiRequestData.class);
        Call<ResponseModelOne>call = apiRequestData.getGraphsAndPrediction();
        call.enqueue(new Callback<ResponseModelOne>() {
            @Override
            public void onResponse(@NonNull Call<ResponseModelOne> call, @NonNull Response<ResponseModelOne> response) {

                if (response.body().getStatus().equals("success")) {

                    int [] bloodOxygenLevel = response.body().getBloodOxygenLevel();
                    int [] heartRate = response.body().getHeartRate();
                    int [] bodyTemperature = response.body().getBodyTemperature();
                    String riskLevel = response.body().getRiskLevel();

                    showBarchart(bloodOxygenLevel, heartRate, bodyTemperature, riskLevel);

                }

            }

            @Override
            public void onFailure(@NonNull Call<ResponseModelOne> call, @NonNull Throwable t) {

                Toast.makeText(GraphsAndPredictionActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                binding.progressBar.setVisibility(View.GONE);

            }
        });

    }

    private void showBarchart(int[] bloodOxygenLevel, int[] heartRate, int[] bodyTemperature, String riskLevel) {

        ArrayList<BarEntry>bloodOxygenLevelList = new ArrayList<>();
        ArrayList<BarEntry>heartRateList = new ArrayList<>();
        ArrayList<BarEntry>bodyTemperatureList = new ArrayList<>();

        for (int i = 0; i <= 10; i++) {

            bloodOxygenLevelList.add(new BarEntry(i, (float) bloodOxygenLevel[i]));
            heartRateList.add(new BarEntry(i, (float) heartRate[i]));
            bodyTemperatureList.add(new BarEntry(i, (float) bodyTemperature[i]));

        }

        BarDataSet bloodOxygenLevelData = new BarDataSet(bloodOxygenLevelList, "Blood Oxygen Level");
        BarDataSet heartRateData = new BarDataSet(heartRateList, "Heart Rate");
        BarDataSet bodyTemperatureData = new BarDataSet(bodyTemperatureList, "Body Temperature Level");

        bloodOxygenLevelData.setColors(ColorTemplate.COLORFUL_COLORS);
        heartRateData.setColors(ColorTemplate.COLORFUL_COLORS);
        bodyTemperatureData.setColors(ColorTemplate.COLORFUL_COLORS);

        bloodOxygenLevelData.setDrawValues(false);
        heartRateData.setDrawValues(false);
        bodyTemperatureData.setDrawValues(false);

        binding.bloodOxygenLevelBarChart.setData(new BarData(bloodOxygenLevelData));
        binding.heartRateBarChart.setData(new BarData(heartRateData));
        binding.bodyTemperatureBarChart.setData(new BarData(bodyTemperatureData));

        binding.bloodOxygenLevelBarChart.animateY(3000);
        binding.heartRateBarChart.animateY(3000);
        binding.bodyTemperatureBarChart.animateY(3000);

        binding.bloodOxygenLevelBarChart.getDescription().setText("Blood Oxygen Level");
        binding.heartRateBarChart.getDescription().setText("Heart Rate");
        binding.bodyTemperatureBarChart.getDescription().setText("Body Temperature Level");

        binding.bloodOxygenLevelBarChart.getDescription().setTextColor(Color.BLUE);

        binding.predictionText.setText(riskLevel);

        binding.progressBar.setVisibility(View.GONE);

    }

}