using System.Collections.Generic;
using System.Windows.Controls;
using System;
using BS.Shared.DataContracts.Reports;

namespace BeWo.View.Report.Auslastung
{
    public partial class AuslastungListView : BeWoView
    {
        private AuslastungAnalysisRootDC _root;


        public AuslastungListView(AuslastungAnalysisRootDC root, AuslastungConfig config)
        {
            InitializeComponent();
            _root = root;
            

            RefreshReport(config);
            
        }

        public string DisplayName 
        {
            get
            {
                string name = "";
                if (_root != null && _root.Team != null)
                {
                    decimal flsApproved = 0;
                    decimal flsTotal = 0;
                    decimal hoursTotal = 0;

                    if (AuslastungRows != null)
                    {
                        foreach (var item in AuslastungRows)
                        {
                            if (item.AuslastungAnalysisEmployee.WeeklyTotalHours.HasValue)
                                hoursTotal += item.AuslastungAnalysisEmployee.WeeklyTotalHours.Value;
                            foreach (var detailRow in item.DetailRows)
                            {
                                if (detailRow.RowType == AuslastungRowTypes.Detail)
                                {
                                    flsApproved += detailRow.ApprovedFLS;
                                    flsTotal += detailRow.FLS;
                                }
                            }
                        }
                    }
                    name = String.Format("{0}   {1:0.##} Gesamtstd. pro Woche / {2:0.##} Std. pro Woche genehmigt / {3:0.##} Std. geleistet", _root.Team.Name, hoursTotal, flsApproved, flsTotal);
                    
                }
                return name;
            }
        }

        public AuslastungConfig AuslastungConfig { get; set; }

        public List<AuslastungRow> AuslastungRows { get; set; }

        private void listview_list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listview_list.SelectedItem != null)
                (listview_list.ItemContainerGenerator.ContainerFromItem(listview_list.SelectedItem)
                    as ListViewItem).IsSelected = false;
        }


        public void RefreshReport(AuslastungConfig config)
        {
            AuslastungConfig = config;
            AuslastungRows = AuslastungGenerator.CreateAuslastungRows(_root, config);
            if (_root == null || _root.Team == null)
                txtHeader.Visibility = System.Windows.Visibility.Collapsed;
            else
                txtHeader.Text = DisplayName;
            this.DataContext = AuslastungRows;
        }

        public ListView ListView
        {
            get
            {
                return listview_list;
            }
        }
    }
}
