using System;
using System.Windows.Controls;

using DevExpress.Xpf.Scheduler;
using DevExpress.Xpf.Scheduler.Drawing;
using DevExpress.XtraScheduler;
using DevExpress.Xpf.Scheduler.UI;
using SchedulerControl = DevExpress.Xpf.Scheduler.SchedulerControl;

using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace BeWo.Scheduler.View
{
    public class AbstractAppointmentEditForm : UserControl
    {
        private readonly SchedulerControl mSchedulerControl;
        private readonly Appointment mAppointment;
        private readonly AppointmentFormController mController;
        private RecurrenceVisualController mRecurrenceVisualController;

        protected AbstractAppointmentEditForm() { }
        
        public AbstractAppointmentEditForm(SchedulerControl schedulerControl, Appointment appointment)
        {
            mSchedulerControl = schedulerControl;
            mAppointment = appointment;

            mController = CreateFormController(schedulerControl, appointment);
            mRecurrenceVisualController = new RecurrenceVisualController(mController);
        }

        public virtual AppointmentFormController CreateFormController(SchedulerControl schedulerControl, Appointment appointment)
        {
            return new AppointmentFormController(schedulerControl, appointment);
        }

        public SchedulerControl Control { get { return mSchedulerControl; } }
        public Appointment Appointment { get { return mAppointment; } }
        public AppointmentFormController Controller { get { return mController; } }
        
        public string TimeEditMask { get { return CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern; } }
        public RecurrenceVisualController RecurrenceVisualController { get { return mRecurrenceVisualController; } }

        protected internal bool IsNewAppointment { get { return mController == null || mController.IsNewAppointment; } }

	    protected bool ShouldShowRecurrence
	    {
		    get
		    {
			    return !mAppointment.IsOccurrence && mController.ShouldShowRecurrenceButton;
		    }
	    }

	    protected virtual bool ApplyChanges()
        {
            if (!mController.IsConflictResolved())
            {
                return false;
            }

            if (ShouldShowRecurrence)
            {
                mRecurrenceVisualController.ApplyRecurrence();
            }

            try
            {
                mController.ApplyChanges();
            }
            catch (Exception e)
            {
	            MessageBox.Show(string.Format("Es ist leider ein Fehler aufgetreten. Die von Ihnen getätigten Änderungen am Termin konnten leider nicht übernommen werden.\n{0}\n{1}", e.Message, e.StackTrace), 
					"Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }
                
            SchedulerFormBehavior.Close(this, true);

            return true;
        }
        
        protected virtual void Cancel()
        {
            SchedulerFormBehavior.Close(this, false);
        }

        protected virtual void DeleteAppointment()
        {
            if (IsNewAppointment)
                return;

            mController.DeleteAppointment();
            SchedulerFormBehavior.Close(this, false);
        }

        protected static void LocalizeVisualChild(DependencyObject obj)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child is WeekOfMonthEdit)
                {
                    var edt = child as WeekOfMonthEdit;

                    edt.CustomDisplayText -= edt_CustomDisplayText;
                    edt.CustomDisplayText += edt_CustomDisplayText;

                    foreach (var womEditItem in edt.Items)
                    {
                        var ne = womEditItem as NamedElement;
                        if (ne != null)
                            ne.Caption = Translate(ne.Caption);
                    }
                }

                if (child is WeekDaysEdit)
                {
                    var edt = child as WeekDaysEdit;

                    edt.CustomDisplayText -= edt_CustomDisplayText;
                    edt.CustomDisplayText += edt_CustomDisplayText;

                    foreach (var womEditItem in edt.Items)
                    {
                        var ne = womEditItem as NamedElement;
                        if (ne != null)
                            ne.Caption = Translate(ne.Caption);
                    }
                }

                LocalizeVisualChild(child);
            }
        }

        static void edt_CustomDisplayText(object sender, DevExpress.Xpf.Editors.CustomDisplayTextEventArgs e)
        {
            e.DisplayText = Translate(e.DisplayText);
            e.Handled = true;
        }

        private static string Translate(String text)
        {
            switch (text)
            {
                case "First":
                    return "Ersten";
                case "Second":
                    return "Zweiten";
                case "Third":
                    return "Dritten";
                case "Fourth":
                    return "Vierten";
                case "Last":
                    return "Letzten";
                case "Day":
                    return "Tag";
                case "Weekday":
                    return "Arbeitstag";
                case "Weekend day":
                    return "Wochenendtag";
            }
            return text;
        }
    }
}
