using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Collections.Generic;

public partial class Default2 : System.Web.UI.Page
{
    private const string UsedResourcesSessionKey = "test";

    private List<Resource> UsedResources
    {
        get
        {
            List<Resource> resources = Session[UsedResourcesSessionKey] as List<Resource>;
            if (resources == null)
            {
                resources = new List<Resource>();
                Session[UsedResourcesSessionKey] = resources;
            }

            return resources;
        }
    }

    private void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UsedResources.Clear();
        }
    }

    private void ReserveResources(ResourceCollection resources)
    {
        foreach (Resource res in resources)
        {
            if (!UsedResources.Contains(res))
            {
                UsedResources.Add(res);
            }
        }
    }

    private void FreeResources(ResourceCollection resources)
    {
        foreach (Resource res in resources)
        {
            if (UsedResources.Contains(res))
            {
                UsedResources.Remove(res);
            }
        }
    }

    protected void RadScheduler1_DataBound(object sender, EventArgs e)
    {
        foreach (Resource res in RadScheduler1.Resources)
        {
            res.Available = !UsedResources.Contains(res);
        }

        DisplayAvailableResources();
    }

    protected void RadScheduler1_AppointmentDataBound(object sender, SchedulerEventArgs e)
    {
        ReserveResources(e.Appointment.Resources);
    }

    protected void RadScheduler1_AppointmentInsert(object sender, SchedulerCancelEventArgs e)
    {
        ReserveResources(e.Appointment.Resources);
    }

    protected void RadScheduler1_AppointmentUpdate(object sender, AppointmentUpdateEventArgs e)
    {
        FreeResources(e.Appointment.Resources);
        ReserveResources(e.ModifiedAppointment.Resources);
    }

    protected void RadScheduler1_AppointmentDelete(object sender, SchedulerCancelEventArgs e)
    {
        FreeResources(e.Appointment.Resources);
    }

    protected void RadScheduler1_AppointmentCreated(object sender, AppointmentCreatedEventArgs e)
    {
        List<string> users = new List<string>();
        foreach (Resource user in e.Appointment.Resources.GetResourcesByType("User"))
        {
            users.Add(user.Text);
        }

        Label assignedTo = (Label)e.Container.FindControl("AssignedTo");
        if (users.Count > 0)
        {
            assignedTo.Text = "Assigned to: " + string.Join(", ", users.ToArray());
        }
        else
        {
            assignedTo.Text = "Not assigned.";
        }

        Resource room = e.Appointment.Resources.GetResourceByType("Room");
        if (room != null)
        {
            Label location = (Label)e.Container.FindControl("Location");
            location.Text = "Location: " + room.Text;
        }
    }

    private void DisplayAvailableResources()
    {
        List<string> users = new List<string>();
        foreach (Resource user in RadScheduler1.Resources.GetResourcesByType("User"))
        {
            if (user.Available)
            {
                users.Add(user.Text);
            }
        }

        AvailableUsers.Text = string.Join(", ", users.ToArray());

        List<string> rooms = new List<string>();
        foreach (Resource room in RadScheduler1.Resources.GetResourcesByType("Room"))
        {
            if (room.Available)
            {
                rooms.Add(room.Text);
            }
        }
        AvailableRooms.Text = string.Join(", ", rooms.ToArray());
    }
}
