package com.example.upload01;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

 

public class listing_backup extends Activity 
{
	 
    String myJSON;
   
    ListView list;
    
    private static final String TAG_RESULTS="result";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_ADD ="address";
 
    JSONArray peoples = null;
 
    ArrayList<HashMap<String, String>> personList;
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listing_backup);
        list = (ListView) findViewById(R.id.listView);
        personList = new ArrayList<HashMap<String,String>>();
        
        list.setOnItemClickListener(new ListClickHandler());
        
        getData();
       
    }
    
    
    public class ListClickHandler implements OnItemClickListener{
   	 
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3)
        {
//             Intent data = new Intent();
//             data.putExtra("valeur_retour", personList.get(position).get(TAG_NAME));
             // Activity finished ok, return the data
//             setResult(RESULT_OK, data);
//             finish();
        	 
        	//String toto = personList.get(position).get(TAG_NAME);
        	
           //  Toast.makeText(listing_backup.this, toto , Toast.LENGTH_SHORT).show();
             
             Intent data = new Intent();
             data.putExtra("valeur_retour", personList.get(position).get(TAG_NAME));
             setResult(RESULT_OK, data);
             finish();
        	 
             
        }
         
    }
    
    protected void showList()
    {
    	
        try 
        {
        	
        	 Toast.makeText(this, "Chargement des backups en cours ... ", Toast.LENGTH_SHORT).show();
            	JSONObject jsonObj = new JSONObject(myJSON);
            	peoples = jsonObj.getJSONArray(TAG_RESULTS);
 
            	for(int i=0;i<peoples.length();i++)
            	{
            		JSONObject c = peoples.getJSONObject(i);
            		String id = c.getString(TAG_ID);
            		String name = c.getString(TAG_NAME);
            		String address = c.getString(TAG_ADD);
 
            		HashMap<String,String> persons = new HashMap<String,String>();
 
            		persons.put(TAG_ID,id);
            		persons.put(TAG_NAME,name);
            		persons.put(TAG_ADD,address);
 
            		personList.add(persons);
            	}
 
            	ListAdapter adapter = new SimpleAdapter
            			(
            					listing_backup.this, personList, R.layout.list_item,
            					new String[]{TAG_ID,TAG_NAME,TAG_ADD},
            					new int[]{R.id.id, R.id.name, R.id.address}
            					);
 
            	list.setAdapter(adapter);
            	
            	
          	  List<String> myList = new ArrayList<String>();   
//            	File liste[] = file.listFiles();
//
//                for( int i=0; i< liste.length; i++)
//                {
//                	if(liste[i].isFile())
//                	{
//                		myList.add( liste[i].getName() );
//                	}	
//                }
            	
 
        } 
        catch (JSONException e) 
        {
            e.printStackTrace();
        }
 
    	/*
    	String[] array = myJSON.split(","); 
    	
    	String[] items = { "Milk", "Butter", "Yogurt", "Toothpaste", "Ice Cream" };
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
       
        list.setAdapter(adapter);
    	*/
    	
    }
 
    public void getData()
    {
        class GetDataJSON extends AsyncTask<String, Void, String>
        {
 
            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            	HttpPost httppost = new HttpPost("http://analog-design.net/WORKAREA/list_file_in_folder.php");
                
                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");
 
                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
 
                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();
 
                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }
 
            @Override
            protected void onPostExecute(String result)
            {
                myJSON=result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }
 
   
}
    
    