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.ServiceProxy;

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

namespace BeWo.View.Detail.Report
{
    public partial class SupportConceptsAnalysisView : BeWoView
    {
        private Dictionary<string, List<SupportConceptOverviewDC>> _UnfilteredSupportConcept2Overviews;

        public SupportConceptsAnalysisView(Dictionary<string, List<SupportConceptOverviewDC>> pSupportConcept2Overviews)
        {
            this.InitializeComponent();

            this.datepicker_appointeddate.AllowNullInput = true;
            this.datepicker_appointeddate.EditValue = null;

            this.chkShowOnlyMySupportConcepts.IsChecked = true;

            if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.SupportConcept_ViewAllSupportConcepts) && !BeWoApp.LoggedOnUser.HasRight(UserRightType.ViewAll))
            {
                this.chkShowOnlyMySupportConcepts.IsEnabled = false;
            }

            // if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.Analysis_SupportConceptOverview_View))
            // {

            // Dictionary<string, List<SupportConceptOverviewDC>> filteredList = new Dictionary<string, List<SupportConceptOverviewDC>>();
            // foreach (var item in pSupportConcept2Overviews)
            // {
            // (item.Value[0].
            // }
            // _UnfilteredSupportConcept2Overviews = filteredList;
            // }
            // else
            // {
            this._UnfilteredSupportConcept2Overviews = pSupportConcept2Overviews;

            // }
            this.DataContext = this._UnfilteredSupportConcept2Overviews;
            this.Loaded += this.SupportConceptsAnalysisView_Loaded;

            this.UpdateReportUrl();
        }

        private void SupportConceptsAnalysisView_Loaded(object sender, RoutedEventArgs e)
        {
            this.textbox_search.Focus();
        }

        private void UpdateReportUrl()
        {
            string url;

            long showOnlyMySupportConcepts = this.chkShowOnlyMySupportConcepts.IsChecked.Value ? 1 : 0;
            long showOnlySCsExpiringNextThreeMonths = this.chkShowOnlySCsExpiringNextThreeMonths.IsChecked.Value ? 1 : 0;
            long showArchived = this.chkShowArchived.IsChecked.Value ? 1 : 0;

            long eOid = BeWoApp.LoggedOnUser.Employee.EmployeeOid;

            // if (ViewModel != null && ViewModel.ServiceRecordsForSelectedCustomer != null && ViewModel.ServiceRecordsForSelectedCustomer.Count > 0)
            // {
            url = BeWoApp.SiteOfOrigin + "/ReportView.aspx" 
                //+ "?tenant=" + BeWoApp.Tenant 
                //+ "&username=" + BeWoApp.UserName
                //+ "&token=" + BeWoApp.LoggedOnUser.RC2Password 
                + "?my=" + showOnlyMySupportConcepts 
                + "&exp=" + showOnlySCsExpiringNextThreeMonths 
                + "&arc=" + showArchived 
                + "&eOid=" + eOid 
                + "&type=" + Utils.EnumName(ReportTypes.SupportConceptReport);

            if (datepicker_appointeddate.EditValue != null)
                url += "&date=" + String.Format("{0:d}", datepicker_appointeddate.DateTime);

            url = BeWoWpfUtils.GetHTMLEncodedURL(url);

            this.linkPdfExport.NavigateUri = new Uri(url);

            // }
        }

        private void buttonRefresh_Click(object sender, RoutedEventArgs e)
        {
            bool showOnlyRelated = this.chkShowOnlyMySupportConcepts.IsChecked.Value;
            bool showOnlyArchivedSCs = this.chkShowArchived.IsChecked.Value;

            this.UpdateReportUrl();

            DateTime? dt = null;
            if (this.datepicker_appointeddate.EditValue != null)
                dt = this.datepicker_appointeddate.DateTime;

            ServiceFacade.DoAnalysisServiceAsync(
                s => s.GetSupportConceptAnalysis(BeWoApp.LoggedOnUser.Employee.EmployeeOid, showOnlyRelated, showOnlyArchivedSCs, dt), 
                r =>
                    {
                        this.Dispatch(
                            delegate
                                {
                                    this._UnfilteredSupportConcept2Overviews = r.OrderBy(o => o.CustomerLastName + ", " + o.CustomerFirstName).GroupBy(so => so.CustomerLastName + ", " + so.CustomerFirstName).ToDictionary(g => g.Key, g => g.ToList());
                                    this.DataContext = this._UnfilteredSupportConcept2Overviews;
                                });
                    });
        }

        private void listview_list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.listview_list.SelectedItem != null)
            {
                (this.listview_list.ItemContainerGenerator.ContainerFromItem(this.listview_list.SelectedItem) as ListViewItem).IsSelected = false;
            }
        }

        private void textbox_searchVisible_TextChanged(object sender, TextChangedEventArgs e)
        {
            String test = textbox_searchVisible.Text;
            
            textbox_search.Text = test;
        }

        private void LinkPdfExport_OnRequestNavigate(object sender, RequestNavigateEventArgs e)
        {
			BeWoUtils.NavigateToReportUri((sender as System.Windows.Documents.Hyperlink).NavigateUri.ToString(), "Hilfeplanübersicht");
            //System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo((sender as System.Windows.Documents.Hyperlink).NavigateUri.ToString()));
        }
    }
}