using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Navigation;
using BS.Shared;
using BS.Shared.DataContracts;
using BeWo.Controls;
using BeWo.Core;
using BeWo.ServiceProxy;
using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;

using BS.Shared.Core;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;

namespace BeWo.View.Detail.Accounting
{
    public partial class SettlementInvoiceView : BeWoView
    {
        private readonly SettlementInvoiceItemEditView _EditView;

        private readonly SettlementInvoiceItemEditView2 _EditView2;

        private CompactSupportConceptDC _SelectedSupportConcept;

        public CompactSupportConceptDC SelectedSupportConcept
        {
            get { return _SelectedSupportConcept; }
            set { _SelectedSupportConcept = value; }
        }

        private SettlementListVM _Viewmodel;

        public SettlementInvoiceView()
        {
            this.InitializeComponent();
            this._EditView = new SettlementInvoiceItemEditView();
            this._EditView.EditingFinished += (s, e) =>
                {
                    // OpenViewModel(_EditView.ViewModel);
                    this.ReloadViewModel();
                    this.MainControl.CloseCurrentPopUp();
                };

            this._EditView2 = new SettlementInvoiceItemEditView2();
            this._EditView2.EditingFinished += (s, e) =>
            {
                // OpenViewModel(_EditView.ViewModel);
                this.ReloadViewModel();
                this.MainControl.CloseCurrentPopUp();
            };
        }

        internal SettlementListVM ViewModel
        {
            get
            {
                return this._Viewmodel;
            }

            set
            {
                this.DataContext = value;
                this._Viewmodel = value;
            }
        }

        public void ReloadViewModel()
        {
            VMFactory.CreateSettlementListForSupportConceptVMAsync(this._SelectedSupportConcept.SupportConceptOid, r => this.Dispatch(delegate { this.ViewModel = r; }));
        }

        // private void OpenViewModel(SettlementVM vm)
        // {
        // Dispatcher.BeginInvoke(DispatcherPriority.Normal,
        // (Action)delegate
        // {
        // String url = BeWoApp.SiteOfOrigin.ToString()
        // + "/ReportView.aspx?dcid="
        // + vm.DataContract.SettlementIvoiceOid.ToString()
        // + "&tenant=" + BeWoApp.Tenant
        // + "&username=" + BeWoApp.UserName
        // + "&rc2password=" + BeWoApp.LoggedOnUser.RC2Password
        // + "&type=" + Utils.EnumName<ReportTypes>(ReportTypes.Settlement);

        // url = BeWoWpfUtils.GetHTMLEncodedURL(url);

        // hyperlinkPrint.NavigateUri = new Uri(url);

        // hyperlinkPrint.DoClick();
        // });
        // }
        private void SupportConceptSearchView_ItemSelected(object sender, EventArgs<CompactSupportConceptDC> e)
        {
            this.popup_costBearer.IsOpen = false;
            this.popupedit_CostBeaerer.Text = e.Data.DetailDescription;
            this._SelectedSupportConcept = e.Data;
            this.ReloadViewModel();
        }

        private void bt_delete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Sind Sie sicher, dass Sie den gewählten Eintrag löschen möchten?", "Spitzabrechnung löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
                var vm = BeWoWpfUtils.GetTag<SettlementVM>(sender);
                this.ViewModel.VMList.Remove(vm);
                ServiceFacade.DoAccountingServiceAsync(s => s.DeactivateSettlementInvoice(new[] { vm.DataContract }.ToList()));
            }
        }

        private void bt_edit_Click(object sender, RoutedEventArgs e)
        {
            var vm = BeWoWpfUtils.GetTag<SettlementVM>(sender);
            if (vm != null)
            {
                if (ShowOldEditView(vm))
                {
                    this._EditView.ViewModel = vm;
                    this.MainControl.ShowControlAsModalPopUp(this._EditView);
                } 
                else
                {
                    this._EditView2.ViewModel = vm;
                    this.MainControl.ShowControlAsModalPopUp(this._EditView2);
                }
            }
        }

        private void button_addCostBearer_Click(object sender, RoutedEventArgs e)
        {
            ServiceFacade.DoOperationsServiceAsync(
                s => s.GenerateInitialSettlementDC(this._SelectedSupportConcept.CostBearerRelOids[0]), 
                dc =>
                    {
                        dc.SupportConceptOid = this._SelectedSupportConcept.SupportConceptOid;
                        this.Dispatch(
                            delegate
                                {
                                    var vm = new SettlementVM(dc);
                                    if (ShowOldEditView(vm))
                                    {
                                        this._EditView.ViewModel = new SettlementVM(dc);
                                        this.MainControl.ShowControlAsModalPopUp(this._EditView);
                                    } 
                                    else
                                    {
                                        this._EditView2.ViewModel = new SettlementVM(dc);
                                        this.MainControl.ShowControlAsModalPopUp(this._EditView2);
                                    }
                                });
                    });
        }

        private bool ShowOldEditView(SettlementVM vm)
        {
            if (vm.InvoiceItems == null || vm.InvoiceItems.VMList.Count == 0 || vm.DataContract.DifferentHourlyRateCount > 0)
                return true;
            return false;
        }

        private void popupedit_CostBeaerer_PopUpClick(object sender, RoutedEventArgs ev)
        {
            this.popup_costBearer.IsOpen = true;
        }

        private void Hyperlink_OnRequestNavigate(object sender, RequestNavigateEventArgs e)
        {
			BeWoUtils.NavigateToReportUri((sender as System.Windows.Documents.Hyperlink).NavigateUri.ToString(), null);
        }

        private void Popupedit_CostBeaerer_OnPreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (!BeWoUtils.CheckPopupEditClickableSpace((PopUpEdit)sender, e) || _SelectedSupportConcept == null || !BeWoApp.LoggedOnUser.HasRight(UserRightType.SupportConceptView_Edit))
                return;

            VMFactory.CreateSupportConceptVMAsync(_SelectedSupportConcept.SupportConceptOid,
                cb => this.Dispatch(
                            () => BeWoUtils.OpenModalViewWindow<SupportConceptVM, SupportConceptDC, SettlementInvoiceView>(cb, this, 
                            () => this.Dispatch(() => BeWoUtils.UpdateAllViews(_SelectedSupportConcept)))));
        }
    }
}