using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web;
using Telerik.Web.UI;
using System.Configuration;
using System.Data.Common;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class SchedulerWcfService
{
    //private WebServiceAppointmentController _controller;

    //private WebServiceAppointmentController Controller
    //{
    //    get
    //    {
    //        if (_controller == null)
    //        {
    //            _controller =
    //                new WebServiceAppointmentController(
    //                    new XmlSchedulerProvider(HttpContext.Current.Server.MapPath("~/App_Data/Appointments_Outlook.xml"),
    //                        false));
    //        }
    //        return _controller;
    //    }
    //}

    private WebServiceAppointmentController _controller;
    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;
        }
    }

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

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

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

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

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

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

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

}