package com.eyekabob;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class FindMusic extends EyekabobActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.findmusicactivity);

        EditText findByArtist = (EditText)findViewById(R.id.findByArtistInput);
        findByArtist.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    ((ImageButton)findViewById(R.id.findByArtistButton)).performClick();
                }
                return false;
            }
        });
        EditText findByVenue = (EditText)findViewById(R.id.findByVenueInput);
        findByVenue.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    ((ImageButton)findViewById(R.id.findByVenueButton)).performClick();
                }
                return false;
            }
        });
        findByArtist.requestFocus();

        SeekBar distance = (SeekBar)findViewById(R.id.milesSeekBar);
        distance.setProgress(10);
        distance.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {}
			public void onStartTrackingTouch(SeekBar seekBar) {}

			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				TextView miles = (TextView)findViewById(R.id.milesTextView);

				if (progress < 1) {
					progress = 1;
					SeekBar distance = (SeekBar)findViewById(R.id.milesSeekBar);
					distance.setProgress(progress);
				}

				miles.setText(Integer.toString(progress));
			}
		});
    }

    public void findByArtistHandler(View v) {
        EditText findByArtist = (EditText)findViewById(R.id.findByArtistInput);
        find("artist.search", ArtistResults.class, "artist", findByArtist.getText().toString());
    }

    public void findByVenueHandler(View v) {
        EditText findByVenue = (EditText)findViewById(R.id.findByVenueInput);
        find("venue.search", VenueResults.class, "venue", findByVenue.getText().toString());
    }

    public void findByLocationHandler(View v) {
    	Map<String, String> params = new HashMap<String, String>();

    	SeekBar distance = (SeekBar)findViewById(R.id.milesSeekBar);
    	params.put("distance", Integer.toString(distance.getProgress()));

    	CheckBox useCurrentLocationInput = (CheckBox)findViewById(R.id.useCurrentLocationCheckBox);
    	EditText locationInput = (EditText)findViewById(R.id.findByLocationInput);
    	if (useCurrentLocationInput.isChecked()) {
    		LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    		// Gets the last known location without actually asking the service.
    		// So if another app got location a while ago, this will just use that location.
    		Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
    		if (location != null) {
	    		params.put("lat", Double.toString(location.getLatitude()));
	    		params.put("long", Double.toString(location.getLongitude()));
    		}
    	}
    	else if (!"".equals(locationInput.getText().toString().trim())) {
    		String criterion = locationInput.getText().toString().trim();
			Pattern pattern = Pattern.compile("^\\d{5}(-\\d{4})?$");
			Matcher matcher = pattern.matcher(criterion);
			if (matcher.find()) {
				findByZip(criterion, params.get("distance"));
				return;
			}

    		params.put("location", criterion);
    	}
    	else {
    		// Nothing entered and not using current location.
    		return;
    	}

    	find("geo.getEvents", EventResults.class, params);
    }

    public void useCurrentLocationHandler(View v) {
    	EditText location = (EditText)findViewById(R.id.findByLocationInput);
    	location.setEnabled(!((CheckBox)v).isChecked());
    }

    private void findByZip(String zip, String distance) {
    	Uri uri = EyekabobHelper.GeoNames.getUri(zip);
    	Intent intent = new Intent(getApplicationContext(), EventResults.class);
    	intent.setData(uri);
    	intent.putExtra("zip", zip);
    	intent.putExtra("distance", distance);
    	startActivity(intent);
    }

    private void find(String restAPI, Class<?> intentClass, Map<String, String> params) {
        Uri uri = EyekabobHelper.LastFM.getUri(restAPI, params);
        Intent intent = new Intent(getApplicationContext(), intentClass);
        intent.setData(uri);
        startActivity(intent);
    }

    private void find(String restAPI, Class<?> intentClass, String paramKey, String paramValue) {
        if ("".equals(paramValue)) {
        	return;
        }

        Map<String, String> params = null;
        if (paramKey != null) {
        	params = new HashMap<String, String>();
            params.put(paramKey, URLEncoder.encode(paramValue));
        }

        find(restAPI, intentClass, params);
    }

    public void adHandler(View v) {
    	Intent emailIntent = new Intent(Intent.ACTION_SEND);
    	emailIntent.setType("plain/text");
    	String to[] = {"create@philipjordandesign.com", "christopherrobertfarrow@gmail.com", "coffbr01@gmail.com"};
    	emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
    	emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Eyekabob Advertising");
    	startActivity(Intent.createChooser(emailIntent, "Write email with:"));
    }
}

@@ -3146,24 +3146,104 @@
 esSeekBar);%0A
+    %09int miles = distance.getProgress();%0A    %09int km = (int)(miles / 1.609344);%0A
     %09params.
@@ -3275,38 +3275,18 @@
 oString(
-distance.getProgress()
+km
 ));%0A%0A   

convert miles to km for the last.fm geo query