using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

using MindFusion.Common.WinForms;
using MindFusion.Mapping;
using MindFusion.Mapping.WinForms;


namespace Markers
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			// show zoom widget
			var zoomControl = new ZoomControl();
			zoomControl.Height = 300;
			zoomControl.Width = 55;
			zoomControl.Location = new System.Drawing.Point(
				mapView.Bounds.Right - 75, mapView.Bounds.Y + 40);
			zoomControl.Target = mapView;
			zoomControl.Anchor = AnchorStyles.Right | AnchorStyles.Top;
			zoomControl.ScrollStep = -20;
			Controls.Add(zoomControl);
			zoomControl.BringToFront();

			// load the world map
			mapView.BaseMap = Map.FromFile(FindMap("ne_50m_admin_0_countries.shp"));

			var baseLayer = mapView.Layers[0];
			baseLayer.EnableHighlight = false;

			// select fill colors so that adjacent countries are painted in different color
			baseLayer.FillColors[0] = Color.DarkSeaGreen;
			baseLayer.LineColor = Color.LightGray;

			// add a second layer to display decoration objects
			var decorationLayer = new DecorationLayer(mapView.BaseMap.BoundingBox);
			mapView.Layers.Add(decorationLayer);

			// define some landmark data
			var landmarks = new[]
			{
				new
				{
					Image = "bigben100.png",
					Latitude = Map.Latitude(51, 32, 'N'),
					Longitude = Map.Longitude(0, -5, 'W'),
					Label = "Big Ben\nLondon",
					ImageX = 50, ImageY = 90
				},

				new
				{
					Image = "christ_redeemer.png",
					Latitude = Map.Latitude(22, 57, 'S'),
					Longitude = Map.Longitude(43, 12, 'W'),
					Label = "Christ the Redeemer\nRio de Janeiro",
					ImageX = 50, ImageY = 100
				},

				new
				{
					Image = "colosseum100.png",
					Latitude = Map.Latitude(41, 54, 'N'),
					Longitude = Map.Longitude(12, 27, 'E'),
					Label = "Coliseum\nRome",
					ImageX = 50, ImageY = 70
				},

				new
				{
					Image = "egypt100.png",
					Latitude = Map.Latitude(29, 58, 'N'),
					Longitude = Map.Longitude(31, 9, 'E'),
					Label = "Great Pyramid\nGiza",
					ImageX = 50, ImageY = 60
				},

				new
				{
					Image = "eiffel100.png",
					Latitude = Map.Latitude(48, 48, 'N'),
					Longitude = Map.Longitude(2, 20, 'E'),
					Label = "Eiffel Tower\nParis",
					ImageX = 50, ImageY = 80
				},

				new
				{
					Image = "liberty100.png",
					Latitude = Map.Latitude(40, 40.8, 'N'),
					Longitude = Map.Longitude(74, 0.2, 'W'),
					Label = "Statue of Liberty\nNew York",
					ImageX = 70, ImageY = 90
				},

				new
				{
					Image = "mayan_pyramid.png",
					Latitude = Map.Latitude(19, 41, 'N'),
					Longitude = Map.Longitude(98, 50, 'W'),
					Label = "Mayan Pyramids\nTeotihuacan",
					ImageX = 50, ImageY = 90
				},

				new
				{
					Image = "sydney_opera.png",
					Latitude = Map.Latitude(34, 0, 'S'),
					Longitude = Map.Longitude(151, 0, 'E'),
					Label = "Sydney Opera House\nSydney",
					ImageX = 70, ImageY = 90
				},

				new
				{
					Image = "tajmahal100.png",
					Latitude = Map.Latitude(27, 11, 'N'),
					Longitude = Map.Longitude(78, 1, 'E'),
					Label = "Taj Mahal\nAgra",
					ImageX = 50, ImageY = 70
				}
			};

            // create decoration images
            foreach (var landmark in landmarks)
            {
                var img = new DecorationImage(
                    Image.FromFile(@"Images\" + landmark.Image),
                    landmark.ImageX, landmark.ImageY,
                    landmark.Latitude, landmark.Longitude);
                img.Label = new MindFusion.Mapping.Label(landmark.Label);
                decorationLayer.Decorations.Add(img);
            }

            var office = new DecorationBubble(
                6, Color.SteelBlue, Color.White,
                42 + 40.0 / 60, 23 + 20.0 / 60) { Label = new MindFusion.Mapping.Label("MindFusion\nSofia") };
            decorationLayer.Decorations.Add(office);

            mapView.ZoomFactor *= 6;
            mapView.ScrollTo(office.Latitude, office.Longitude);
			mapView.Focus();
		}

		private void miFileLoadMap_Click(object sender, EventArgs e)
		{
			var openDialog = new OpenFileDialog
			{
				Filter = "Shapefile map|*.shp",
				Title = "Open map"
			};

			if (openDialog.ShowDialog() == DialogResult.OK)
			{
				mapView.BaseMap = new Map();
				mapView.BaseMap.LoadFromFile(openDialog.FileName);
				mapView.Invalidate();
			}
		}

		private void miFileExit_Click(object sender, EventArgs e)
		{
			Close();
		}

		private string FindMap(string fileName)
		{
			string testDir = @"..\Maps\50m_cultural\";
			for (int i = 0; i < 10; i++)
			{
				if (Directory.Exists(testDir))
					break;
				testDir = @"..\" + testDir;
			}

			var testFile = testDir + fileName;
			if (File.Exists(testFile))
				return testFile;

			return "";
		}
	}
}