/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dhaval.android.appstat;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.dhaval.android.appstat.model.Stats;
import com.dhaval.android.appstat.service.IdentityManager;
import com.dhaval.android.appstat.service.LocationManager;
import com.dhaval.android.appstat.service.ProcessManager;
import com.dhaval.android.appstat.service.StatsUpdateManager;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

/**
 *
 * @author dhaval
 */
public class AppStats extends Activity {

    private ProcessManager processManager;
    private LocationManager locationManager;
    private IdentityManager identityManager;
    private StatsUpdateManager statsUpdateManager;
    private TextView msg;
    private MenuItem sync;
    private MenuItem settings;
    private MenuItem about;
    private int updateTimer = 1000 * 60;
    private MenuItem start;

    public AppStats() {
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        try {
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.VERTICAL);
            ll.setPadding(10, 10, 10, 10);

            msg = new TextView(this);
            ll.addView(msg);
            msg.setText("log messages");

            processManager = new ProcessManager(this);

            List<com.dhaval.android.appstat.model.Process> processes = processManager.getAllProcesses();

            for (com.dhaval.android.appstat.model.Process proc : processes) {
                LinearLayout entry = new LinearLayout(this);
                entry.setOrientation(LinearLayout.VERTICAL);

                TextView tv = new TextView(this);
                tv.setText(proc.getLabel());
                tv.setTextSize(28);

                if (proc.isRunning()) {
                    tv.setTextColor(Color.GREEN);
                } else {
                    tv.setTextColor(Color.RED);
                }

                entry.addView(tv);

                TextView tv1 = new TextView(this);
                tv1.setText(proc.getName());
                entry.addView(tv1);

                ll.addView(entry);
            }

            ScrollView sv = new ScrollView(this);
            sv.addView(ll);
            setContentView(sv);

            locationManager = new LocationManager(this);
            identityManager = new IdentityManager(this);
            statsUpdateManager = new StatsUpdateManager(this);

            StatsUpdateTimer timer = new StatsUpdateTimer();
//            timer.execute();
        } catch (Exception e) {
            msg.setText(e.getMessage());
        }
    }

    private Drawable loadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            msg.setText("image Error: " + e.getMessage());
            return null;
        }
    }

    private class StatsUpdateTimer extends AsyncTask<Integer, String, Integer> {

        @Override
        protected void onProgressUpdate(String... values) {
//            Toast.makeText(AppStats.this, values[0], Toast.LENGTH_SHORT);
            msg.setText(values[0]);
        }

        @Override
        protected Integer doInBackground(Integer... arg0) {
            while (true) {
                try {
                    publishProgress("response: " + updateStats());
                    Thread.sleep(updateTimer);
                } catch (Exception e) {
                    publishProgress("Error: " + e.getMessage());
                }
            }
        }
    }

    private String updateStats() {
        Stats stats = new Stats(identityManager.getIdentity(), locationManager.getLocation(), processManager.getRunningProcesses());
        String response = statsUpdateManager.update(stats);
        return response;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        start = menu.add("Start Sync");
        sync = menu.add("Force Sync");
        settings = menu.add("Settings");
        about = menu.add("About");
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if (item == sync) {
            msg.setText(updateStats());
        } else if (settings == item) {
            Intent intent = new Intent(this, Prefs.class);
            startActivity(intent);
        }
        return true;
    }
}
