using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml;
using C1.Silverlight;
using C1.Silverlight.Data;
using C1.Silverlight.Olap;
using C1.C1Zip;

namespace CustomizePage
{
    public partial class MainPage : UserControl
    {
        const string VIEWDEF_KEY = "C1OlapViewDefinition";

        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // load data from embedded zip resource
            var ds = new DataSet();
            var asm = Assembly.GetExecutingAssembly();
            using (var s = asm.GetManifestResourceStream("CustomizePage.Resources.nwind.zip"))
            {
                var zip = new C1ZipFile(s);
                using (var zr = zip.Entries[0].OpenReader())
                {
                    // load data
                    ds.ReadXml(zr);
                }
            }

            // bind olap page to data
            _c1OlapPage.DataSource = ds.Tables[0].DefaultView;

            // initialize olap view
            var userSettings = IsolatedStorageSettings.ApplicationSettings;
            if (userSettings.Contains(VIEWDEF_KEY))
            {
                // load last used olap view from isolated storage
                _c1OlapPage.ViewDefinition = userSettings[VIEWDEF_KEY] as string;
            }
            else
            {
                // view not found in storage, use default
                var olap = _c1OlapPage.OlapPanel.OlapEngine;
                olap.DataSource = ds.Tables[0].DefaultView;
                olap.BeginUpdate();
                olap.RowFields.Add("Country");
                olap.ColumnFields.Add("Category");
                olap.ValueFields.Add("Sales");
                olap.Fields["Sales"].Format = "n0";
                olap.EndUpdate();
            }

            // save the current view to storage when closing the app
            Application.Current.Exit += Current_Exit;

            // get predefined views from XML resource
            var views = new Dictionary<string, string>();
            using (var s = asm.GetManifestResourceStream("CustomizePage.Resources.OlapViews.xml"))
            using (var reader = XmlReader.Create(s))
            {
                // read predefined view definitions
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "C1OlapPage")
                    {
                        var id = reader.GetAttribute("id");
                        var def = reader.ReadOuterXml();
                        views[id] = def;
                    }
                }
            }

            // build new menu with predefined views
            var menuViews = new C1MenuItem();
            menuViews.Header = "View";
            menuViews.Icon = GetImage("Resources/views.png");
            menuViews.VerticalAlignment = VerticalAlignment.Center;
            ToolTipService.SetToolTip(menuViews, "Select a predefined Olap view.");
            foreach (var id in views.Keys)
            {
                var mi = new C1MenuItem();
                mi.Header = id;
                mi.Tag = views[id];
                mi.Click += mi_Click;
                menuViews.Items.Add(mi);
            }

            // add new menu to the page's main menu
            _c1OlapPage.MainMenu.Items.Insert(6, menuViews);
        }

        // apply a predefined view
        void mi_Click(object sender, SourcedEventArgs e)
        {
            var mi = sender as C1MenuItem;
            var viewDef = mi.Tag as string;
            _c1OlapPage.ViewDefinition = viewDef;
        }

        // save the current view to storage when closing the app
        void Current_Exit(object sender, EventArgs e)
        {
            var userSettings = IsolatedStorageSettings.ApplicationSettings;
            userSettings[VIEWDEF_KEY] = _c1OlapPage.ViewDefinition;
            userSettings.Save();
        }

        // regenerate the olap view
        void Button_Click(object sender, RoutedEventArgs e)
        {
            _c1OlapPage.OlapPanel.OlapEngine.Update();
        }

        // utility to load an image from a URI
        static Image GetImage(string name)
        {
            var uri = new Uri(name, UriKind.Relative);
            var img = new Image();
            img.Source = new BitmapImage(uri);
            img.Stretch = Stretch.None;
            img.VerticalAlignment = VerticalAlignment.Center;
            img.HorizontalAlignment = HorizontalAlignment.Center;
            return img;
        }
    }
}
