package hozza.network.views.frontend.components;

import com.storedobject.chart.*;
import hozza.network.models.twitch.result.IWatchingDayOfWeek;

import java.time.DayOfWeek;
import java.util.List;

public class WatchingDayOfWeekChart extends SOChart implements IChartGenerable
{
    final List<IWatchingDayOfWeek> service;
    public WatchingDayOfWeekChart(List<IWatchingDayOfWeek> service)
    {
        this.service = service;
        this.generateChart();
    }

    public void generateChart()
    {
        LineChart[] lineCharts = new LineChart[7];

        Data[] xValues = new Data[lineCharts.length];
        Data[] yValues = new Data[lineCharts.length];

        for(int i = 0; i < lineCharts.length; i++)
        {
            xValues[i] = new Data();
            xValues[i].setName("hour");
            yValues[i] = new Data();
            yValues[i].setName("number of viewers");
        }
        this.service.forEach(e ->
        {
            yValues[e.getDayOfWeek()].add(e.getCount());
            xValues[e.getDayOfWeek()].add(e.getHourOfDay());
        });

        for(int i = 0; i < lineCharts.length; i++)
        {
            DayOfWeek dayOfWeek = DayOfWeek.of(i+1);
            lineCharts[i] = new LineChart(xValues[i], yValues[i]);
            lineCharts[i].setName(dayOfWeek.name());
        }

        XAxis xAxis = new XAxis(DataType.NUMBER);
        YAxis yAxis = new YAxis(DataType.NUMBER);

        RectangularCoordinate rc = new RectangularCoordinate(xAxis, yAxis);

        for (LineChart lineChart : lineCharts) {
            lineChart.plotOn(rc);
            this.add(lineChart);
        }

        this.add(new Title("Viewers based on day of week and hour"));
    }
}
