using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Telerik.Web.UI;

public class AppointmentsController : ApiController
{

    public AppointmentsController()
        : base()
    {
    }

    private WebServiceAppointmentController _controller;
    //private SchedulerViewModel localSchedulerInfo;
    private XmlSchedulerProvider _provider;
    private XmlSchedulerProvider Provider
    {
        get
        {
            if (_provider == null)
            {
                _provider = new XmlSchedulerProvider(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Appointments.xml"), 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;
        }
    }

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

    [HttpPost]
    public IEnumerable<AppointmentData> InsertAppointment(JObject data)
    {
        var schedulerInfo = data.ToObject<SchedulerViewModel>();
        var appointmentData = data["appointmentData"].ToObject<AppointmentData>();

        return Controller.InsertAppointment(schedulerInfo, appointmentData);
    }

    [HttpPost]
    public IEnumerable<AppointmentData> UpdateAppointment(JObject data)
    {
        var schedulerInfo = data.ToObject<SchedulerViewModel>();
        var appointmentData = data["appointmentData"].ToObject<AppointmentData>();

        return Controller.UpdateAppointment(schedulerInfo, appointmentData);
    }

    [HttpPost]
    public IEnumerable<AppointmentData> CreateRecurrenceException(JObject data)
    {

        var schedulerInfo = data.ToObject<SchedulerViewModel>();
        var recurrenceExceptionData = data["appointmentData"].ToObject<AppointmentData>();

        return Controller.CreateRecurrenceException(schedulerInfo, recurrenceExceptionData);
    }

    [HttpPost]
    public IEnumerable<AppointmentData> RemoveRecurrenceExceptions(JObject data)
    {

        var schedulerInfo = data.ToObject<SchedulerViewModel>();
        var masterAppointmentData = data["appointmentData"].ToObject<AppointmentData>();

        return Controller.RemoveRecurrenceExceptions(schedulerInfo, masterAppointmentData);
    }

    [HttpPost]
    public IEnumerable<AppointmentData> DeleteAppointment(JObject data)
    {
        var schedulerInfo = data.ToObject<SchedulerViewModel>();
        var appointmentData = data["appointmentData"].ToObject<AppointmentData>();
        var deleteSeries = data["deleteSeries"].ToObject<bool>();

        return Controller.DeleteAppointment(schedulerInfo, appointmentData, deleteSeries);
    }

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

  
}
