using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using C1.Silverlight;
using C1.Silverlight.DataGrid;
using System.ComponentModel.DataAnnotations;

namespace C1DataGrid_Demo2010
{
    public partial class Appearance : System.Windows.Controls.UserControl, INotifyPropertyChanged
    {
        public Appearance()
        {
            InitializeComponent();

            DataContext = Data.GetProducts((product) => !string.IsNullOrEmpty(product.Element("ProductModelID").Value));

            foreach (DictionaryEntry resource in Resources)
            {
                if (resource.Value is Style)
                {
                    cmbStyles.Items.Insert(0, resource.Key);
                }
            }
            cmbStyles.Items.Insert(0, "Default");
            cmbStyles.SelectedIndex = 1;
        }

        private void cmbStyles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            ClearBrushProperties();
            var style = Resources[cmbStyles.SelectedItem] as Style;
            grid.Style = style;
            FireNotifyPropertyChanged();
        }

        private void FireNotifyPropertyChanged()
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("DEMO_Background"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("DEMO_Foreground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("DEMO_BorderBrush"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("DEMO_BorderThickness"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("RowBackground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("AlternatingRowBackground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("RowForeground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("AlternatingRowForeground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("HorizontalGridLinesBrush"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("VerticalGridLinesBrush"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("SelectedBackground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("MouseOverBrush"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("PressedBrush"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ValidationBackground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ValidationForeground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("HeaderBackground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("HeaderForeground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("GroupingPanelBackground"));
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("GroupingPanelForeground"));
        }

        private void ClearBrushProperties()
        {
            grid.ClearValue(C1DataGrid.BackgroundProperty);
            grid.ClearValue(C1DataGrid.ForegroundProperty);
            grid.ClearValue(C1DataGrid.BorderBrushProperty);
            grid.ClearValue(C1DataGrid.BorderThicknessProperty);
            grid.ClearValue(C1DataGrid.RowBackgroundProperty);
            grid.ClearValue(C1DataGrid.AlternatingRowBackgroundProperty);
            grid.ClearValue(C1DataGrid.RowForegroundProperty);
            grid.ClearValue(C1DataGrid.AlternatingRowForegroundProperty);
            grid.ClearValue(C1DataGrid.HorizontalGridLinesBrushProperty);
            grid.ClearValue(C1DataGrid.VerticalGridLinesBrushProperty);
            grid.ClearValue(C1DataGrid.SelectedBackgroundProperty);
            grid.ClearValue(C1DataGrid.MouseOverBrushProperty);
            grid.ClearValue(C1DataGrid.ValidationBackgroundProperty);
            grid.ClearValue(C1DataGrid.ValidationForegroundProperty);
            grid.ClearValue(C1DataGrid.HeaderBackgroundProperty);
            grid.ClearValue(C1DataGrid.HeaderForegroundProperty);
            grid.ClearValue(C1DataGrid.GroupingPanelBackgroundProperty);
            grid.ClearValue(C1DataGrid.GroupingPanelForegroundProperty);
            grid.ClearValue(C1DataGrid.PressedBrushProperty);
        }

        private void grid_AutoGeneratingColumn(object sender, C1.Silverlight.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
        {
            Common.HandleColumnAutoGeneration(e);
            if (e.Property.Name == "ImageUrl")
            {
                e.Cancel = true;
            }
        }

        private void grid_AutoGeneratedColumns(object sender, EventArgs e)
        {
            grid.GroupBy(grid.Columns["Name"]);
            grid.FilterBy(grid.Columns["Name"], new DataGridFilterState() { FilterInfo = new List<DataGridFilterInfo>(new DataGridFilterInfo[1] { new DataGridFilterInfo() { FilterOperation = DataGridFilterOperation.Contains, FilterType = DataGridFilterType.Text, Value = "W" } }) });
        }

        public Brush DEMO_Background
        {
            get
            {
                return grid.Background;
            }
            set
            {
                grid.Background = value;
            }
        }

        public Brush DEMO_Foreground
        {
            get
            {
                return grid.Foreground;
            }
            set
            {
                grid.Foreground = value;
            }
        }

        public Brush DEMO_BorderBrush
        {
            get
            {
                return grid.BorderBrush;
            }
            set
            {
                grid.BorderBrush = value;
            }
        }

        public Thickness DEMO_BorderThickness
        {
            get
            {
                return grid.BorderThickness;
            }
            set
            {
                grid.BorderThickness = value;
            }
        }

        public Brush RowBackground
        {
            get
            {
                return grid.RowBackground;
            }
            set
            {
                grid.RowBackground = value;
            }
        }

        public Brush AlternatingRowBackground
        {
            get
            {
                return grid.AlternatingRowBackground;
            }
            set
            {
                grid.AlternatingRowBackground = value;
            }
        }

        public Brush RowForeground
        {
            get
            {
                return grid.RowForeground;
            }
            set
            {
                grid.RowForeground = value;
            }
        }

        public Brush AlternatingRowForeground
        {
            get
            {
                return grid.AlternatingRowForeground;
            }
            set
            {
                grid.AlternatingRowForeground = value;
            }
        }

        public Brush VerticalGridLinesBrush
        {
            get
            {
                return grid.VerticalGridLinesBrush;
            }
            set
            {
                grid.VerticalGridLinesBrush = value;
            }
        }

        public Brush HorizontalGridLinesBrush
        {
            get
            {
                return grid.HorizontalGridLinesBrush;
            }
            set
            {
                grid.HorizontalGridLinesBrush = value;
            }
        }

        public DataGridGridLinesVisibility GridLinesVisibility
        {
            get
            {
                return grid.GridLinesVisibility;
            }
            set
            {
                grid.GridLinesVisibility = value;
            }
        }

        public Brush SelectedBackground
        {
            get
            {
                return grid.SelectedBackground;
            }
            set
            {
                grid.SelectedBackground = value;
            }
        }

        public Brush PressedBrush
        {
            get
            {
                return grid.PressedBrush;
            }
            set
            {
                grid.PressedBrush = value;
            }
        }

        public Brush MouseOverBrush
        {
            get
            {
                return grid.MouseOverBrush;
            }
            set
            {
                grid.MouseOverBrush = value;
            }
        }

        public Brush ValidationBackground
        {
            get
            {
                return grid.ValidationBackground;
            }
            set
            {
                grid.ValidationBackground = value;
            }
        }

        public Brush ValidationForeground
        {
            get
            {
                return grid.ValidationForeground;
            }
            set
            {
                grid.ValidationForeground = value;
            }
        }

        public Brush HeaderBackground
        {
            get
            {
                return grid.HeaderBackground;
            }
            set
            {
                grid.HeaderBackground = value;
            }
        }

        public Brush HeaderForeground
        {
            get
            {
                return grid.HeaderForeground;
            }
            set
            {
                grid.HeaderForeground = value;
            }
        }

        public Brush GroupingPanelBackground
        {
            get
            {
                return grid.GroupingPanelBackground;
            }
            set
            {
                grid.GroupingPanelBackground = value;
            }
        }

        public Brush GroupingPanelForeground
        {
            get
            {
                return grid.GroupingPanelForeground;
            }
            set
            {
                grid.GroupingPanelForeground = value;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void btnExportStyle_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string style = string.Format(@"<Style x:Key=""MyGridStyle"" TargetType=""c1dg:C1DataGrid"">
    <Setter Property=""Background"" Value=""{0}""/>
    <Setter Property=""BorderBrush"" Value=""{1}""/>
    <Setter Property=""HorizontalGridLinesBrush"" Value=""{2}""/>
    <Setter Property=""MouseOverBrush"" Value=""{3}""/>
    <Setter Property=""PressedBrush"" Value=""{11}""/>
    <Setter Property=""RowBackground"" Value=""{4}""/>
    <Setter Property=""AlternatingRowBackground"" Value=""{5}""/>
    <Setter Property=""SelectedBackground"" Value=""{6}""/>
    <Setter Property=""VerticalGridLinesBrush"" Value=""{7}""/>
    <Setter Property=""Foreground"" Value=""{8}""/>
    <Setter Property=""RowForeground"" Value=""{9}""/>
    <Setter Property=""AlternatingRowForeground"" Value=""{10}""/>
    <Setter Property=""ValidationBackground"" Value=""{12}""/>
    <Setter Property=""ValidationForeground"" Value=""{13}""/>
    <Setter Property=""HeaderBackground"" Value=""{14}""/>
    <Setter Property=""HeaderForeground"" Value=""{15}""/>
    <Setter Property=""GroupingPanelBackground"" Value=""{16}""/>
    <Setter Property=""GroupingPanelForeground"" Value=""{17}""/>
</Style>",
             (grid.Background as SolidColorBrush).Color,
             (grid.BorderBrush as SolidColorBrush).Color,
             (grid.HorizontalGridLinesBrush as SolidColorBrush).Color,
             (grid.MouseOverBrush as SolidColorBrush).Color,
             (grid.RowBackground as SolidColorBrush).Color,
             (grid.AlternatingRowBackground as SolidColorBrush).Color,
             (grid.SelectedBackground as SolidColorBrush).Color,
             (grid.VerticalGridLinesBrush as SolidColorBrush).Color,
             (grid.Foreground as SolidColorBrush).Color,
             (grid.RowForeground as SolidColorBrush).Color,
             (grid.AlternatingRowForeground as SolidColorBrush).Color,
             (grid.PressedBrush as SolidColorBrush).Color,
             (grid.ValidationBackground as SolidColorBrush).Color,
             (grid.ValidationForeground as SolidColorBrush).Color,
             (grid.HeaderBackground as SolidColorBrush).Color,
             (grid.HeaderForeground as SolidColorBrush).Color,
             (grid.GroupingPanelBackground as SolidColorBrush).Color,
             (grid.GroupingPanelForeground as SolidColorBrush).Color);
                var window = new C1Window();
                window.Content = new System.Windows.Controls.TextBox() { Text = style, FontSize = 16 };
                window.ShowModal();
                window.CenterOnScreen();
            }
            catch
            {
            }
        }
    }
}
