package com.ericsson.uecontrol.gui.fragments;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.ericsson.uecontrol.R;
import com.ericsson.uecontrol.core.UeControlExecutor;
import com.ericsson.uecontrol.core.util.ThroughputCalculator;
import com.ericsson.uecontrol.core.util.CSVWriter;
import com.ericsson.uecontrol.core.util.StringUtil;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle;
import com.jjoe64.graphview.GraphViewStyle;
import com.jjoe64.graphview.LineGraphView;


/**
 * Created by ezivkoc on 2014-07-15.
 */
public class StatusFragment extends Fragment {
    private ImageView down_img;
    private TextView down_speed;
    private ImageView up_img;
    private TextView up_speed;
    private TextView rat_type;

    private static GraphViewSeries downGraph;
    private static GraphViewSeries upGraph;
    private static int x = 0;

    private Handler handler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        handler = new Handler();

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_status, container, false);

        down_img = (ImageView) view.findViewById(R.id.down_img);
        down_speed = (TextView) view.findViewById(R.id.down_speed);
        up_img = (ImageView) view.findViewById(R.id.up_img);
        up_speed = (TextView) view.findViewById(R.id.up_speed);
        rat_type = (TextView) view.findViewById(R.id.rat_type);

        // Init the graph
        if(downGraph == null)
            downGraph = new GraphViewSeries("Download Throughput", new GraphViewSeriesStyle(Color.GREEN, 3),
                    new GraphViewData[]{new GraphViewData(0, 0)});
        if(upGraph == null)
            upGraph = new GraphViewSeries("Upload Throughput", new GraphViewSeriesStyle(Color.RED, 3),
                    new GraphViewData[]{new GraphViewData(0, 0)});

        LineGraphView graphView = new LineGraphView(this.getActivity(), "GraphView");
        graphView.addSeries(downGraph);
        graphView.addSeries(upGraph);
        graphView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        graphView.getGraphViewStyle().setGridStyle(GraphViewStyle.GridStyle.BOTH);
        graphView.setShowVerticalLabels(false);
        //graphView.setDrawDataPoints(true);
        graphView.setViewPort(0, 120);
        graphView.setScrollable(true);
        graphView.setDisableTouch(true);
        graphView.scrollToEnd();

        LinearLayout layout = (LinearLayout) view.findViewById(R.id.graph);
        layout.addView(graphView);

        return view;
    }


    private UeControlExecutor.ThroughputListener throughputListener =
            new UeControlExecutor.ThroughputListener() {
                public void throughputUpdate(final double downThroughput, final double upThroughput) {
                    if(down_speed != null && up_speed != null)
                        down_speed.post(new Runnable() {
                            @Override
                            public void run() {
                                down_speed.setText(StringUtil.getBitThroughputString(downThroughput));
                                up_speed.setText(StringUtil.getBitThroughputString(upThroughput));
                                rat_type.setText(CSVWriter.getRat());

                                downGraph.appendData(new GraphViewData(x, downThroughput), true, 120);
                                upGraph.appendData(new GraphViewData(x, upThroughput), true, 120);
                                x++;

                                ExecNotification.update(downThroughput, upThroughput);
                                startActivityTimer();
                            }
                        });
                }
            };
    public UeControlExecutor.ThroughputListener getThroughputListener(){
        return throughputListener;
    }

    private void startActivityTimer(){
        // Set active images
        if(down_img != null)
            down_img.setImageResource(R.drawable.stat_download);
        if(up_img != null)
            up_img.setImageResource(R.drawable.stat_upload);

        // Start reset timer
        if(handler != null) {
            handler.removeMessages(0);
            handler.postDelayed(new Runnable() {
                public void run() {
                    if (down_img != null)
                        down_img.setImageResource(R.drawable.stat_download_static);
                    if (up_img != null)
                        up_img.setImageResource(R.drawable.stat_upload_static);
                }
            }, 2000);
        }
    }

    public void reset(){
        if(downGraph != null)
            downGraph.resetData(new GraphViewData[]{new GraphViewData(0, 0)});
        if(upGraph != null)
            upGraph.resetData(new GraphViewData[]{new GraphViewData(0, 0)});
        x = 0;

        down_speed.post(new Runnable() {
            @Override
            public void run() {
                down_speed.setText(StringUtil.getBitThroughputString(0));
                up_speed.setText(StringUtil.getBitThroughputString(0));
            }
        });
    }

}