using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

using BeWo.ServiceProxy;
using BeWo.View.Detail.Report;
using BeWo.View.Report.AnnualReport;
using BeWo.View.Report.Employee;
using BeWo.View.Report.FLSReport;

using BS.Shared;
using BS.Shared.Extensions;
using BeWo.View.Report.Auslastung;

namespace BeWo.View.Master
{
    /// <summary>
    /// Interaction logic for ReportView.xaml
    /// </summary>
    public partial class ReportView : BeWoView
    {
        private readonly Dictionary<TabItem, BeWoView> _OpenViews = new Dictionary<TabItem, BeWoView>();

        public Dictionary<TabItem, BeWoView> OpenViews { get { return _OpenViews; } }

        public ReportView()
        {
            this.InitializeComponent();

            this.InitRights();
        }

        public override bool IsDirty
        {
            get
            {
                if (this._OpenViews.ContainsKey(this.root.SelectedItem as TabItem))
                {
                    return this._OpenViews[this.root.SelectedItem as TabItem].IsDirty;
                }

                return false;
            }
        }

        internal override DependencyObject ValidationOnSaveRootElement
        {
            get
            {
                return this._OpenViews[this.root.SelectedItem as TabItem].ValidationOnSaveRootElement;
            }
        }

        internal override bool DoSaveCheck()
        {
            foreach (var lPair in _OpenViews)
            {
                if (lPair.Value.IsDirty)
                {
                    lPair.Key.IsSelected = true;
                    if (!lPair.Value.DoSaveCheck())
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        protected override void Save()
        {
            this._OpenViews[this.root.SelectedItem as TabItem].SaveData();
        }

        private void InitRights()
        {
            if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.Analysis_ServiceOverview_View))
            {
                this.tabitem_serviceOverview.Visibility = Visibility.Collapsed;
            }
            if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.Analysis_Auslastung_View))
                tabitem_maReport.Visibility = Visibility.Collapsed;

            if (BeWoApp.Mandator.BeWoClientType != 3) //Nur Köln Ring
                tabitem_Mitarbeiter.Visibility = Visibility.Collapsed;
            else if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.Analysis_EmployeeOverviewAll_View))
            {
                tabitem_Mitarbeiter.Visibility = Visibility.Collapsed;
            }

            tabitem_annualReport.Visibility = Visibility.Collapsed;
            
			if (BeWoApp.Mandator.BeWoClientType == 6 || BeWoApp.Mandator.BeWoClientType == 7 || BeWoApp.Mandator.BeWoClientType == 10 || BeWoApp.AppSettings.ShowAnnualReport)
            {
                //Nur Jahresbericht + IBP + Integra
                tabitem_annualReport.Visibility = Visibility.Visible;
            }
			if (BeWoApp.Tenant == "3010479871" || (!BeWoApp.LoggedOnUser.HasRight(UserRightType.Mitarbeiterstundenkonto_ViewAll) && !BeWoApp.LoggedOnUser.HasRight(UserRightType.Mitarbeiterstundenkonto_ViewSelf)))
	        {
				//Aachener Verein
		        tabitem_makonto.Visibility = Visibility.Collapsed;
	        }

            if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.Jahresbericht))
            {
                tabitem_annualReport.Visibility = Visibility.Collapsed;
            }
        
            // if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.Analysis_Settlement_View))
            // tabitem_settlement.Visibility = Visibility.Collapsed;
        }

        private void Show(TabItem pTi)
        {
            foreach (var i in this._OpenViews)
            {
                if (i.Key == pTi)
                {
                    pTi.Content = this._OpenViews[pTi];
                }
                else
                {
                    i.Key.Content = null;
                }
            }
        }

        private void tabitem_flsReport_Selected(object sender, RoutedEventArgs e)
        {
            if (!this._OpenViews.ContainsKey(this.tabitem_flsReport))
            {
                this._OpenViews[this.tabitem_flsReport] = new BeWo.View.Report.FLSReport.FLSReportView();
            }

            this.Show(this.tabitem_flsReport);
        }

        private void tabitem_Mitarbeiter_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_Mitarbeiter))
                _OpenViews[tabitem_Mitarbeiter] = new BeWo.View.Report.MitarbeiterKoelnRing.FLSReportView();
            Show(tabitem_Mitarbeiter);
        }

        private void tabitem_maReport_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_maReport))
                _OpenViews[tabitem_maReport] = new AuslastungReportView2();
            Show(tabitem_maReport);
        }

		private void tabitem_maKonto_Selected(object sender, RoutedEventArgs e)
		{
			if (!_OpenViews.ContainsKey(tabitem_makonto))
				_OpenViews[tabitem_makonto] = new EmployeeHourReportView();
			Show(tabitem_makonto);
		}

        private void tabitem_queries_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_queries))
            {
                ServiceFacade.DoQueryServiceAsync(
                    s => s.GetAllQueriesOfType(QueryType.Public), 
                    r => this.Dispatch(
                        delegate
                            {
                                _OpenViews[tabitem_queries] = new QueryView(r);
                                Show(tabitem_queries);
                            }));
            }
            else
            {
                Show(tabitem_queries);
            }
        }

        private void tabitem_accountingreport_Selected(object sender, RoutedEventArgs e)
        {
            //if (!_OpenViews.ContainsKey(tabitem_accountingreport))
            //    _OpenViews[tabitem_accountingreport] = new AccountingReportView();
            //Show(tabitem_accountingreport);
        }

        private void tabitem_serviceOverview_Selected(object sender, RoutedEventArgs e)
        {
            if (!this._OpenViews.ContainsKey(this.tabitem_serviceOverview))
            {
                this._OpenViews[this.tabitem_serviceOverview] = new ServicesReportView();
            }

            this.Show(this.tabitem_serviceOverview);
        }


        private void tabitem_supportConceptAnalysis_Selected(object sender, RoutedEventArgs e)
        {
            if (!this._OpenViews.ContainsKey(this.tabitem_supportConceptAnalysis))
            {
                ServiceFacade.DoAnalysisServiceAsync(
                    s => s.GetSupportConceptAnalysis(BeWoApp.LoggedOnUser.Employee.EmployeeOid, true, false, null), 
                    r => this.Dispatch(
                        delegate
                            {
                                this._OpenViews[this.tabitem_supportConceptAnalysis] = new SupportConceptsAnalysisView(r.OrderBy(o => o.CustomerLastName + ", " + o.CustomerFirstName).GroupBy(so => so.CustomerLastName + ", " + so.CustomerFirstName).ToDictionary(g => g.Key, g => g.ToList()));

                                // _OpenViews[tabitem_supportConceptAnalysis] = new SupportConceptsAnalysisView(r
                                // .GroupBy(so => so.CustomerLastName + ", " + so.CustomerFirstName)
                                // .ToDictionary(g => g.Key, g => g.ToList()));
                                this.Show(this.tabitem_supportConceptAnalysis);
                            }));
            }
            else
            {
                this.Show(this.tabitem_supportConceptAnalysis);
            }
        }

        private void tabitem_annualReport_Selected(object sender, RoutedEventArgs e)
        {
            if (!this._OpenViews.ContainsKey(this.tabitem_annualReport))
            {
                this._OpenViews[this.tabitem_annualReport] = new AnnualReportView();
            }

            this.Show(this.tabitem_annualReport);

        }
    }
}