package com.creative.GPSDemo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.creative.GPSDemo.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class Report extends Activity implements OnClickListener
{
    private TextView message02 = null;
	private TextView message03 = null;
    private EditText ed2;
    private EditText ed1;
    private Button sendBtn;
    private String uriAPI = "http://114.33.38.10/www/http1.php";
	private LocationManager LOCATIONManager;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        message02 = (EditText)findViewById(R.id.message02);
        message03 = (EditText)findViewById(R.id.message03);
        ed1 = (EditText) findViewById(R.id.ed1);
        ed2 = (EditText) findViewById(R.id.ed2);

        sendBtn = (Button) findViewById(R.id.button1);
 
        
        LOCATIONManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        
        // 獲得位置訊息
        locate();
 
        if (sendBtn != null)
        {
 
        	sendBtn.setOnClickListener(this);
        	 
 
        }
    }
    //@Override
    
    public void onClick(View v )
 
    {
 
        if (v == sendBtn)
 
        {

            String msg = null;
 
            if (ed1 != null&&ed2 != null)
 
            {
 
            	msg = ed1.getEditableText().toString();
            	msg = ed2.getEditableText().toString();
            	msg = message02.getEditableText().toString();
            	msg = message03.getEditableText().toString();

 
                String result  = sendPostDataToInternet(msg);
            
 
 
 
                // 印出網路回傳的文字
 
                if (result != null)
 
                    Toast.makeText(this, result, Toast.LENGTH_LONG).show();
                	
 
            }
          
 
        }
 
    }
 
  
 
    private String sendPostDataToInternet(String strTxt)
 
    {
 
        /* 建立HTTP Post連線 */
 
        HttpPost httpRequest = new HttpPost(uriAPI);
 
        /*
 
         * Post運作傳送變數必須用NameValuePair[]陣列儲存
 
         */
 
        List<NameValuePair> params = new ArrayList<NameValuePair>();
 
        params.add(new BasicNameValuePair("name", ed1.getText().toString()));
        params.add(new BasicNameValuePair("phone", ed2.getText().toString()));
        params.add(new BasicNameValuePair("lat", message02.getText().toString()));
        params.add(new BasicNameValuePair("lng", message03.getText().toString()));
 
        try
 
        {
 
            /* 發出HTTP request */
 
            httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
 
            /* 取得HTTP response */
 
            HttpResponse httpResponse = new DefaultHttpClient()
 
                    .execute(httpRequest);
 
            /* 若狀態碼為200 ok */
 
            if (httpResponse.getStatusLine().getStatusCode() == 200)
 
            {
 
                /* 取出回應字串 */
 
                String strResult = EntityUtils.toString(httpResponse
 
                        .getEntity());
 
 
 
                // 回傳回應字串
 
                return strResult;
 
            }
 
 
 
        } catch (ClientProtocolException e)
 
        {
 
            Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT)
 
                    .show();
 
            e.printStackTrace();
 
        } catch (IOException e)
 
        {
 
            Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT)
 
                    .show();
 
            e.printStackTrace();
 
        } catch (Exception e)
 
        {
 
            Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT)
 
                    .show();
 
            e.printStackTrace();
 
        }
 
        return null;
 
    }
 
 
 



    
    @SuppressLint("ParserError")
	private void locate()
    {	
    	StringBuilder builder = new StringBuilder("可用的providers:");
    	
    	LocationListener ll = new LocationListener(){

			public void onLocationChanged(Location location)
			{
			}

			public void onProviderDisabled(String provider)
			{
			}

			public void onProviderEnabled(String provider)
			{
			}

			public void onStatusChanged(String provider, int status,
					Bundle extras)
			{
			}
    	};
    	
			String provider = LOCATIONManager.GPS_PROVIDER;
				
			LOCATIONManager.requestLocationUpdates(provider, 0, 1000, ll);

	    	Location location = LOCATIONManager.getLastKnownLocation(provider);
	    		
	    	//if(location != null)
	    	//{
	    		double lat = location.getLatitude();   // 緯度
	    		double lng = location.getLongitude();  // 經度

	    		message02.setText( String.valueOf(lat));
	    		message03.setText( String.valueOf(lng));
	    	
	    
    //}
}
}