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.Animation;
using System.Windows.Shapes;
using System.Reflection;
using C1.C1Zip;
using C1.Silverlight.Data;
using C1.Silverlight.Olap;

namespace CustomUI
{
    public partial class MainPage : UserControl
    {
        C1OlapPanel _olapPanel = new C1OlapPanel();

        public MainPage()
        {
            InitializeComponent();
            
            var ds = new DataSet();
            var asm = Assembly.GetExecutingAssembly();
            using (var s = asm.GetManifestResourceStream("CustomUI.Resources.nwind.zip"))
            {
                var zip = new C1ZipFile(s);
                using (var zr = zip.Entries[0].OpenReader())
                {
                    // load data
                    ds.ReadXml(zr);
                }
            }

            // bind olap grid/chart to panel
            _olapChart.DataSource = _olapPanel;
            _olapGrid.DataSource = _olapPanel;

            // bind olap panel to data
            _olapPanel.DataSource = ds.Tables[0].DefaultView;

            // start with the SalesPerson view, all products
            _btnSalesperson_Click(this, null);
            _btnAllPrices_Click(this, null);
        }

        //------------------------------------------------------------------------
        #region ** handle clicks on view buttons

        void _btnSalesperson_Click(object sender, RoutedEventArgs e)
        {
            BuildView("SalesPerson");
        }
        void _btnProduct_Click(object sender, RoutedEventArgs e)
        {
            BuildView("ProductName");
        }
        void _btnCountry_Click(object sender, RoutedEventArgs e)
        {
            BuildView("Country");
        }

        #endregion

        //------------------------------------------------------------------------
        #region ** handle clicks on price filter buttons

        void _btnExpensive_Click(object sender, RoutedEventArgs e)
        {
            SetPriceFilter("Expensive Products (price > $50)", 50, double.MaxValue);
        }
        void _btnModerate_Click(object sender, RoutedEventArgs e)
        {
            SetPriceFilter("Moderately Priced Products ($20 < price < $50)", 20, 50);
        }
        void _btnInexpensive_Click(object sender, RoutedEventArgs e)
        {
            SetPriceFilter("Inexpensive Products (price < $20)", 0, 20);
        }
        void _btnAllPrices_Click(object sender, RoutedEventArgs e)
        {
            SetPriceFilter("All Products", 0, double.MaxValue);
        }
        #endregion

        //------------------------------------------------------------------------
        #region ** update chart when user sorts the grid

        void _olapGrid_SortedColumn(object sender, C1.Silverlight.FlexGrid.CellRangeEventArgs e)
        {
            _olapChart.UpdateChart();
        }

        #endregion

        //------------------------------------------------------------------------
        #region ** implementation

        // rebuild the view after a button was clicked
        void BuildView(string fieldName)
        {
            // get olap engine
            var olap = _olapPanel.OlapEngine;

            // stop updating until done
            olap.BeginUpdate();

            // clear all fields
            olap.RowFields.Clear();
            olap.ColumnFields.Clear();
            olap.ValueFields.Clear();

            // format order dates to group by year
            var f = olap.Fields["OrderDate"];
            f.Format = "yyyy";

            // build up view
            olap.ColumnFields.Add("OrderDate");
            olap.RowFields.Add(fieldName);
            olap.ValueFields.Add("ExtendedPrice");

            // restore updates
            olap.EndUpdate();
        }

        // apply a filter to the product price
        void SetPriceFilter(string footerText, double min, double max)
        {
            // get olap engine
            var olap = _olapPanel.OlapEngine;

            // stop updating until done
            olap.BeginUpdate();

            // make sure unit price field is active in the view
            var field = olap.Fields["UnitPrice"];
            olap.FilterFields.Add(field);

            // customize the filter
            var filter = field.Filter;
            filter.Clear();
            filter.Condition1.Operator = C1.Olap.ConditionOperator.GreaterThanOrEqualTo;
            filter.Condition1.Parameter = min;
            filter.Condition2.Operator = C1.Olap.ConditionOperator.LessThanOrEqualTo;
            filter.Condition2.Parameter = max;

            // restore updates
            olap.EndUpdate();
        }

        #endregion

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

    }
}
