using System;
using System.Drawing;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;

namespace FleeStyleSample
{
    public partial class TestForm : Form
    {
        private MapEngine mapEngine = new MapEngine();
        private Bitmap bitmap = null;

        public TestForm()
        {
            InitializeComponent();
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            // Set the full extent and the background color
            mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);
            mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.DeepOcean);

            // Highlight the countries that are land locked and have a population greater than 10 million
            string expression = "(ToInt32(POP_CNTRY)>10000000) AND (ToChar(LANDLOCKED)='Y')";
            FleeBooleanStyle landLockedCountryStyle = new FleeBooleanStyle(expression);

            // You can access the static methods on these types.  We use this
            // to access the Convert.Toxxx methods to convert variable types
            landLockedCountryStyle.StaticTypes.Add(typeof(System.Convert));
            // The math class might be handy to include but in this sample we do not use it
            //landLockedCountryStyle.StaticTypes.Add(typeof(System.Math));

            landLockedCountryStyle.ColumnVariables.Add("POP_CNTRY");
            landLockedCountryStyle.ColumnVariables.Add("LANDLOCKED");

            landLockedCountryStyle.CustomTrueStyles.Add(new AreaStyle(AreaStyles.Country1.OutlinePen, new GeoSolidBrush(GeoColor.SimpleColors.Yellow)));
            landLockedCountryStyle.CustomFalseStyles.Add(AreaStyles.Country1);

            ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"..\..\App_Data\Countries02.shp", ShapeFileReadWriteMode.ReadOnly);
            worldLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(landLockedCountryStyle);
            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

            // Add the new layers to the MapEngine
            mapEngine.StaticLayers.Add("WorldLayer", worldLayer);

            DrawImage();
        }

        private void DrawImage()
        {
            if (bitmap != null) { bitmap.Dispose(); }
            bitmap = new Bitmap(Map.Width, Map.Height);
            mapEngine.OpenAllLayers();
            mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
            mapEngine.CloseAllLayers();

            Map.Image = bitmap;
        }

        private void ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            switch (e.Button.Tag.ToString())
            {
                case "Zoom In":
                    mapEngine.CurrentExtent.ScaleDown(50);
                    break;
                case "Zoom Out":
                    mapEngine.CurrentExtent.ScaleUp(50);
                    break;
                case "Full Extent":
                    mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);
                    break;
                case "Pan Left":
                    mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20);
                    break;
                case "Pan Right":
                    mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);
                    break;
                case "Pan Up":
                    mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);
                    break;
                case "Pan Down":
                    mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20);
                    break;
                default:
                    break;
            }
            DrawImage();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
