using System;
using System.Collections.Generic;
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;
using BeWo.ServiceProxy;
using BS.Shared.Extensions;
using System.Windows.Threading;
using BeWo.Core;

namespace BeWo.View.Report.MitarbeiterKoelnRing
{
    public partial class FLSOverviewGrid : UserControl
    {
        public FLSOverviewDetailRow DetailRow
        {
            get { return (FLSOverviewDetailRow)GetValue(DetailRowProperty); }
            set { SetValue(DetailRowProperty, value); }
        }

        public static readonly DependencyProperty DetailRowProperty =
            DependencyProperty.Register("DetailRow",
                                        typeof(FLSOverviewDetailRow), 
                                        typeof(FLSOverviewGrid),
                                        new UIPropertyMetadata(new FLSOverviewDetailRow(DetailRowTypes.Header), new PropertyChangedCallback(RowChanged)));

        public FLSOverviewGrid()
        {
            InitializeComponent();
            
        }



        private static void RowChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            FLSOverviewGrid item = (FLSOverviewGrid)obj;
            FLSOverviewDetailRow row = (FLSOverviewDetailRow)e.NewValue;
            item.RefreshGrid(row);
            
        }

        public void RefreshGrid(FLSOverviewDetailRow row) 
        {
            
            Grid grid = detailRowGrid;
            
            grid.RowDefinitions.Add(new RowDefinition());

            // Employee Column
            if (row.ShowServiceRecords && row.ShowOnlyTotalRows)
                grid.ColumnDefinitions.Add(CreateColumnDefinition(175));
            else
                grid.ColumnDefinitions.Add(CreateColumnDefinition(190));
            Label lbl;

            lbl = new Label();
            if (row.DetailRowType != DetailRowTypes.Detail)
                lbl.FontWeight = FontWeights.Bold;
            lbl.SetValue(Grid.ColumnProperty, 0);
            if (row.ShowServiceRecords && row.ShowOnlyTotalRows && row.DetailRowType == DetailRowTypes.Header)
                lbl.Content = "Datum";
            else
                lbl.Content = row.GroupName;
            grid.Children.Add(lbl);


            if (row.ShowServiceRecords)
            {
                //grid.Margin = new Thickness(0, 0, 8, 0);
                if (row.ShowServiceRecords && row.ShowOnlyTotalRows)
                    grid.ColumnDefinitions.Add(CreateColumnDefinition(150));
                else
                    grid.ColumnDefinitions.Add(CreateColumnDefinition(175));

                grid.ColumnDefinitions.Add(CreateColumnDefinition(175));
                grid.ColumnDefinitions.Add(CreateColumnDefinition(1, GridUnitType.Star));

                if (row.DetailRowType == DetailRowTypes.Header)
                {

                    lbl = new Label();
                    lbl.FontWeight = FontWeights.Bold;
                    lbl.SetValue(Grid.ColumnProperty, 1);
                    if (row.ShowOnlyTotalRows)
                        lbl.Content = row.GroupName;
                    else
                        lbl.Content = "Datum";
                    grid.Children.Add(lbl);

                    lbl = new Label();
                    lbl.FontWeight = FontWeights.Bold;
                    lbl.SetValue(Grid.ColumnProperty, 2);
                    lbl.Content = "Leistung";
                    grid.Children.Add(lbl);

                    lbl = new Label();
                    lbl.FontWeight = FontWeights.Bold;
                    lbl.SetValue(Grid.ColumnProperty, 3);
                    lbl.Content = "Dokumentation";
                    grid.Children.Add(lbl);
                }
                else if (row.DetailRowType == DetailRowTypes.Detail)
                {

                    int rowIndex = 0;
                    

                    foreach (var item in row.DateValues)
                    {
                        bool evenRow = false;
                        if (rowIndex > 0)
                            grid.RowDefinitions.Add(new RowDefinition());

                        Border border = new Border();

                        if (row.ShowOnlyTotalRows)
                        {
                            border.SetValue(Grid.ColumnProperty, 0);
                            border.SetValue(Grid.ColumnSpanProperty, 4);
                        }
                        else
                        {
                            border.SetValue(Grid.ColumnProperty, 1);
                            border.SetValue(Grid.ColumnSpanProperty, 3);
                        }
                        
                        border.SetValue(Grid.RowProperty, rowIndex);
                        border.CornerRadius = new CornerRadius(3, 3, 0, 0);
                        border.BorderThickness = new Thickness(1, 1, 1, 0);
                        border.BorderBrush = Brushes.Gray;
                        border.Background = Brushes.Cornsilk;
                        if (rowIndex > 0)
                        {
                            border.Margin = new Thickness(0, 4, 0, 0);
                            //border.Height = 35;
                        }
                        
                        grid.Children.Add(border);

                        lbl = new Label();
                        if (row.ShowOnlyTotalRows)
                        {
                            lbl.SetValue(Grid.ColumnProperty, 0);
                        }
                        else
                        {
                            lbl.SetValue(Grid.ColumnProperty, 1);
                        }
                        lbl.SetValue(Grid.ColumnSpanProperty, 2);
                        lbl.SetValue(Grid.RowProperty, rowIndex);
                        lbl.FontWeight = FontWeights.Bold;
                        lbl.Content = item.HeaderLongValue;
                        if (rowIndex > 0)
                        {
                            lbl.Margin = new Thickness(0, 4, 0, 0);
                        }
                        grid.Children.Add(lbl);

                        lbl = new Label();
                        if (row.ShowOnlyTotalRows)
                        {
                            lbl.SetValue(Grid.ColumnProperty, 1);
                        }
                        else
                        {
                            lbl.SetValue(Grid.ColumnProperty, 2);
                        }
                        lbl.SetValue(Grid.ColumnSpanProperty, 2);
                        lbl.SetValue(Grid.RowProperty, rowIndex);
                        lbl.FontWeight = FontWeights.Bold;
                        lbl.Content = "Summe: " + item.DateValueText + " FLS";
                        lbl.Margin = new Thickness(-100, 0, 0, 0);
                        if (rowIndex > 0)
                        {
                            lbl.Margin = new Thickness(-100, 4, 0, 0);
                        }
                        grid.Children.Add(lbl);

                        rowIndex++;
                        int count = 0;
                        if (item.ServiceRecordList != null)
                        {
                            
                            foreach (var sr in item.ServiceRecordList)
                            {
                                grid.RowDefinitions.Add(new RowDefinition());

                                border = new Border();
                                if (row.ShowOnlyTotalRows)
                                {
                                    border.SetValue(Grid.ColumnProperty, 0);
                                    border.SetValue(Grid.ColumnSpanProperty, 4);
                                }
                                else
                                {
                                    border.SetValue(Grid.ColumnProperty, 1);
                                    border.SetValue(Grid.ColumnSpanProperty, 3);
                                }
                                border.SetValue(Grid.RowProperty, rowIndex);
                                border.BorderThickness = new Thickness(1, 0, 1, 0);
                                border.BorderBrush = Brushes.Gray;
                                if (evenRow)
                                    border.Background = Brushes.WhiteSmoke;
                                else
                                    border.Background = Brushes.White;
                                evenRow = !evenRow;
                                if (count == item.ServiceRecordList.Count - 1)
                                {
                                    border.CornerRadius = new CornerRadius(0, 0, 3, 3);
                                    border.BorderThickness = new Thickness(1, 0, 1, 1);
                                }
                                grid.Children.Add(border);

                                if (row.ShowOnlyTotalRows)
                                {
                                    lbl = new Label();
                                    lbl.SetValue(Grid.ColumnProperty, 1);
                                    lbl.SetValue(Grid.RowProperty, rowIndex);
                                
                                    if (sr.GroupOid != null && sr.GroupPersonCount != null)
                                        lbl.Content = "Gruppe (" + sr.GroupPersonCount.Value + " Teilnehmer)";
                                    else
                                        lbl.Content = "Sonstiges".Equals(sr.GroupName) ? "" : sr.GroupName;
                                    
                                    grid.Children.Add(lbl);
                                }
                                lbl = new Label();
                                if (row.ShowOnlyTotalRows)
                                {
                                    lbl.SetValue(Grid.ColumnProperty, 0);
                                }
                                else
                                {
                                    lbl.SetValue(Grid.ColumnProperty, 1);
                                }
                                lbl.SetValue(Grid.RowProperty, rowIndex);

                                if (sr.Start != null && sr.Start.Value.Second == 0)
                                {
                                    if (sr.GroupOid != null && sr.GroupRoundedDuration != null && sr.GroupPersonCount != null)
                                    {
                                        lbl.Content = sr.Start.Value.ToShortDateString().Substring(0, 6) + " " + sr.Start.Value.ToShortTimeString() + "-" + sr.Start.Value.AddMinutes((double)sr.GroupRoundedDuration).ToShortTimeString() + " (" + (int)sr.RoundedDuration + " min.)";
                                        grid.Children.Add(lbl);

                                        lbl = new Label();
                                        if (row.ShowOnlyTotalRows)
                                        {
                                            lbl.SetValue(Grid.ColumnProperty, 0);
                                        }
                                        else
                                        {
                                            lbl.SetValue(Grid.ColumnProperty, 1);
                                        }
                                        lbl.SetValue(Grid.RowProperty, rowIndex);
                                        lbl.VerticalAlignment = VerticalAlignment.Top;
                                        lbl.Margin = new Thickness(0, 20, 0, 0);

                                        lbl.Content += sr.GroupPersonCount.Value + " Personen, " + sr.GroupEmployeeCount.Value + " Betreuuer";



                                    }
                                    else
                                    {
                                        lbl.Content = sr.Start.Value.ToShortDateString().Substring(0, 6) + " " + sr.Start.Value.ToShortTimeString() + "-" + sr.Start.Value.AddMinutes((double)sr.RoundedDuration).ToShortTimeString() + " (" + (int)sr.RoundedDuration + " min.)";
                                    }
                                }
                                else
                                {
                                    if (sr.GroupOid != null && sr.GroupRoundedDuration != null && sr.GroupPersonCount != null)
                                    {
                                        lbl.Content = sr.Start.Value.ToShortDateString().Substring(0, 6) + " " + (int)sr.GroupRoundedDuration.Value + " min.";
                                        grid.Children.Add(lbl);

                                        lbl = new Label();
                                        if (row.ShowOnlyTotalRows)
                                        {
                                            lbl.SetValue(Grid.ColumnProperty, 0);
                                        }
                                        else
                                        {
                                            lbl.SetValue(Grid.ColumnProperty, 1);
                                        }
                                        lbl.SetValue(Grid.RowProperty, rowIndex);
                                        lbl.VerticalAlignment = VerticalAlignment.Top;
                                        lbl.Margin = new Thickness(0, 20, 0, 0);

                                        lbl.Content += sr.GroupPersonCount.Value + " Personen, " + sr.GroupEmployeeCount.Value + " Betreuuer";



                                    }
                                    else
                                    {
                                        lbl.Content = sr.Start.Value.ToShortDateString().Substring(0, 6) + " " + (int)sr.RoundedDuration + " min.";
                                    }
                                }
                                
                                grid.Children.Add(lbl);

                                TextBlock block = new TextBlock();
                                block.TextWrapping = TextWrapping.Wrap;
                                block.HorizontalAlignment = HorizontalAlignment.Left;
                                block.SetValue(Grid.ColumnProperty, 2);
                                block.SetValue(Grid.RowProperty, rowIndex);
                                if (sr.ServiceDescription != null)
                                    block.Text = sr.ServiceDescription.Name;
                                //block.MaxWidth = 500;
                                block.Margin = new Thickness(0, 5, 5, 3);

                                
                                grid.Children.Add(block);

                                block = new TextBlock();
                                block.TextWrapping = TextWrapping.Wrap;
                                block.HorizontalAlignment = HorizontalAlignment.Left;
                                block.SetValue(Grid.ColumnProperty, 3);
                                block.SetValue(Grid.RowProperty, rowIndex);
                                block.Text = sr.Notice;
                                block.MaxWidth = 500;
                                block.Margin = new Thickness(5, 5, 5, 3);

                                grid.Children.Add(block);

                                rowIndex++;
                                count++;
                            }
                        }
                        if (count == 0)
                        {
                            border.CornerRadius = new CornerRadius(3, 3, 3, 3);
                            border.BorderThickness = new Thickness(1, 1, 1, 1);
                        }
                    }
                    
                }
                else if (row.DetailRowType == DetailRowTypes.Total)
                {

                    lbl = new Label();
                    lbl.SetValue(Grid.ColumnProperty, 1);
                    lbl.SetValue(Grid.ColumnSpanProperty, 2);
                    //lbl.SetValue(Grid.RowProperty, rowIndex);
                    lbl.FontWeight = FontWeights.Bold;
                    lbl.Content = row.RowTotalFLS + " FLS";

                    grid.Children.Add(lbl);
                }
            }
            else
            {
                grid.ColumnDefinitions.Add(CreateColumnDefinition(85));
                grid.ColumnDefinitions.Add(CreateColumnDefinition(75));
                lbl = new Label();
                if (row.DetailRowType != DetailRowTypes.Detail)
                    lbl.FontWeight = FontWeights.Bold;
                lbl.SetValue(Grid.ColumnProperty, 1);
                lbl.Content = row.Text1;
                grid.Children.Add(lbl);

                lbl = new Label();
                if (row.DetailRowType != DetailRowTypes.Detail)
                    lbl.FontWeight = FontWeights.Bold;
                lbl.SetValue(Grid.ColumnProperty, 2);
                lbl.Content = row.Text2;
                
                grid.Children.Add(lbl);

                if (row.DateValues != null)
                {
                    int idx = 3;
                    foreach (FLSOverviewDateValue dv in row.DateValues)
                    {

                        grid.ColumnDefinitions.Add(CreateColumnDefinition(65));

                        Label lblDate = new Label();
                        if (row.DetailRowType != DetailRowTypes.Detail)
                            lblDate.FontWeight = FontWeights.Bold;
                        lblDate.SetValue(Grid.ColumnProperty, idx++);
                        lblDate.Content = dv.DateValueText;
                        lblDate.HorizontalAlignment = HorizontalAlignment.Right;
                        lblDate.ToolTip = CreateToolTip(dv);
                        grid.Children.Add(lblDate);
                    }
                }

                //Total Column
                grid.ColumnDefinitions.Add(CreateColumnDefinition(70));

                lbl = new Label();
                if (row.DetailRowType != DetailRowTypes.Detail)
                    lbl.FontWeight = FontWeights.Bold;
                lbl.SetValue(Grid.ColumnProperty, grid.ColumnDefinitions.Count - 1);
                lbl.Content = row.RowTotalFLS;
                lbl.HorizontalAlignment = HorizontalAlignment.Right;
                grid.Children.Add(lbl);
            }

        }

        private object CreateToolTip(FLSOverviewDateValue dv)
        {
            if (dv.ServiceRecordList != null && dv.ServiceRecordList.Count > 0)
            {
                Grid grid = new Grid();

                grid.ColumnDefinitions.Add(CreateColumnDefinition(40));
                grid.ColumnDefinitions.Add(CreateColumnDefinition(130));
                ColumnDefinition def = CreateColumnDefinition(1, GridUnitType.Auto);
                //def.MaxWidth = 330;
                grid.ColumnDefinitions.Add(def);
                int rowIndex = 0;
                foreach (var item in dv.ServiceRecordList)
                {

                    grid.RowDefinitions.Add(new RowDefinition());
                    
                    Label lbl = new Label();
                    lbl.SetValue(Grid.ColumnProperty, 0);
                    lbl.SetValue(Grid.RowProperty, rowIndex);
                    lbl.Content = item.Start.Value.ToShortDateString().Substring(0, 6);
                    grid.Children.Add(lbl);

                    lbl = new Label();
                    lbl.SetValue(Grid.ColumnProperty, 1);
                    lbl.SetValue(Grid.RowProperty, rowIndex);
                    if (item.Start != null && item.Start.Value.Second == 0)
                    {
                        if (item.GroupOid != null && item.GroupRoundedDuration != null && item.GroupPersonCount != null)
                        {
                            lbl.Content = item.Start.Value.ToShortTimeString() + "-" + item.Start.Value.AddMinutes((double)item.GroupRoundedDuration).ToShortTimeString() + " (" + (int)item.RoundedDuration + " min.)";
                            grid.Children.Add(lbl);

                            lbl = new Label();
                            lbl.SetValue(Grid.ColumnProperty, 0);
                            lbl.SetValue(Grid.RowProperty, rowIndex);
                            lbl.SetValue(Grid.ColumnSpanProperty, 2);
                            lbl.VerticalAlignment = VerticalAlignment.Top;
                            lbl.Margin = new Thickness(0, 20, 0, 0);
                            
                            lbl.Content += item.GroupPersonCount.Value + " Personen, " + item.GroupEmployeeCount.Value + " Betreuuer";
                            
                            
                                
                        }
                        else
                        {
                            lbl.Content = item.Start.Value.ToShortTimeString() + "-" + item.Start.Value.AddMinutes((double)item.RoundedDuration).ToShortTimeString() + " (" + (int)item.RoundedDuration + " min.)";
                        }
                    }
                    else
                    {
                        if (item.GroupOid != null && item.GroupRoundedDuration != null && item.GroupPersonCount != null)
                        {
                            lbl.Content = (int)item.GroupRoundedDuration.Value + " min.";
                            grid.Children.Add(lbl);


                            lbl = new Label();
                            lbl.SetValue(Grid.ColumnProperty, 0);
                            lbl.SetValue(Grid.RowProperty, rowIndex);
                            lbl.SetValue(Grid.ColumnSpanProperty, 2);
                            lbl.VerticalAlignment = VerticalAlignment.Top;
                            lbl.Margin = new Thickness(0, 20, 0, 0);

                            lbl.Content += item.GroupPersonCount.Value + " Personen, " + item.GroupEmployeeCount.Value + " Betreuuer";

                        }
                        else
                        {
                            lbl.Content = (int)item.RoundedDuration + " min.";
                        }
                        
                       
                    }
                    grid.Children.Add(lbl);

                    TextBlock block = new TextBlock();
                    block.TextWrapping = TextWrapping.Wrap;
                    block.MaxWidth = 400;
                    block.SetValue(Grid.ColumnProperty, 2);
                    block.SetValue(Grid.RowProperty, rowIndex);
                    block.Text = item.Notice;

                    block.Margin = new Thickness(0, 5, 3, 0);
                    
                    grid.Children.Add(block);

                    rowIndex++;
                }

                return grid;
            }
            return null;
        }

        private ColumnDefinition CreateColumnDefinition(int p)
        {
            ColumnDefinition def = new ColumnDefinition();
            def.Width = new GridLength(p);

            return def;
        }

        private ColumnDefinition CreateColumnDefinition(double value, GridUnitType type)
        {
            ColumnDefinition def = new ColumnDefinition();
            def.Width = new GridLength(value, type);

            return def;
        }

    }
}

