/*
 * Copyright 2015 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.hawkular.client.android.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import org.hawkular.client.android.R;
import org.hawkular.client.android.backend.BackendClient;
import org.hawkular.client.android.backend.model.Metric;
import org.hawkular.client.android.backend.model.MetricData;
import org.hawkular.client.android.backend.model.Tenant;
import org.hawkular.client.android.util.Intents;
import org.hawkular.client.android.util.ViewDirector;
import org.jboss.aerogear.android.pipe.callback.AbstractActivityCallback;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;
import timber.log.Timber;

public class MetricDataActivity extends AppCompatActivity {
    @InjectView(R.id.toolbar)
    Toolbar toolbar;

    @InjectView(R.id.chart)
    LineChartView chart;

    @Override
    protected void onCreate(Bundle state) {
        super.onCreate(state);
        setContentView(R.layout.activity_chart);

        setUpBindings();

        setUpToolbar();

        setUpMetricData();
    }

    private void setUpBindings() {
        ButterKnife.inject(this);
    }

    private void setUpToolbar() {
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void setUpMetricData() {
        showProgress();

        BackendClient.getInstance().getMetricData(getTenant(), getMetric(), this, new MetricDataCallback());
    }

    private void showProgress() {
        ViewDirector.of(this).using(R.id.animator).show(R.id.progress);
    }

    private Tenant getTenant() {
        return getIntent().getParcelableExtra(Intents.Extras.TENANT);
    }

    private Metric getMetric() {
        return getIntent().getParcelableExtra(Intents.Extras.METRIC);
    }

    private void setUpMetricData(List<MetricData> metricDataList) {
        List<PointValue> chartPoints = new ArrayList<>();

        List<AxisValue> chartAxisPoints = new ArrayList<>();

        for (int metricDataPosition = 0; metricDataPosition < metricDataList.size(); metricDataPosition++) {
            MetricData metricData = metricDataList.get(metricDataPosition);

            chartPoints.add(new PointValue(metricDataPosition, metricData.getValue()));

            chartAxisPoints.add(new AxisValue(metricDataPosition)
                .setLabel(DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date(metricData.getTimestamp()))));
        }

        Line chartLine = new Line(chartPoints)
            .setColor(getResources().getColor(R.color.background_primary_dark))
            .setCubic(true)
            .setHasPoints(false);

        LineChartData chartData = new LineChartData()
            .setLines(Collections.singletonList(chartLine));
        chartData.setAxisXBottom(new Axis()
            .setValues(chartAxisPoints));
        chartData.setAxisYLeft(new Axis()
            .setHasLines(true));

        chart.setLineChartData(chartData);

        Viewport chartViewport = new Viewport(chart.getMaximumViewport());

        chartViewport.bottom = chart.getMaximumViewport().bottom - 50;
        chartViewport.top = chart.getMaximumViewport().top + 50;

        chart.setMaximumViewport(chartViewport);
        chart.setCurrentViewport(chartViewport);

        hideProgress();
    }

    private void hideProgress() {
        ViewDirector.of(this).using(R.id.animator).show(R.id.chart);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case android.R.id.home:
                finish();
                return true;

            default:
                return super.onOptionsItemSelected(menuItem);
        }
    }

    private static final class MetricDataCallback extends AbstractActivityCallback<List<MetricData>> {
        @Override
        public void onSuccess(List<MetricData> metricDataList) {
            Timber.d("Metric data :: Success!");

            MetricDataActivity activity = (MetricDataActivity) getActivity();

            activity.setUpMetricData(metricDataList);
        }

        @Override
        public void onFailure(Exception e) {
            Timber.d("Metric data :: Failure...");
        }
    }
}

@@ -1402,16 +1402,45 @@
 ctions;%0A
+import java.util.Comparator;%0A
 import j
@@ -1454,16 +1454,16 @@
 l.Date;%0A
-
 import j
@@ -3067,32 +3067,73 @@
 tricDataList) %7B%0A
+        sortMetricData(metricDataList);%0A%0A
         List%3CPoi
@@ -4553,32 +4553,176 @@
 gress();%0A    %7D%0A%0A
+    private void sortMetricData(List%3CMetricData%3E metricDataList) %7B%0A        Collections.sort(metricDataList, new MetricDataComparator());%0A    %7D%0A%0A
     private void
@@ -5562,24 +5562,24 @@
 eption e) %7B%0A
-
             
@@ -5632,11 +5632,579 @@
  %7D%0A    %7D
+%0A%0A    private static final class MetricDataComparator implements Comparator%3CMetricData%3E %7B%0A        @Override%0A        public int compare(MetricData leftMetricData, MetricData rightMetricData) %7B%0A            long leftTimestamp = leftMetricData.getTimestamp();%0A            long rightTimestamp = rightMetricData.getTimestamp();%0A%0A            if (leftTimestamp == rightTimestamp) %7B%0A                return 0;%0A            %7D%0A%0A            if (leftTimestamp %3C rightTimestamp) %7B%0A                return -1;%0A            %7D else %7B%0A                return 1;%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Add proper sorting for metric data.