using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using BeWo.Core;
using BeWo.Core.Service;
using BeWo.ServiceProxy;

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

namespace BeWo.View.Report.AnnualReport
{
    public partial class AnnualReportView : BeWoView
    {

        private CompactOrganisationDC _SelectedOrganisation;

        private CompactTeamDC _SelectedTeam;


        public AnnualReportView()
        {
            this.InitializeComponent();

            InitDateCombos();
            InitCustomerCareTypeCombo();

            //linkPdfExport.NavigateUri = new Uri("http://www.bewoplaner.de");
            linkPdfExport.NavigateUri = new Uri(String.Format("{0}/ReportView.aspx", BeWoApp.SiteOfOrigin));

        }

        private void InitCustomerCareTypeCombo()
        {
            Cache.GetInstance().GetAllValueListEntrysByTypeAsync(ValueListEntryType.CustomerCareType, cb => 
                this.Dispatch(
                    delegate
                        {
                            var vle = new ValueListEntryDC();
                            cb.Add(vle);
                            vle.TypeDescription = "";

                            this.combobox_customercaretype.ItemsSource = cb.OrderBy(a => a.TypeDescription).ToList();
                        }));
           
        }

        private void InitDateCombos()
        {
            var years = new List<int>();
            for (int i = 0; i <= 10; i++)
            {
                years.Add(DateTime.Now.Year - i);
            }

            this.combobox_year.ItemsSource = years;
            this.combobox_year.SelectedItem = DateTime.Now.Year - 1;

        }

        private void CreateReportView(List<AnnualReportDC> reportList )
        {
            this.reportContent.Children.Clear();

            int col = 0;
            int row = 0;
            foreach (var annualReportDc in reportList)
            {
                
                AnnualReportChartControl ctrl = new AnnualReportChartControl();
                ctrl.AnnualReport = annualReportDc;
                ctrl.SetValue(Grid.ColumnProperty, col++);
                ctrl.SetValue(Grid.RowProperty, row);
                ctrl.Height = 300;
                ctrl.Margin = new Thickness(5);
                this.reportContent.Children.Add(ctrl);

                if (col > 1)
                {
                    col = 0;
                    row++;
                }
                    
            }
            //this._FLSOverview = new FLSOverview(list, config);
            //this.reportContent.Children.Clear();
            //this.reportContent.Children.Add(this._FLSOverview);
        }

        private void OrganisationSearchView_ItemSelected(object sender, EventArgs<CompactOrganisationDC> e)
        {
            this._SelectedOrganisation = e.Data;
            this.popupedit_costcenter.Text = this._SelectedOrganisation.ToString();
            this.popup_costcenter.IsOpen = false;
        }

        private void TeamSearchView_ItemSelected(object sender, EventArgs<CompactTeamDC> e)
        {
            this._SelectedTeam = e.Data;
            this.popupedit_team.Text = this._SelectedTeam.ToString();
            this.popup_team.IsOpen = false;
        }

        private void UpdatePDFExportUri(string id)
        {
            //string url = BeWoApp.SiteOfOrigin + "/ReportView.aspx?dcid=" + BeWoApp.Tenant + "eoid" + BeWoApp.LoggedOnUser.Employee.EmployeeOid + "&tenant=" + BeWoApp.Tenant + "&username=" + BeWoApp.UserName + "&token=" + BeWoApp.LoggedOnUser.RC2Password + "&type=" + Utils.EnumName(ReportTypes.FLSReport);

            //url = BeWoWpfUtils.GetHTMLEncodedURL(url);
            //this.linkPdfExport.NavigateUri = new Uri(url);

            //this.linkPdfExport.TargetName = Guid.NewGuid().ToString();

        }

        private void button_generate_Click(object sender, RoutedEventArgs e)
        {

            
            
                int year = DateTime.Now.Year - 1;


                if (combobox_year.SelectedItem != null)
                {

                    year = (int)combobox_year.SelectedItem;

                }

                DateTime startDate = new DateTime(year, 1, 1);

                long? costbearerOid = null;
                long? teamOid = null;
                long? customerCareTypeOid = null;

                if (_SelectedOrganisation != null)
                    costbearerOid = _SelectedOrganisation.CostBearerOid;

                if (_SelectedTeam != null)
                    teamOid = _SelectedTeam.TeamOid;

                ValueListEntryDC en = combobox_customercaretype.SelectedItem as ValueListEntryDC;

                if (combobox_customercaretype.SelectedItem == null || en.TypeDescription.Equals("") ) { }
                else
                if (combobox_customercaretype.SelectedItem != null)
                {
                    ValueListEntryDC entry = combobox_customercaretype.SelectedItem as ValueListEntryDC;
                    if (entry != null)
                        customerCareTypeOid = entry.ValueListEntryOid;

                }

                ServiceFacade.DoAnalysisServiceAsync(s => s.CreateAnnualReport(costbearerOid, teamOid, customerCareTypeOid, startDate, startDate.AddYears(1)), r => { this.Dispatch(delegate { this.CreateReportView(r); }); });
              
                this.UpdatePDFExportUri(string.Empty);
            
        }


        private void linkPdfExport_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            string url = String.Format("{0}/ReportView.aspx?y={1}", BeWoApp.SiteOfOrigin, combobox_year.Text);


            if (_SelectedOrganisation != null)
            {
                url += "&cb=" + _SelectedOrganisation.CostBearerOid;
            }

            if (_SelectedTeam != null)
            {
                url += "&t=" + _SelectedTeam.TeamOid;
            }


            ValueListEntryDC en = combobox_customercaretype.SelectedItem as ValueListEntryDC;

            if (combobox_customercaretype.SelectedItem == null || en.TypeDescription.Equals("")) { }
            else
            if (combobox_customercaretype.SelectedItem != null)
            {
                ValueListEntryDC entry = combobox_customercaretype.SelectedItem as ValueListEntryDC;
                if (entry != null)
                    url += "&art=" + entry.ValueListEntryOid;
            }

            url += "&type=" + Utils.EnumName(ReportTypes.Jahresbericht);

            var tempToken = string.Empty;
            ServiceFacade.DoDownloadServiceSync(s => tempToken = s.CreateTemporaryToken(url, true));
            url = url + "&token=" + tempToken;


            url = BeWoWpfUtils.GetHTMLEncodedURL(url);

            BeWoUtils.NavigateToReportUri(url, "Druckvorschau");

        }

        private void popupedit_costcenter_PopUpClick(object sender, RoutedEventArgs e)
        {
            this.popup_costcenter.IsOpen = true;
        }

        private void popupedit_team_PopUpClick(object sender, RoutedEventArgs e)
        {
            this.popup_team.IsOpen = true;
        }
    }
}