package com.example.mountaintravel.kind;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.esri.android.map.MapView;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polyline;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.CompositeSymbol;
import com.esri.core.symbol.FontStyle;
import com.esri.core.symbol.PictureMarkerSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.TextSymbol;
import com.esri.core.symbol.TextSymbol.HorizontalAlignment;
import com.esri.core.symbol.TextSymbol.VerticalAlignment;
import com.smart.gtstrvl.R;

public class AddGraphic {
	private MapView mapView;
	Context context;
	FindDbData dbData;
	@SuppressWarnings("unused")
	private Graphic line;
	private SimpleMarkerSymbol simpleMarker;
	@SuppressWarnings("unused")
	private SimpleLineSymbol lineSymbol;
	@SuppressWarnings("unused")
	private Polyline lGeometry;
	@SuppressWarnings("unused")
	private TextSymbol txtSymbol;
	PictureMarkerSymbol pictureSymbol;
	private Graphic graphic;
	private int densityDPI;

	public AddGraphic(MapView mapView, Context context) {
		super();
		this.mapView = mapView;
		this.context = context;
		// 获取屏幕密度（方法2）
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		densityDPI = dm.densityDpi;
		init();
	}

	public void init() {
		dbData = new FindDbData(context);
		simpleMarker = new SimpleMarkerSymbol(Color.RED, 15,
				SimpleMarkerSymbol.STYLE.CIRCLE);
		lineSymbol = new SimpleLineSymbol(Color.RED, 4,
				SimpleLineSymbol.STYLE.SOLID);
		lGeometry = new Polyline();
		// pictureSymbol=new PictureMarkerSymbol(drawable);
	}

	/**
	 * 添加点
	 * 
	 * @return Graphic
	 */

	public Graphic addPoint(double x, double y) {
		Point mapPoint = latAndlonToMapPoint(x, y);
		graphic = new Graphic(mapPoint, simpleMarker);
		return graphic;

	}
	
	/**
	 * 添加点
	 * 
	 * @return Graphic
	 */

	public Graphic addPoint2(double x, double y,Drawable pic) {
		Point mapPoint = latAndlonToMapPoint(x, y);
		pictureSymbol = new PictureMarkerSymbol(pic);
		graphic = new Graphic(mapPoint, pictureSymbol);
		return graphic;

	}
	public Graphic onDrawLine(List<Point> list) {
//		Point StartPoint =latAndlonToMapPoint(120.20809962400006, 30.37493997600006);
//		Point Point2 =latAndlonToMapPoint(120.2060060550001, 30.37786469100007);
//		Point Point3 =latAndlonToMapPoint(120.20469497200008, 30.380854722000038);
//		Point Point4 =latAndlonToMapPoint(120.2075420970001, 30.378920324000035);
		SimpleLineSymbol lSymbol = new SimpleLineSymbol(Color.RED, 8,SimpleLineSymbol.STYLE.SOLID);
		Polyline lGeometry = new Polyline();
		for (int i = 0; i < list.size(); i++) {
			if (i==0) {
				lGeometry.startPath(list.get(i));//起始点
			}else {
				lGeometry.lineTo(list.get(i));
			}
		}
//		lGeometry.startPath(StartPoint);//起始点
//		lGeometry.lineTo(Point3);//其余的点，可写循环
//		lGeometry.lineTo(Point2);
//		lGeometry.lineTo(Point4);
		Graphic graphic = new Graphic(lGeometry, lSymbol);
		return graphic;
	}
	public Point  setPoint(Double x,Double y){
		Point point =latAndlonToMapPoint(x, y);
		return point;
	}
	/**
	 * 添加线
	 * 
	 * @return Graphic
	 */
	public Graphic addLine(String str) {
		// 打开数据库输出流
		Point start_mapPoint = null;
		SimpleLineSymbol lSymbol = new SimpleLineSymbol(Color.RED, 3,
				SimpleLineSymbol.STYLE.SOLID);
		Polyline lGeometry = new Polyline();
		Cursor cursor = dbData.db.rawQuery(
				"select * from Point where RoadCount=?", new String[] { str });
		if (cursor.moveToFirst()) {
			String start_x = cursor.getString(cursor.getColumnIndex("point_x"));
			String start_y = cursor.getString(cursor.getColumnIndex("point_y"));
			Point point = new Point(Double.parseDouble(start_x),
					Double.parseDouble(start_y));
			start_mapPoint = (Point) GeometryEngine.project(point,
					SpatialReference.create(4326),
					mapView.getSpatialReference());
		}
		
		lGeometry.startPath(start_mapPoint);
		while (cursor.moveToNext()) {
			String x = cursor.getString(cursor.getColumnIndex("point_x"));
			String y = cursor.getString(cursor.getColumnIndex("point_y"));
			Point mapPoint = latAndlonToMapPoint(Double.parseDouble(x),
					Double.parseDouble(y));
			lGeometry.lineTo(mapPoint);
			start_mapPoint = mapPoint;
		}
		Graphic graphic = new Graphic(lGeometry, lSymbol);
		cursor.close();
		return graphic;

	}

	/**
	 * 添加图片
	 * 
	 * @return Graphic
	 */

	public Graphic addPicture(Drawable pic, Map<String, Object> attributes) {
		double x = Double.parseDouble((String) attributes.get("x"));
		double y = Double.parseDouble((String) attributes.get("y"));
		pictureSymbol = new PictureMarkerSymbol(pic);
		Point mapPoint = latAndlonToMapPoint(x, y);
		Graphic graphic = new Graphic(mapPoint, pictureSymbol, attributes);
		return graphic;
	}

	/**
	 * 添加文字
	 * 
	 * @return Graphic
	 */
	public Graphic addText(double x, double y,String name) {
		TextDrawable text = new TextDrawable(context);
		text.setText(name);
		text.setTextColor(context.getResources().getColor(R.color.text_color));
		text.setTextSize(textSize(densityDPI));
		text.setTextAlign();
		int drawableWidth = text.getMinimumWidth();
		float width = drawableWidth / 2;
		Point mapPoint = latAndlonToMapPoint(x, y);
		PictureMarkerSymbol pic = new PictureMarkerSymbol(text);
		pic.setOffsetX(width + 15.0f);
		pic.setOffsetY(5.0f);
		Graphic graphic = new Graphic(mapPoint, pic);
		return graphic;
	}

	/**
	 * 地理坐标转投影坐标
	 */
	public Point latAndlonToMapPoint(double x, double y) {
		Point point = new Point(x, y);
		Point mapPoint = (Point) GeometryEngine.project(point,
				SpatialReference.create(4326), mapView.getSpatialReference());
		return mapPoint;
	}

	public Graphic AddCompositeSymbol(Map<String, Object> attributes,
			Drawable drawable) {
		double x = Double.parseDouble((String) attributes.get("x"));
		double y = Double.parseDouble((String) attributes.get("y"));
		String UnComeCount = (String) attributes.get("UnComeCount");
		String name = (String) attributes.get("name");
		TextDrawable d = new TextDrawable(context);
		d.setText(name);
		d.setTextColor(context.getResources().getColor(R.color.trailRoadColor));
		d.setTextSize(textSize(densityDPI));
		d.setTextAlign();
		TextDrawable d2 = new TextDrawable(context);
		d2.setText(context.getResources().getString(R.string.UnComeCount)
				+ UnComeCount);
		d2.setTextColor(context.getResources().getColor(R.color.trailRoadColor));
		d2.setTextSize(textSize(densityDPI));
		d2.setTextAlign();
		int drawableWidth = d.getMinimumWidth();
		float width = drawableWidth / 2;
		int drawableWidth2 = d2.getMinimumWidth();
		float width2 = drawableWidth2 / 2;
		PictureMarkerSymbol pic = new PictureMarkerSymbol(drawable);
		PictureMarkerSymbol picName = new PictureMarkerSymbol(d);
		PictureMarkerSymbol picUncome = new PictureMarkerSymbol(d2);
		picName.setOffsetX(width + 12.0f);
		picName.setOffsetY(7.0f);
		picUncome.setOffsetX(width2 + 12.0f);
		picUncome.setOffsetY(-7.0f);
		CompositeSymbol compositeSymbolPolygon = new CompositeSymbol();
		compositeSymbolPolygon.add(pic);
		compositeSymbolPolygon.add(picName);
		compositeSymbolPolygon.add(picUncome);
		Point mapPoint = latAndlonToMapPoint(x, y);
		Graphic graphic = new Graphic(mapPoint, compositeSymbolPolygon,
				attributes);
		return graphic;
	}

	public Graphic AddCompositeSymbol2(Map<String, Object> attributes,
			Drawable drawable) {
		String name = (String) attributes.get("name");
		double x = Double.parseDouble((String) attributes.get("x"));
		double y = Double.parseDouble((String) attributes.get("y"));
		TextDrawable d = new TextDrawable(context);
		d.setText(name);
		d.setTextColor(Color.BLACK);
		d.setTextSize(textSize(densityDPI));
		d.setTextAlign();
		int drawableWidth = d.getIntrinsicWidth();
		float width = drawableWidth / 2;
		PictureMarkerSymbol pic = new PictureMarkerSymbol(drawable);
		PictureMarkerSymbol picName = new PictureMarkerSymbol(d);
		picName.setOffsetX(width + 12.0f);
		picName.setOffsetY(6.0f);
		// System.out.println("getIntrinsicWidth  width=: "+width+"\t drawableWidth=: "+drawableWidth);
		/*
		 * TextSymbol tes = new TextSymbol(10, name, Color.BLACK);
		 * tes.setFontFamily("DroidSansFallback.ttf");
		 * tes.setHorizontalAlignment(HorizontalAlignment.LEFT);
		 * tes.setVerticalAlignment(VerticalAlignment.MIDDLE);
		 * tes.setOffsetX(12.0f); tes.setOffsetY(3.0f);
		 */
		CompositeSymbol compositeSymbolPolygon = new CompositeSymbol();
		compositeSymbolPolygon.add(pic);
		compositeSymbolPolygon.add(picName);
		Point mapPoint = latAndlonToMapPoint(x, y);
		Graphic graphic = new Graphic(mapPoint, compositeSymbolPolygon,
				attributes);
		return graphic;
	}

	public float textSize(int dpi) {
		float size = 0;
		// System.out.println(dpi+"dpi****");
		if (dpi <= 240) {
			size = 8.0f;
		} else if (240 < dpi && dpi <= 320) {
			size = 6.0f;
		} else if (320 < dpi && dpi <= 480) {
			size = 4.5f;
		} else if (480 < dpi && dpi <= 640) {
			size = 3.0f;
		}
		return size;
	}
}
