using C1.Xaml.Maps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace ControlExplorer.Controls.Maps
{
    public enum Location
    {
        USA,
        Japan
    }

    /// <summary>
    /// Interaction logic for MapShape.xaml
    /// </summary>
    public partial class MapShape : UserControl, IExposeApplicationBarItems
    {
        Dictionary<Location, string> Locations = new Dictionary<Location, string>();
        List<AppBarButton> _menuItems = new List<AppBarButton>();
        public MapShape()
        {
            InitializeComponent();
            InitialMenuItems();
            this.Loaded += MapShape_Loaded;
        }

        void InitialMenuItems()
        { 
            if (Locations.Count == 0)
            {
                Locations.Add(Location.USA, "USA");
                Locations.Add(Location.Japan, "Japan");
            }
            if (_menuItems.Count == 0)
            {
                foreach (var location in Locations)
                {
                    var mItem = new AppBarButton()
                    {
                        Label = location.Value
                    };
                    mItem.Click += mItem_Click;
                    _menuItems.Add(mItem);
                }
            }
        }

        void mItem_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = (AppBarButton)sender;
            int index = _menuItems.IndexOf(menuItem);
            Location location = Locations.Keys.ElementAt(index);
            InitialMapsByLocaltion(maps, location);
        }
        void MapShape_Loaded(object sender, RoutedEventArgs e)
        {
            maps.Source = null;
            InitialMapsByLocaltion(maps, Location.USA);
        }

        void ProcessMap(C1VectorItemBase vector, C1ShapeAttributes attribute, Location location)
        {
            InitialVectorItemBase(vector, attribute, location);
        }

        void InitialMapsByLocaltion(C1Maps maps, Location location)
        {
            C1VectorLayer layer = null;
            if (maps.Layers.Count != 0)
            {
                layer = maps.Layers[0] as C1VectorLayer;
                layer.Children.Clear();
            }
            else
            {
                layer = new C1VectorLayer();
                maps.Layers.Add(layer);
            }
            
            Point center = new Point();
            double zoom = 0;
            switch (location)
            {
                case Location.USA:
                    Utils.LoadShapeFromResource(layer, new Uri("ms-appx:///Resources/Maps/states.shp"), new Uri("ms-appx:///Resources/Maps/states.dbf"),
               location, true, ProcessMap);
                    center = new Point(-115, 50);
                    zoom = 2;
                    break;
                case Location.Japan:
                    Utils.LoadShapeFromResource(layer, new Uri("ms-appx:///Resources/Maps/jp_toku_kuni_pgn.shp"), new Uri("ms-appx:///Resources/Maps/jp_toku_kuni_pgn.dbf"),
                location, true, ProcessMap);
                    center = new Point(135, 37);
                    zoom = 4;
                    break;
            }
            
            maps.MinZoom = zoom;
            maps.Zoom = zoom;
            maps.TargetCenter = center;
        }

        void InitialVectorItemBase(C1VectorItemBase vector, C1ShapeAttributes attribute, Location location)
        {
            vector.Stroke = new SolidColorBrush(Colors.LightGray);
            switch (location)
            {
                case Location.USA:
                    vector.Fill = new SolidColorBrush(Colors.Purple);
                    break;
                case Location.Japan:
                    vector.Fill = new SolidColorBrush(Colors.Brown);
                    break;
            }
        }

        #region IExposeApplicationBarItems Members
        public IEnumerable<ICommandBarElement> ApplicationBarItems
        {
            get
            {
                return _menuItems;
            }
        }

        #endregion
    }
}
