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 C1.Silverlight;
using System.IO.IsolatedStorage;
using System.Reflection;

namespace Localization
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Language = System.Windows.Markup.XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentCulture.Name);
            C1Window.AddDefaultPopupToVisualTree(LayoutRoot);

            try
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains("tabIndex"))
                {
                    var storedValue = IsolatedStorageSettings.ApplicationSettings["tabIndex"];
                    if (storedValue != null)
                    {
                        int index = 0;
                        if (int.TryParse(storedValue.ToString(), out index))
                        {
                            tabControl.SelectedIndex = index;
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Please, open Silverlight settings and enable Application Storage.");
            }


            listMsgBoxOptions.ItemsSource = typeof(C1MessageBoxButton).GetEnumValues<C1MessageBoxButton>();
            SelectMessageBoxType(C1MessageBoxButton.OK);

            flexGrid.ItemsSource = Person.Generate(100);
            dataGrid.ItemsSource = Person.Generate(100);

            olap.Loaded += (s, ea) =>
            {
                olap.DataSource = Person.Generate(100);
                olap.OlapEngine.RowFields.Add("Name", "Residence");
                olap.OlapEngine.ValueFields.Add("Age");
            };

            var asmName = new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name;
            var uri = new Uri(string.Format("/{0};component/C1XAPOptimizer.pdf", asmName), UriKind.Relative);
            var resource = Application.GetResourceStream(uri);
            pdfViewer.LoadDocument(resource.Stream);

            resource = Application.GetResourceStream(uri);
            reportViewer.LoadDocument(resource.Stream);

            rtb.Html = "This is a simple <b>HTML</b> text <i>sample</i>.";
        }


        C1MessageBoxButton _selectedButton;

        private void btnOpenMsgBox_Click(object sender, RoutedEventArgs e)
        {
            C1MessageBox.Show("", "", _selectedButton);
        }
        
        private void listMsgBoxOptions_ItemClick(object sender, SourcedEventArgs e)
        {
            var menuItem = (C1MenuItem)e.Source;
            var selection = (C1MessageBoxButton)menuItem.Header;

            SelectMessageBoxType(selection);

            C1MessageBox.Show("", "", _selectedButton);
        }

        private void SelectMessageBoxType(C1MessageBoxButton selection)
        {
            _selectedButton = selection;
            btnOpenMsgBox.Header = selection.ToString().Replace("C1.Silverlight.", "");
        }

        private void C1TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tabControl == null)
                return;

            try
            {
                IsolatedStorageSettings.ApplicationSettings["tabIndex"] = tabControl.SelectedIndex;
            }
            catch
            {
                MessageBox.Show("Please, open Silverlight settings and enable Application Storage.");
            }
        }
    }
}
