package com.camera.simplemjpeg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

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

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

//import android.os.AsyncTask;



import android.app.Activity;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Switch;


import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;


import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import java.io.InputStream;
import java.net.URL;
import java.io.*;
import java.net.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.ActionBar;
import android.util.Log;

import java.net.InetAddress;

public class gpio extends Activity{
	
	Button cmd;
	TextView resultat;
 
	public void onCreate(Bundle savedInstanceState) 
	{
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gpio);
		
		ActionBar actionBar = getActionBar();
		actionBar.show();
		
		cmd=(Button) findViewById(R.id.button_cmd);
		resultat=(TextView) findViewById(R.id.text_resultat);

		
		cmd.setOnClickListener(new OnClickListener() 
		{
		
			public void onClick(View v)
			{
				new CommandTask().execute("tag_allume_led");

				
			}
		});
		
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu)
	{
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.items, menu);
    	return super.onCreateOptionsMenu(menu);
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	
	    	super.onOptionsItemSelected(item);
	    	
	    	switch(item.getItemId()){
		    	
		    	
	    	case R.id.GPIO:
	    		Toast.makeText(getBaseContext(), "Selection GPIO", Toast.LENGTH_SHORT).show();
	    		Intent gpioIntent = new Intent(this,gpio.class);
	            startActivity(gpioIntent);
	    		break;
	    		
	    	case R.id.musique:
	    		Toast.makeText(getBaseContext(), "Selection du player", Toast.LENGTH_SHORT).show();
	    		Intent playerIntent = new Intent(this,mediaplayer.class);
	            startActivity(playerIntent);
	    		break;
	    		
	    				    		
	    	case R.id.camera:
	    		Toast.makeText(getBaseContext(), "Selection Camera", Toast.LENGTH_SHORT).show();
	    		 Intent productIntent = new Intent(this,MjpegActivity.class);
	             startActivity(productIntent);
	         // return true;
	    		break;
	    		
	    	case R.id.fichier:
	    		Toast.makeText(getBaseContext(), "Selection du browser", Toast.LENGTH_SHORT).show();
	    		//Intent browserIntent = new Intent(this,listing_fichiers.class);
	            //startActivity(browserIntent);
	    		break;	
	    		
		    		
	    	}
	    	
	    	return true;
    	
    }	
    
    class CommandTask extends AsyncTask<String, Integer, String>
    {

		@Override
		protected String doInBackground(String... params)
		{
			
			String res = PostData(params);
			return res;
		}
		
		@Override
		protected void onPostExecute(String result) 
		{
			//progressBar.setVisibility(View.GONE);
			resultat.setText(result);
	
		}
    	
    }
/*
    class ExecuteTask extends AsyncTask<String, Integer, String>
    {

		@Override
		protected String doInBackground(String... params)
		{
			
			String res=PostData(params);
			return res;
		}
		
		@Override
		protected void onPostExecute(String result) 
		{
			//progressBar.setVisibility(View.GONE);
			resultat.setText(result);
			Toast.makeText(getApplicationContext(), result, 3000).show();
		}
    	
    }
*/
    public String PostData(String[] valeur)
    {	
    	String s="";
		try
		{
			HttpClient httpClient=new DefaultHttpClient();
			HttpPost httpPost=new HttpPost("http://82.234.53.234//test.php");
			//HttpPost httpPost=new HttpPost("http://analog-design.net/WORKAREA/test.php");
			
			
			int l = valeur.length;
			String str = "+++++++" + Integer.toString(l) + "+++++++++";
			System.out.println(str);
			
			List<NameValuePair> list=new ArrayList<NameValuePair>();
			list.add(new BasicNameValuePair("valeur", valeur[0]));
			
			//list.add(new BasicNameValuePair("name", valeur[0]));
			//list.add(new BasicNameValuePair("pass",valeur[1]));
			httpPost.setEntity(new UrlEncodedFormEntity(list));
		    HttpResponse httpResponse=	httpClient.execute(httpPost);
		
		    HttpEntity httpEntity=httpResponse.getEntity();
		    s= readResponse(httpResponse);
	
		}

		catch(Exception exception) 	{}
		
		return s;

    }

    public String readResponse(HttpResponse res) 
	{
		InputStream is=null; 
		String return_text="";
		try 
		{
			is=res.getEntity().getContent();
			BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
			String line="";
			StringBuffer sb=new StringBuffer();
			while ((line=bufferedReader.readLine())!=null)
			{
				sb.append(line);
			}
			return_text=sb.toString();
		} 
		
		catch (Exception e)
		{
			
		}
		
		return return_text;
		
	}

	
	

}