using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GridResizeProblem
{
    public delegate void CloseVisualizationHandler(object o, EventArgs e);

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<GridElement> Data { get; set; }

        public MainWindow()
        {
            Data = new ObservableCollection<GridElement>
                       {
                           new GridElement { FlowElementType = "P", Id = Guid.NewGuid(), ReceivedTimestamp = DateTime.Now, Type = "type 12345"},
                           new GridElement { FlowElementType = "P", Id = Guid.NewGuid(), ReceivedTimestamp = DateTime.Now - TimeSpan.FromHours(1), Type = "type 12345"},
                           new GridElement { FlowElementType = "O", Id = Guid.NewGuid(), ReceivedTimestamp = DateTime.Now - TimeSpan.FromHours(2), Type = "type 34ede32212"},
                           new GridElement { FlowElementType = "P", Id = Guid.NewGuid(), ReceivedTimestamp = DateTime.Now - TimeSpan.FromHours(3), Type = "type 333333333333333333"},
                           new GridElement { FlowElementType = "O", Id = Guid.NewGuid(), ReceivedTimestamp = DateTime.Now - TimeSpan.FromHours(4), Type = "type 1sss2345"},
                       };
            InitializeComponent();

            CurrentStateGrid.ItemsSource = Data;
        }

        #region CloseVisualization

        public event CloseVisualizationHandler CloseVisualization;

        protected void Close(object sender, RoutedEventArgs routedEventArgs)
        {
            if (CloseVisualization != null)
                CloseVisualization(this, routedEventArgs);
        }

        #endregion 

        #region popup options area

        private bool _disableMouseOverLogAreaTracking = false;

        private void MouseOverLogArea(object sender, MouseEventArgs e)
        {
            Debug.WriteLine("Mouse over grid");
            if (_disableMouseOverLogAreaTracking)
                return;

            optionsArea.Visibility = Visibility.Visible;
            _disableMouseOverLogAreaTracking = true;

        }

        private void MouseLeaveLogArea(object sender, MouseEventArgs e)
        {
            if (_disableMouseOverLogAreaTracking)
                return;

            if (optionsArea.Visibility == Visibility.Visible)
            {
                optionsArea.Visibility = Visibility.Hidden;
            }
        }

        private void MouseLeaveOptionsArea(object sender, MouseEventArgs e)
        {
            //Debug.WriteLine("MouseLeaveOptionsArea");
            optionsArea.Visibility = Visibility.Hidden;
            _disableMouseOverLogAreaTracking = false;
        }

        #endregion

        #region Grid font size

        private void FontSizeChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            foreach (var column in CurrentStateGrid.Columns.OfType<Telerik.Windows.Controls.GridViewColumn>())
            {
                column.Width = Telerik.Windows.Controls.GridViewLength.Auto;
                column.Width = Telerik.Windows.Controls.GridViewLength.SizeToCells;
            }
        }

        #endregion
    }

    public class GridElement
    {
        public string FlowElementType { get; set; }
        public DateTime ReceivedTimestamp { get; set; }
        public Guid Id { get; set; }
        public string Type { get; set; }
    }
}
