/*
 * Copyright (C) 2022 - Florindo Smilari (spherIC)
 */
package ch.spherIC.recurvebowsight.components;

import android.content.Context;
import android.graphics.Color;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.text.DecimalFormat;

import ch.spherIC.recurvebowsight.R;
import ch.spherIC.recurvebowsight.logic.TrajectoryCalculator;


/**
 * @author FC Smilari
 */
public class FlightCurveChart {

    private XYMultipleSeriesDataset dataSet;
    private XYMultipleSeriesRenderer mRenderer;

    public GraphicalView getView(final Context context) {

        XYSeries series = new XYSeries(context.getResources().getString(R.string.fcChart_TitleLable));

        this.dataSet = new XYMultipleSeriesDataset(); // collection of series under one object.,there could any
        this.dataSet.addSeries(series);               // number of series

        //customization of the chart

        XYSeriesRenderer renderer = new XYSeriesRenderer(); // one renderer for one series
        renderer.setColor(Color.RED);
        renderer.setDisplayChartValues(false);
        renderer.setLineWidth(2f);

        this.mRenderer = new XYMultipleSeriesRenderer();
        this.mRenderer.addSeriesRenderer(renderer);
        this.mRenderer.setMargins(new int[]{60, 80, 60, 60}); // top, left, bottom, right
        this.mRenderer.setShowLegend(true);
        this.mRenderer.setAntialiasing(true);
        this.mRenderer.setLabelsColor(Color.BLACK);

        // Beschriftungen
        this.mRenderer.setChartTitle(context.getResources().getString(R.string.fcChart_Title));
        this.mRenderer.setChartTitleTextSize(30);
        this.mRenderer.setAxisTitleTextSize(30);
        this.mRenderer.setLegendTextSize(30);
        this.mRenderer.setLabelFormat(new DecimalFormat("0.##"));
        this.mRenderer.setLabelsTextSize(15);

        this.mRenderer.setXTitle(context.getResources().getString(R.string.fcChart_TitleXAxis));
        this.mRenderer.setXLabelsColor(Color.DKGRAY);

        this.mRenderer.setYTitle(context.getResources().getString(R.string.fcChart_TitleYAxis));
        this.mRenderer.setYLabelsPadding(20);
        this.mRenderer.setYLabelsVerticalPadding(-5);
        this.mRenderer.setYLabelsColor(0, Color.DKGRAY);

        // Zoom ausschalten
        this.mRenderer.setZoomEnabled(false, false);
        this.mRenderer.setZoomButtonsVisible(false);

        // Grid
        this.mRenderer.setShowGridX(true);
        this.mRenderer.setShowGridY(true);
        this.mRenderer.setGridColor(Color.DKGRAY);

        // X/Y-Achsen
        this.mRenderer.setShowAxes(true);
        this.mRenderer.setAxesColor(Color.DKGRAY);

        // Hintergrundfarbe
        this.mRenderer.setApplyBackgroundColor(true);
        this.mRenderer.setBackgroundColor(context.getResources().getColor(R.color.qrt_translucent_white));
        this.mRenderer.setMarginsColor(Color.WHITE);

        // Verschiebungen ausschalten
        this.mRenderer.setPanEnabled(false, false);

        GraphicalView v = ChartFactory.getLineChartView(context, this.dataSet, this.mRenderer);
        return v;
    }

    /**
     * @return the dataSet
     */
    public XYMultipleSeriesDataset getDataSet() {
        return this.dataSet;
    }

    /**
     * @return the mRenderer
     */
    public XYMultipleSeriesRenderer getMultiRenderer() {
        return this.mRenderer;
    }

    public void updateRendererAfterCalculation(final Double shootingDistance, final Double[][] flightCurve) {

        Double deltaH;
        Double dy;
        Double rangeY;
        int xLbls = 1;
        int yLbls = 1;
        Double minH = 0.0;
        Double maxH = 0.0;


        // X-Achse
        if (shootingDistance <= 18d) {
            xLbls = (int) Math.round(shootingDistance);
        } else if (shootingDistance <= 30d) {
            xLbls = (int) Math.round(shootingDistance) / 2;
        } else if (shootingDistance <= 90d) {
            xLbls = (int) Math.round(shootingDistance) / 5;
        } else {
            xLbls = (int) Math.round(shootingDistance) / 10;
        }
        this.mRenderer.setXLabels(xLbls);

        // Y-Achse
        for (int i = 0; i < flightCurve.length; i++) {
            if (flightCurve[i][TrajectoryCalculator.IDX_Y] > maxH) {
                maxH = flightCurve[i][TrajectoryCalculator.IDX_Y];
            }
        }
        minH = maxH;
        for (int i = 0; i < flightCurve.length; i++) {
            if (flightCurve[i][TrajectoryCalculator.IDX_Y] < minH) {
                minH = flightCurve[i][TrajectoryCalculator.IDX_Y];
            }
        }
        deltaH = maxH - minH;

        if (deltaH <= shootingDistance / 10) {
            dy = (shootingDistance / 10 - deltaH) / 2;
            rangeY = shootingDistance / 10;
        } else if (deltaH <= shootingDistance / 5) {
            dy = (shootingDistance / 5 - deltaH) / 2;
            rangeY = shootingDistance / 5;
        } else {
            dy = (deltaH + shootingDistance * 0.1) / 2;
            rangeY = deltaH + shootingDistance * 0.1;
        }

        if (rangeY <= 3d) {
            yLbls = 10;
        } else if (rangeY <= 6d) {
            yLbls = 12;
        } else if (rangeY <= 9d) {
            yLbls = 18;
        } else {
            yLbls = 20;
        }

        this.mRenderer.setYLabels(yLbls);
        this.mRenderer.setYAxisMin(minH - dy);
        this.mRenderer.setYAxisMax(maxH + dy);
    }
}
