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

using BeWo.Core.Service;
using BeWo.View.Detail;
using BeWo.View.Detail.Accounting;
using BeWo.View.Report.Finance;
using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;

using BS.Shared;
using BS.Shared.Extensions;

namespace BeWo.View.Master
{
    public partial class FinanceView
    {
        private readonly Dictionary<TabItem, BeWoView> _OpenViews = new Dictionary<TabItem, BeWoView>();

        private bool _SavePerformed;

        public FinanceView()
        {
            InitializeComponent();

            InitRights();
        }

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

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

        public void OpenBookingForCostBearer2SupportConcept(long costBearer2SupportConceptOid)
        {
            tabitem_financeBooking.IsSelected = true;

            if (_OpenViews.ContainsKey(tabitem_financeBooking))
            {
                var bookingView = _OpenViews[tabitem_financeBooking] as AccountingTransactionBookingView;

                bookingView.SelectCostBearer2SupportConcept(costBearer2SupportConceptOid);
            }
        }

        internal override bool DoSaveCheck()
        {
            var lDoSave = true;

            if (root.SelectedItem != null)
            {
                if (_OpenViews.ContainsKey(root.SelectedItem as TabItem))
                {
                    lDoSave = _OpenViews[root.SelectedItem as TabItem].DoSaveCheck();

                    if (lDoSave && IsDirty)
                    {
                        _SavePerformed = true;
                        _OpenViews.Remove(root.SelectedItem as TabItem);
                    }
                }
            }

            return lDoSave;
        }

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

        private void AddView(TabItem pTabItem, BeWoView pView)
        {
            _OpenViews[pTabItem] = pView;
            pTabItem.Content = pView;
        }

        private void InitRights()
        {
            if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.Analysis_Settlement_View))
            {
                tabitem_settlementinvoice.Visibility = Visibility.Collapsed;
            }

            if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.DokumenteFinanzen))
            {
                tabitem_documents.Visibility = Visibility.Collapsed;
            }

            if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.BargeldverwaltungFinanzenAnsehen))
            {
                tabitem_bargeldverwaltung.Visibility = Visibility.Collapsed;
            }
        }

        private void tabitem_equityinvoice_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_equityinvoice) || _SavePerformed)
            {
                AddView(tabitem_equityinvoice, new EquityInvoiceView());
            }
        }

        private void tabitem_financeBooking_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_financeBooking) || _SavePerformed)
            {
                VMFactory.CreateAccountingBookingVMAsnyc(
                    vm => this.Dispatch(
                        delegate
                        {
                            vm.DispatcherObject = this;

                            AddView(tabitem_financeBooking, new AccountingTransactionBookingView(vm));
                        }));
            }
        }

		//private void tabitem_financeBooking_Selected2(object sender, RoutedEventArgs e)
		//{
		//	if (!this._OpenViews.ContainsKey(this.tabitem_financeBooking2) || this._SavePerformed)
		//	{
		//		VMFactory.CreateAccountingBookingVMAsnyc(
		//			vm =>
		//			{
		//				this.Dispatch(
		//					delegate
		//					{
		//						vm.DispatcherObject = this;

		//						this.AddView(this.tabitem_financeBooking2, new AccountingTransactionBookingView2(vm));
		//					});
		//			});
		//	}
		//}

        private void tabitem_financeOverview_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_financeOverview) || _SavePerformed)
            {
                Cache.GetInstance()
                    .GetAllValueListEntrysByTypeAsync(ValueListEntryType.AccountingTransactionType,
                        categories => Cache.GetInstance()
                            .GetSupportConceptTreeAllActiveWithCostbearer(
                                tree => this.Dispatch(
                                    () => AddView(tabitem_financeOverview, new AccountingTransactionView(categories, tree)))));
            }
            else
            {
                var view = _OpenViews[tabitem_financeOverview] as AccountingTransactionView;

                if (view != null)
                {
                    view.ReloadViewModel();
                }
            }
        }

        private void tabitem_openClaims_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_openClaims) || _SavePerformed)
            {
                AddView(tabitem_openClaims, new FinanceOverviewView());
            }
        }

        private void tabitem_otherinvoice_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_otherinvoice) || _SavePerformed)
            {
                AddView(tabitem_otherinvoice, new GeneralInvoiceView());
            }
        }

        private void tabitem_overviewinvoice_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_overviewinvoice) || _SavePerformed)
            {
                AddView(tabitem_overviewinvoice, new InvoiceOverview(new InvoiceBaseListVM(null)));

                //VMFactory.CreateInvoiceOverviewListVM(cb => this.Dispatch(delegate { this.AddView(this.tabitem_overviewinvoice, new InvoiceOverview(cb)); }));
            }
        }

        //Dokument Selection
        private void tabitem_documents_Selected(object sender, RoutedEventArgs e)
        {
            if (_OpenViews.ContainsKey(tabitem_documents))
            {
                // TODO
                MessageBox.Show("Bevor zu einem Datensatz Dokumente hinterlegt werden können, muss dieser gespeichert werden.", "Erst speichern bitte", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            if (tabitem_documents.Content == null)
            {
                tabitem_documents.Content = new BeWoFileView(TableID.FinanzDokumente, null);
            }
        }

        private void tabitem_settlementinvoice_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_settlementinvoice) || _SavePerformed)
            {
                AddView(tabitem_settlementinvoice, new SettlementInvoiceView());
            }
        }

        private void tabitem_collectiveinvoice_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_collectiveinvoice) || _SavePerformed)
            {
                AddView(tabitem_collectiveinvoice, new ServiceInvoiceView());
            }
        }

        private void tabitem_bargeldverwaltung_Selected(object sender, RoutedEventArgs e)
        {
            if (!_OpenViews.ContainsKey(tabitem_bargeldverwaltung) || _SavePerformed)
            {
                AddView(tabitem_bargeldverwaltung, new BargeldkassenView(TableID.Bargeldkasse, -1));
            }
        }
    }
}