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.Auslastung
{
    public partial class AuslastungGrid : UserControl
    {
        public AuslastungDetailRow DetailRow
        {
            get { return (AuslastungDetailRow)GetValue(DetailRowProperty); }
            set { SetValue(DetailRowProperty, value); }
        }

        public static readonly DependencyProperty DetailRowProperty =
            DependencyProperty.Register("DetailRow",
                                        typeof(AuslastungDetailRow), 
                                        typeof(AuslastungGrid),
                                        new UIPropertyMetadata(new AuslastungDetailRow(AuslastungRowTypes.Header), new PropertyChangedCallback(RowChanged)));

        public AuslastungGrid()
        {
            InitializeComponent();
            
        }



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

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

            Label lbl;

            
            //Nummer
            grid.ColumnDefinitions.Add(CreateColumnDefinition(25));
            lbl = CreateLabel(0, row);
            if (row.RowType == AuslastungRowTypes.Header)
                lbl.Content = "Nr.";
            else if (row.RowType == AuslastungRowTypes.Total)
                lbl.Content = "";
            else
                lbl.Content = String.Format("{0}", row.Number);
            grid.Children.Add(lbl);

            //Klient
            grid.ColumnDefinitions.Add(CreateColumnDefinition(175));
            lbl = CreateLabel(1, row);
            lbl.Content = String.Format("{0}", row.CustomerName);
            grid.Children.Add(lbl);

            //Genehmigte FLS
            grid.ColumnDefinitions.Add(CreateColumnDefinition(100));
            lbl = CreateLabel(2, row);
            lbl.HorizontalAlignment = HorizontalAlignment.Right;
            if (row.RowType == AuslastungRowTypes.Header)
                lbl.Content = "FLS genehmigt";
            else
                lbl.Content = String.Format("{0:0.##}", row.ApprovedFLS);
            grid.Children.Add(lbl);

            //Beginn
            grid.ColumnDefinitions.Add(CreateColumnDefinition(100));
            lbl = CreateLabel(3, row);
            
            if (row.RowType == AuslastungRowTypes.Header)
                lbl.Content = "Beginn";
            else if (row.RowType == AuslastungRowTypes.Detail)
                lbl.Content = String.Format("{0:dd.MM.yyyy}", row.StartDate);
            grid.Children.Add(lbl);

            //Ende
            grid.ColumnDefinitions.Add(CreateColumnDefinition(100));
            lbl = CreateLabel(4, row);
            
            if (row.RowType == AuslastungRowTypes.Header)
                lbl.Content = "Ende";
            else if (row.RowType == AuslastungRowTypes.Detail)
                lbl.Content = String.Format("{0:dd.MM.yyyy}", row.EndDate);
            grid.Children.Add(lbl);

            //Geleistete FLS
            grid.ColumnDefinitions.Add(CreateColumnDefinition(100));
            lbl = CreateLabel(5, row);
            lbl.HorizontalAlignment = HorizontalAlignment.Right;
            if (row.RowType == AuslastungRowTypes.Header)
                lbl.Content = "FLS geleistet";
            else
                lbl.Content = String.Format("{0:0.##}", row.FLS);
            grid.Children.Add(lbl);


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

            
        }

        private Label CreateLabel(int colIndex, AuslastungDetailRow row)
        {
            Label lbl = new Label();
            lbl.SetValue(Grid.ColumnProperty, colIndex);
            if (row.RowType != AuslastungRowTypes.Detail)
            {
                lbl.FontWeight = FontWeights.Bold;
            }
            return lbl;
        }

        

        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;
        }

    }
}

