/**
 * Copyright (C) 2005-2012 XELOG AG
 */
package ch.spherIC.recurvebowsight.components;

import android.content.Context;

import android.graphics.Color;

import ch.spherIC.recurvebowsight.R;

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;


/**
 * @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.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) {

        int xLbls = 1;

        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);
        this.mRenderer.setYLabels(10);
        this.mRenderer.setYAxisMin(-0.9);
        this.mRenderer.setYAxisMax(0.9);

    }
}
