using System;
using System.Web.UI;
using Telerik.Web.UI;

namespace Telerik.Web.Examples.Scheduler.RadDock
{
    public partial class DefaultCS : Page
    {
        protected object EditedAppointmentID
        {
            get { return ViewState["EditedAppointmentID"]; }
            set { ViewState["EditedAppointmentID"] = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AsyncPostBackTrigger submitButtonTrigger = new AsyncPostBackTrigger();
                submitButtonTrigger.ControlID = SubmitButton.UniqueID;
                UpdatePanel1.Triggers.Add(submitButtonTrigger);
            }
        }

        protected void RadScheduler1_FormCreating(object sender, SchedulerFormCreatingEventArgs e)
        {
            if (e.Mode == SchedulerFormMode.Insert || e.Mode == SchedulerFormMode.Edit)
            {
                EditedAppointmentID = e.Appointment.ID;
                e.Cancel = true;

                ScriptManager.RegisterStartupScript(Page, GetType(), "formScript", "Sys.Application.add_load(openForm);", true);
                PopulateEditForm(e.Appointment);
            }
        }

        protected void PopulateEditForm(Appointment editedAppointment)
        {
            Appointment appointmentToEdit = RadScheduler1.PrepareToEdit(editedAppointment, RadScheduler1.EditingRecurringSeries);
            DescriptionText.Text = appointmentToEdit.Subject;
            StartTime.SelectedDate = RadScheduler1.UtcToDisplay(appointmentToEdit.Start);
            EndTime.SelectedDate = RadScheduler1.UtcToDisplay(appointmentToEdit.End);
            if (appointmentToEdit.Reminders.Count == 0)
            {
                ReminderDropDown.SelectedValue = "";
            }
            else
            {
                ReminderDropDown.SelectedValue = appointmentToEdit.Reminders[0].Trigger.TotalMinutes.ToString();
            }

            Resource user = appointmentToEdit.Resources.GetResourceByType("User");
            if (user != null)
            {
                UserDropDown.SelectedValue = user.Key.ToString();
            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            // Create resource based on the selected user
            Resource user = new Resource("User", int.Parse(UserDropDown.SelectedValue), UserDropDown.SelectedItem.Text);
            DateTime start = RadScheduler1.DisplayToUtc(StartTime.SelectedDate.Value);
            DateTime end = RadScheduler1.DisplayToUtc(EndTime.SelectedDate.Value);
            string reminder = ReminderDropDown.SelectedValue;

            if (EditedAppointmentID == null)
            {
                // Insert
                Appointment appointment = new Appointment(null, start, end, DescriptionText.Text);
                if (!String.IsNullOrEmpty(reminder))
                {
                    appointment.Reminders.Add(new Reminder(int.Parse(reminder)));
                }
                appointment.Resources.Add(user);

                RadScheduler1.InsertAppointment(appointment);
            }
            else
            {
                Appointment appointment = RadScheduler1.Appointments.FindByID(EditedAppointmentID);
                Appointment appointmentToUpdate = RadScheduler1.PrepareToEdit(appointment, RadScheduler1.EditingRecurringSeries);

                appointmentToUpdate.Subject = DescriptionText.Text;
                appointmentToUpdate.Start = start;
                appointmentToUpdate.End = end;
                if (!String.IsNullOrEmpty(reminder))
                {
                    appointmentToUpdate.Reminders.Add(new Reminder(int.Parse(reminder)));
                }

                // Remove the existing user resource, if any
                Resource existingUser = appointmentToUpdate.Resources.GetResourceByType("User");
                if (existingUser != null)
                {
                    appointmentToUpdate.Resources.Remove(existingUser);
                }
                appointmentToUpdate.Resources.Add(user);

                RadScheduler1.UpdateAppointment(appointmentToUpdate);
            }

            RadDock1.Closed = true;
        }
    }
}