package net.heartshield.ui;

import android.graphics.Color;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

/**
 * Implements UI look and feel.
 * Logical methods are in LineChartWrapper directly.
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-03-20
 */
class LineChartUI {
    LineChart mChart;
    float mLineWidth;
    int mLineColor;

    /**
     * pass in an existing LineChart from the UI:
     * <p>
     * <pre>
     *     new LineChartUI((LineChart) findViewById(R.id.chart))
     * </pre>
     */
    LineChartUI(LineChart chart, float lineWidth, int lineColor) {
        mChart = chart;
        mLineWidth = lineWidth;
        mLineColor = lineColor;

        setupChart();
        setupData();
        setupAxes();
    }

    LineDataSet createDataSet() {

        LineDataSet set = new LineDataSet(null, "Dynamic Data");
        set.setAxisDependency(YAxis.AxisDependency.LEFT);
        //set.setColor(ColorTemplate.getHoloBlue());

        set.setColor(mLineColor);

        set.setCircleColor(Color.WHITE);
        set.setLineWidth(mLineWidth);

        set.setCircleRadius(4f);
        set.setFillAlpha(65);
        set.setFillColor(ColorTemplate.getHoloBlue());
        set.setHighLightColor(Color.rgb(244, 117, 117));
        set.setValueTextColor(Color.WHITE);
        set.setValueTextSize(9f);
        set.setDrawValues(false);

        set.setDrawCircles(false);
        set.setHighlightEnabled(false);

        return set;
    }

    /** general chart look and feel settings */
    private void setupChart() {
        // enable description text
        mChart.getDescription().setEnabled(false);

        // enable touch gestures
        mChart.setTouchEnabled(true);

        // enable scaling and dragging
        mChart.setDragEnabled(true);
        mChart.setScaleEnabled(true);
        mChart.setDrawGridBackground(false);

        // if disabled, scaling can be done on x- and y-axis separately
        mChart.setPinchZoom(false);

        // set an alternative background color
        //mChart.setBackgroundColor(Color.LTGRAY);
        mChart.setBackgroundColor(Color.WHITE);
    }

    private void setupData() {
        LineData data = new LineData();
        data.setValueTextColor(Color.WHITE);

        data.setHighlightEnabled(false);

        // add empty data
        mChart.setData(data);

        LineDataSet set = createDataSet();
        data.addDataSet(set);


        // get the legend (only possible after setting data)
        Legend l = mChart.getLegend();

        l.setEnabled(false);
        // modify the legend ...
        //l.setForm(Legend.LegendForm.LINE);
        //l.setTypeface(mTfLight);
        //l.setTextColor(Color.WHITE);
    }

    float AXIS_MIN = 0f;
    float AXIS_MAX = 1f;

    private void setupAxes() {
        XAxis xl = mChart.getXAxis();
        //xl.setTypeface(mTfLight);
        xl.setTextColor(Color.WHITE);
        xl.setDrawGridLines(false);
        xl.setAvoidFirstLastClipping(true);
        xl.setEnabled(false);

        YAxis leftAxis = mChart.getAxisLeft();
        //leftAxis.setTypeface(mTfLight);
        leftAxis.setTextColor(Color.WHITE);
        leftAxis.setAxisMaximum(AXIS_MAX);
        leftAxis.setAxisMinimum(AXIS_MIN);
        leftAxis.setDrawGridLines(false);
        leftAxis.setEnabled(false);

        YAxis rightAxis = mChart.getAxisRight();
        rightAxis.setEnabled(false);
    }
}
