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 C1.C1Zip;
using System.ComponentModel;
using System.IO;

namespace EsriMapSamples
{
    public partial class MainPage : UserControl
    {
        SolidColorBrush _whiteBrush = new SolidColorBrush(Colors.White);


        public MainPage()
        {
            InitializeComponent();

            iAddPins.Tag = new AddPins();
            iDragDrop.Tag = new DragDrop();
            //iFeatureDataGrid.Tag = new FeatureDataGrid();
            iClustering.Tag = new Clustering();
            iGeoRSS.Tag = new FlickerGeoRSS();


            foreach (C1TreeViewItem tvi in tv.Items)
                tvi.IsExpanded = true;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            SetSample(iAddPins.Tag as UserControl);
        }

        private void tv_ItemClick(object sender, SourcedEventArgs e)
        {
            UserControl child = null;
            C1TreeView tv = sender as C1TreeView;
            if (tv != null)
            {
                C1TreeViewItem tvi = e.Source as C1TreeViewItem;
                if (tvi != null && tvi.Tag is UserControl)
                    child = (UserControl)tvi.Tag;
                else
                {
                    tvi.IsSelected = false;
                    if (tvi.Items.Count > 0)
                    {
                        tvi = tvi.Items[0] as C1TreeViewItem;
                        tvi.IsSelected = true;
                        child = tvi.Tag as UserControl;
                    }
                }
                if (child != null)
                    child.Foreground = _whiteBrush;

                SetSample(child);
            }
        }

        void SetSample(UserControl sample)
        {
            bdr.Child = sample;
            DescriptionAttribute da = null;
            if (sample != null)
            {
                da = sample.GetType().GetCustomAttributes(typeof(DescriptionAttribute), false).
                  FirstOrDefault(o => o is DescriptionAttribute) as DescriptionAttribute;
            }

            if (da != null)
                ControlDescription.Text = da.Description;
            else
                ControlDescription.Text = "";

            // set source code and xaml
            SetSourceCode(sample);
        }

        private void SetSourceCode(UserControl sample)
        {
            var client = new WebClient();
            string sampleName = sample.GetType().ToString();
            sampleName = sampleName.Substring(sampleName.IndexOf('.') + 1);
            client.OpenReadAsync(Extensions.GetAbsoluteUri("SourceCode.zip")); // SourceCode.zip is built at run-time on the server
            client.OpenReadCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    C1ZipFile zip = new C1ZipFile();
                    zip.Open(e.Result);

                    // get xaml file
                    C1ZipEntry ze = zip.Entries[sampleName + ".xaml"];
                    if (ze != null)
                    {
                        Stream stream = ze.OpenReader();
                        StreamReader streamReader = new StreamReader(stream);
                        txtXaml.Text = streamReader.ReadToEnd();
                        streamReader.Close();
                        stream.Close();
                    }
                    else
                    {
                        txtXaml.Text = "Unavailable for this demo.";
                    }

                    // get code file
                    ze = zip.Entries[sampleName + ".xaml.cs"];
                    if (ze != null)
                    {
                        Stream stream = ze.OpenReader();
                        StreamReader streamReader = new StreamReader(stream);
                        txtCode.Text = streamReader.ReadToEnd();
                        streamReader.Close();
                        stream.Close();
                    }
                    else
                    {
                        txtCode.Text = "Unavailable for this demo.";
                    }
                }
                else
                {
                    C1MessageBox.Show("Couldn't download source code from server.", "Error", C1MessageBoxIcon.Error);
                }
            };
        }

        
    }

    public class ControlNode : C1TreeViewItem
    {
        public ControlNode()
        {
            //DefaultStyleKey = typeof(ControlNode);
            Foreground = new SolidColorBrush(Color.FromArgb(0xff, 0x92, 0xc8, 0xd8));
        }
    }
}
