package com.chernowii.hero4;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class HDR extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hdr);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_hdr, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void sendHDROne (View view){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/mode?p=1");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/21/1");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/0");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "First picture taken!", Toast.LENGTH_SHORT).show();
      try {
            Thread.sleep(1900);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/4");
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "Second picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(1900);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/8");
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "Third picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Vibrator v = (Vibrator)getSystemService(VIBRATOR_SERVICE);
        v.vibrate(600);
        Toast.makeText(getApplicationContext(),
                "HDR FINISHED!", Toast.LENGTH_LONG).show();
    }
    public void sendHDRTwo (View view){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/mode?p=1");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/21/1");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/1");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "First picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(1900);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/4");
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "Second picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(1900);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/7");
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "Third picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Vibrator v = (Vibrator)getSystemService(VIBRATOR_SERVICE);
        v.vibrate(600);
        Toast.makeText(getApplicationContext(),
                "HDR FINISHED!", Toast.LENGTH_LONG).show();
    }
    public void sendHDRThree (View view){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/mode?p=1");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/21/1");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/2");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "First picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(1900);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/4");
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "Second picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(1900);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/6");
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "Third picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(1900);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Vibrator v = (Vibrator)getSystemService(VIBRATOR_SERVICE);
        v.vibrate(600);
        Toast.makeText(getApplicationContext(),
                "HDR FINISHED!", Toast.LENGTH_LONG).show();
    }
    public void sendHDRFour (View view){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/mode?p=1");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/21/1");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/3");
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "First picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/4");
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "Second picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/setting/26/5");
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new HttpAsyncTask().execute("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
        Toast.makeText(getApplicationContext(),
                "Third picture taken!", Toast.LENGTH_SHORT).show();
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Vibrator v = (Vibrator)getSystemService(VIBRATOR_SERVICE);
        v.vibrate(600);
        Toast.makeText(getApplicationContext(),
                "HDR FINISHED!", Toast.LENGTH_LONG).show();
    }
    public static String GET(String url){
        InputStream inputStream = null;
        String result = "";
        try {

            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert inputstream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        return result;
    }

    private static String convertInputStreamToString(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;

    }

    public boolean isConnected(){
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected())
            return true;
        else
            return false;
    }
    private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            return GET(urls[0]);
        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(getBaseContext(), "Done!", Toast.LENGTH_SHORT).show();
            ;
        }
    }
}