using System.Collections.Generic;
using System.Web.Script.Services;
using System.Web.Services;
using System.Configuration;
using System.Data.Common;
using Telerik.Web.UI;


[WebService]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class SchedulerWebService : WebService
{
    private WebServiceAppointmentController _controller;
    //private MySchedulerInfo localSchedulerInfo;
    private MyDbSchedulerProvider _provider;
    private MyDbSchedulerProvider Provider
    {
        get
        {
            if (_provider == null)
            {
                var connString = ConfigurationManager.ConnectionStrings["TelerikVSXConnectionString"].ConnectionString;
                var factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
                _provider = new MyDbSchedulerProvider() { ConnectionString = connString, DbFactory = factory, PersistChanges= true };
            }
            return _provider;
        }
    }

    /// <summary>
    /// The WebServiceAppointmentController class is used as a facade to the SchedulerProvider.
    /// </summary>
    private WebServiceAppointmentController Controller
    {
        get
        {
            if (_controller == null)
            {
                _controller = new WebServiceAppointmentController(Provider);
            }

            return _controller;
        }
    }

    [WebMethod]
    public IEnumerable<AppointmentData> GetAppointments(MySchedulerInfo schedulerInfo)
    {        
        return Controller.GetAppointments(schedulerInfo);
    }

    [WebMethod]
    public IEnumerable<AppointmentData> InsertAppointment(MySchedulerInfo schedulerInfo, AppointmentData appointmentData)
    {
        return Controller.InsertAppointment(schedulerInfo, appointmentData);
    }

    [WebMethod]
    public IEnumerable<AppointmentData> UpdateAppointment(MySchedulerInfo schedulerInfo, AppointmentData appointmentData)
    {
        return Controller.UpdateAppointment(schedulerInfo, appointmentData);
    }

    [WebMethod]
    public IEnumerable<AppointmentData> CreateRecurrenceException(MySchedulerInfo schedulerInfo, AppointmentData recurrenceExceptionData)
    {
        return Controller.CreateRecurrenceException(schedulerInfo, recurrenceExceptionData);
    }

    [WebMethod]
    public IEnumerable<AppointmentData> RemoveRecurrenceExceptions(MySchedulerInfo schedulerInfo, AppointmentData masterAppointmentData)
    {
        return Controller.RemoveRecurrenceExceptions(schedulerInfo, masterAppointmentData);
    }

    [WebMethod]
    public IEnumerable<AppointmentData> DeleteAppointment(MySchedulerInfo schedulerInfo, AppointmentData appointmentData, bool deleteSeries)
    {
        return Controller.DeleteAppointment(schedulerInfo, appointmentData, deleteSeries);
    }

    [WebMethod]
    public IEnumerable<ResourceData> GetResources(MySchedulerInfo schedulerInfo)
    {
        return Controller.GetResources(schedulerInfo);
    }
}
