using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;

using BeWo.Core;
using BeWo.ServiceProxy;
using BeWo.ViewModel;

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

namespace BeWo.View.Detail.Report
{
    public partial class SettlementView : BeWoView
    {
        private bool _FrameVisible;

        private SettlementVM _ViewModel;

        public SettlementView(List<SupportConceptTreeNodeDC> pCurrentSupportConceptsTree)
        {
            this.InitializeComponent();
        }

        public override string Title
        {
            get
            {
                return "Spitzabrechnung";
            }
        }

        public SettlementVM ViewModel
        {
            get
            {
                return this._ViewModel;
            }

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

        protected override void popup_Closed(object sender, EventArgs e)
        {
            if (!this._FrameVisible)
            {
                this.frame.Width = double.NaN;
                this.frame.Height = double.NaN;
                this._FrameVisible = true;
            }

            base.popup_Closed(sender, e);
        }

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

            VMFactory.CreateSettlementVMAsync(
                e.Data.CostBearerRelOids[0], 
                r => this.Dispatch(
                    delegate
                        {
                            this.ViewModel = r;
                            this.tabcontrol.Visibility = Visibility.Visible;
                        }));
        }

        private void popupedit_CostBeaerer_PopUpClick(object sender, RoutedEventArgs ev)
        {
            if (this._FrameVisible && this.tabItem_report.IsSelected)
            {
                Visual lVisual = this.frame;
                do
                {
                    if (lVisual.ToString().ToLower().Contains("webbrowser"))
                    {
                        this.image.Source = new DrawingImage(VisualTreeHelper.GetDrawing(lVisual));
                        break;
                    }
                }
                while ((lVisual = VisualTreeHelper.GetChild(lVisual, 0) as Visual) != null);

                this.frame.Height = 0;
                this.frame.Width = 0;

                this._FrameVisible = false;
            }

            new DispatcherTimer(
                TimeSpan.FromMilliseconds(500), 
                DispatcherPriority.Background, 
                (s, e) =>
                    {
                        ((DispatcherTimer)s).Stop();
                        this.popup_costBearer.IsOpen = true;
                    }, 
                this.Dispatcher).Start();
        }

        private void tabItem_report_Selected(object sender, RoutedEventArgs e)
        {
            if (!this.Validate(this.tabitem_data))
            {
                e.Handled = true;
                this.tabcontrol.SelectedIndex = 0;
                this.tabItem_report.IsSelected = false;
            }
            else
            {
                ServiceFacade.DoOperationsServiceAsync(
                    s => s.PrepareSettlementReport(this.ViewModel.CommitToDataContract()), 
                    r => this.Dispatch(
                        delegate
                            {
								
                                string url = BeWoApp.SiteOfOrigin.ToString() + "/ReportView.aspx?dcid=" + r + "&type=" + Utils.EnumName(ReportTypes.Settlement);

                                url = BeWoWpfUtils.GetHTMLEncodedURL(url);
                                this.frame.Source = new Uri(url);
                            }));
            }
        }
    }
}