using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BS.Shared.Core;
using System.Windows.Media;
using BS.Shared.Extensions;
using System.Collections;
using BeWo.Controls;
using BeWo.Core;
using System.Windows.Documents;
using BeWo.Core.Service;
using System.Windows.Threading;
using BeWo.QueryService;
using BeWo.View.Detail;
using BeWo.View.Master;
using BeWo.View.Report.FLSReport;
using BS.Shared.DataContracts.Compact;
using BS.Shared.DataContracts.Reports;
using BS.Shared.DataContracts;
using BeWo.ServiceProxy;
using BS.Shared;
using BeWo.ViewModel;
using DevExpress.Xpf.Core;
using Microsoft.Win32;


namespace BeWo.View.Report.Auslastung
{
    public partial class AuslastungReportView2 : BeWoView
    {
        private CompactEmployeeDC _Employee;

		private CompactTeamDC _Team;

        public CompactEmployeeDC Employee { get { return _Employee; } set { _Employee = value; } }

		public AuslastungReportView2()
        {
            InitializeComponent();
            InitDateCombos();

            if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.Analysis_AuslastungAllEmployee_View))
            {
                checkbox_all.IsEnabled = false;
                checkbox_all.IsChecked = false;

				checkbox_all.Visibility = Visibility.Collapsed;
				popupedit_employee.Visibility = Visibility.Collapsed;
				checkbox_showTeamReport.Visibility = Visibility.Collapsed;
            }


            combobox_month.SelectionChanged += combobox_month_SelectionChanged;

			ThemeManager.SetTheme(BeWoApp.CurrentBeWo.MainWindow, Theme.Office2010Black);
            //ThemeManager.SetTheme(docPrevControl, Theme.Office2010Black);

            Unloaded += delegate { ThemeManager.SetTheme(BeWoApp.CurrentBeWo.MainWindow, null); docPrevControl.Visibility = Visibility.Collapsed; };
        }

        void combobox_month_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            InitDayCombos();
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (popupedit_employee == null)
                return;

			popupedit_employee.Text = string.Empty;
            _Employee = null;
        }

		//private void CheckBoxTeam_Checked(object sender, RoutedEventArgs e)
		//{
		//	if (popupedit_team == null)
		//		return;

		//	popupedit_team.Text = string.Empty;
		//	_Team = null;
		//}

		private void EmployeeSearchView_EmployeeSelected(object sender, EventArgs<CompactEmployeeDC> e)
        {
			_Employee = e.Data;
			popupedit_employee.Text = _Employee.SimpleDescription;
			popup_employee.IsOpen = false;
        }

		//private void TeamSearchView_TeamSelected(object sender, EventArgs<CompactTeamDC> e)
		//{
		//	_Team = e.Data;
		//	popupedit_team.Text = _Team.Name;
		//	popup_team.IsOpen = false;
		//}

        private void InitDateCombos()
        {
            this.combobox_month.ItemsSource = DateTimeFormatInfo.CurrentInfo.MonthNames.Where(s => !string.IsNullOrEmpty(s));
            int monthIdx = DateTime.Now.Month - 1;
            //if (DateTime.Now.Day < 15)
            //{
                monthIdx--;
                if (monthIdx < 0)
                    monthIdx = 11;
            //}
            this.combobox_month.SelectedIndex = monthIdx;
            var years = new List<int>();
            for (int i = 0; i <= 10; i++)
            {
                years.Add(DateTime.Now.Year - i);
            }

            this.combobox_year.ItemsSource = years;
            if (monthIdx == 11)
            {
                this.combobox_year.SelectedItem = DateTime.Now.Year - 1;
            }
            else
                this.combobox_year.SelectedItem = DateTime.Now.Year;

            InitDayCombos();
        }

        private void InitDayCombos()
        {
            int year = DateTime.Now.Year;

            var yearObj = this.combobox_year.SelectedItem;
            if (yearObj != null)
                year = Int32.Parse(yearObj.ToString());

            int month = this.combobox_month.SelectedIndex + 1;

            
            int days = DateTime.DaysInMonth(year, month);
            List<int> dayList = new List<int>();
            for (int i = 1 ; i <= days; i++)
            {
                dayList.Add(i);
            }

        }

        private void button_generate_Click(object sender, RoutedEventArgs e)
        {
            long empOid = 0;
            if (checkbox_all.IsChecked.Value)
                _Employee = null;

			if (!checkbox_all.IsChecked.Value && _Employee == null && BeWoApp.LoggedOnUser.HasRight(UserRightType.Analysis_AuslastungAllEmployee_View))
                MessageBox.Show("Bitte wählen Sie eine/n Mitarbeiter/in aus.", "BeWoPlaner", MessageBoxButton.OK, MessageBoxImage.Information);
            else
            {
                if (_Employee != null)
                    empOid = _Employee.EmployeeOid;
				if (!BeWoApp.LoggedOnUser.HasRight(UserRightType.Analysis_AuslastungAllEmployee_View))
		            empOid = BeWoApp.LoggedOnEmployee.EmployeeOid.Value;

                string url = BeWoApp.SiteOfOrigin + "/ReportView.aspx?";

				if (empOid > 0)
					url += "eoid=" + empOid + "&type=" + Utils.EnumName(ReportTypes.AuslastungReport);
                else
					url += "type=" + Utils.EnumName(ReportTypes.AuslastungReport);

                url += "&m=" + (combobox_month.SelectedIndex + 1) + "&y=" + combobox_year.SelectedItem ;
				if (checkbox_showTeamReport.IsChecked == true)
				{
					url += "&t=1";
				}
                var tempToken = string.Empty;
                ServiceFacade.DoDownloadServiceSync(s => tempToken = s.CreateTemporaryToken(url, true));
                string addChar = (url.Contains("?")) ? "&" : "?";
                url = url + addChar + "token=" + tempToken;

                url = BeWoWpfUtils.GetHTMLEncodedURL(url);

				BeWoUtils.NavigateToReportUri(url, "Mitarbeiterauslastung", docPrevControl);

            }
        }

        private void popupedit_employee_PopUpClick(object sender, RoutedEventArgs e)
        {
            popup_employee.IsOpen = true;
        }

       
        
    }
}
