package com.android.MAKER;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MAKER2 extends MapActivity {
/** Called when the activity is first created. */
private LocationManager location;
protected MapView mapView;
private MapController mc;
public MyLocationOverlay myLocationOverlay;
public LocationListener locationListener;
public String[] mylocation = null;
private Itemoverlay items;
private DataSet dataset;
private CalledInfo call;
private LocationInfo locationInfo;
private GeoCode geocode;
protected LocationInfoOverlay locInfo;
private List<Overlay> listOverlays;
private Criteria criteria;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dataset = new DataSet();
call = new CalledInfo(this);
// locationInfo = new LocationInfo(dataset);
// geocode = new GeoCode(this, call, locationInfo);
// Thread cthread = new Thread(new TCPclient(dataset));
// cthread.start();
// try {
// Thread.sleep(50);
// } catch (InterruptedException e1) {
// // TODO Auto-generated catch block
// Log.e(e1.toString(),"어떤 에러?");
// e1.printStackTrace();
// }
//
// while (cthread.isAlive()) {
//
// if (dataset.getTel_arr().size() != 0) {
// locationInfo = new LocationInfo(dataset);
// geocode = new GeoCode(MAKER2.this, call, locationInfo);
try {
Community();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Viewmap();
MyLocation();
// }
// cthread.destroy();
// break;
// }
// locationInfo = new LocationInfo(dataset);
// geocode = new GeoCode(MAKER2.this, call, locationInfo);

Viewmap();
MyLocation();

// MyLocation();

}

public void Community() throws InterruptedException {
// TCPclient tcp = new TCPclient(gps);

Thread cthread = new Thread(new TCPclient(dataset));
cthread.start();
Thread.sleep(50);
while (cthread.isAlive()) {

if (dataset.getTel_arr().size() != 0) {
locationInfo = new LocationInfo(dataset);
geocode = new GeoCode(MAKER2.this, call, locationInfo);
// Viewmap();
// MyLocation();
return;
}
}

// tcp.run();
// cthread.setPriority(1);
Log.e(geocode.getGps_list().toString(), "bbbbbbbbbbbbb");
// cthread.stop();

// TCP

// cthread.destroy();

// cthread.stop();

// tcp.TC(gps);

}

public void Viewmap() {

// try {
// geopoint_set.set_GeoPoint();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// Log.d("Exception", "Message : " + e.getMessage());
// }

mapView = (MapView) findViewById(R.id.map);// 맵뷰 사용
// Drawable locmarker = getResources().getDrawable(R.drawable.arrow);
// Drawable bollon = getResources().getDrawable(
// R.drawable.balloon_empty_new);

// Drawable mymarker = getResources().getDrawable(R.drawable.icon);

mapView.setBuiltInZoomControls(true);// 줌을 가능하게 해줌
mc = mapView.getController();// 맵컨트롤러를 가져옴
mc.setZoom(17);
mapView.invalidate();
// tcp.run();
}

public void MyLocation() {
location = (LocationManager) getSystemService(LOCATION_SERVICE);
criteria = new Criteria();

final TextView gps = (TextView) findViewById(R.id.Text);// 위치변화시 텍스트로
// 보여줌
locationListener = new LocationListener() {

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

gps.setText("lat:" + location.getLatitude() + ", logi:"// 위치변화시
// 현재좌표
// 표시
+ location.getLongitude());
updateOverlay(location);

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub

}

};
String best = location.getBestProvider(criteria, true);
Toast t = Toast.makeText(getApplicationContext(), best,
Toast.LENGTH_LONG);
t.setGravity(Gravity.BOTTOM, 0, -50);
t.show();
criteria.setAccuracy(Criteria.ACCURACY_FINE);

// location.requestLocationUpdates(location.GPS_PROVIDER, 0, 0,
// locationListener);// 에뮬 상황에서 GPS만 사용하기 위한 것
/*********** 장치에만 적용되는 것 *********/
location.requestLocationUpdates(best, 3, 100, locationListener);
Location curloc = location.getLastKnownLocation(best);
updateOverlay(curloc);
/******************************************************/
/* Mylocation을 이용한 내위치 표시 */

// myLocationOverlay = new MyLocationOverlay(this, mapView);
//
// // mapView.getOverlays().add(myLocationOverlay);
// myLocationOverlay.enableCompass();
// myLocationOverlay.enableMyLocation();
//
// myLocationOverlay.runOnFirstFix(new Runnable() {
// public void run() {
// mc.animateTo(myLocationOverlay.getMyLocation());
// // tcp.run();
//
// }
// });
mapView.invalidate();
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

protected void updateOverlay(Location location) {
listOverlays = mapView.getOverlays();
if (listOverlays.size() > 0) {
listOverlays.clear();
}
GeoPoint geoPoint = getGeoPoint(location);
// Drawable marker;
Drawable locmarker = getResources().getDrawable(R.drawable.home);
// marker = getResources().getDrawable(R.drawable.makerhuman);
// marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
// .getIntrinsicHeight());
items = new Itemoverlay(locmarker);
int temp = geocode.getGps_list().size();
List<Overlay> overlay = mapView.getOverlays();
// Iterator<Integer> itr = geocode.getGps_list().iterator();
if (temp != 0) {
for (int i = 0; i < temp; i += 2) {
GeoPoint p = new GeoPoint(geocode.getGps_list().get(i), geocode
.getGps_list().get(i + 1));
OverlayItem overlayItem = new OverlayItem(p, "Here", null);
items.addOverlay(overlayItem);
overlay.add(items);
items.mpopulate();
}
}
// myLocationOverlay = new MyLocationOverlay(this, mapView);
//
// // mapView.getOverlays().add(myLocationOverlay);
// myLocationOverlay.enableCompass();
// myLocationOverlay.enableMyLocation();
//
// myLocationOverlay.runOnFirstFix(new Runnable() {
// public void run() {
// mc.animateTo(myLocationOverlay.getMyLocation());
// // tcp.run();
//
// }
// });
mapView.invalidate();


}

private GeoPoint getGeoPoint(Location location) {
if (location == null) {
return null;
}
Double lat = location.getLatitude() * 1E6;
Double lng = location.getLongitude() * 1E6;
return new GeoPoint(lat.intValue(), lng.intValue());
}
// /**********************//////
/*
* class MapOverlay extends Overlay {
* @Override public boolean draw(Canvas canvas, MapView mapView, boolean
* shadow, long when) { super.draw(canvas, mapView, shadow);
* // ---translate the GeoPoint to screen pixels--- Point screenPts = new
* Point(); mapView.getProjection().toPixels(p, screenPts);
* // ---add the marker--- Bitmap bmp =
* BitmapFactory.decodeResource(getResources(),
* R.drawable.balloon_empty_new); canvas.drawBitmap(bmp, screenPts.x-120,
* screenPts.y-130, null); return true; }
* public boolean onTouchEvent(MotionEvent event, MapView mapView) { //
* ---when user lifts his finger--- // ---when user lifts his finger--- if
* (event.getAction() == 1) { p = mapView.getProjection().fromPixels( (int)
* event.getX(), (int) event.getY());
* Geocoder geoCoder = new Geocoder(getBaseContext(), Locale .getDefault());
* try { List<Address> addresses = geoCoder
* .getFromLocation(p.getLatitudeE6() / 1E6, p .getLongitudeE6() / 1E6, 1);
* String add = ""; if (addresses.size() > 0) { for (int i = 0; i <
* addresses.get(0) .getMaxAddressLineIndex(); i++) add +=
* addresses.get(0).getAddressLine(i) + "\n";
* Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show(); }
* } catch (IOException e) { e.printStackTrace(); } return true; } else
* return false; }
*/
}
/***********************************************/