using System;
using System.Data;
using System.Linq;
using DayPilot.Web.Ui.Recurrence;

public partial class Calendar_RecurrentEventEdit : System.Web.UI.Page
{
    private RecurrenceRule _rule = RecurrenceRule.NoRepeat;



    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetNoStore();

        var db = new DayPilotDataClassesDataContext();

        int targetId = Convert.ToInt32(Request.QueryString["id"]);
        int masterId = Convert.ToInt32(Request.QueryString["master"]);

        Event target = (from ev in db.Events where ev.id == targetId select ev).SingleOrDefault();
        Event master = (from ev in db.Events where ev.id == masterId select ev).SingleOrDefault();

        if (!IsPostBack)
        {
            switch (Mode)
            {
                case EventMode.Master:
                    _rule = RecurrenceRule.Decode(master.recurrence);
                    TextBoxStart.Text = master.eventstart.ToString();
                    TextBoxEnd.Text = master.eventend.ToString();
                    TextBoxName.Text = master.name;
                    break;
                case EventMode.NewException:
                    _rule = RecurrenceRule.Exception;
                    DateTime start = Occurrence;
                    TimeSpan duration = master.eventend - master.eventstart;
                    DateTime end = start + duration;
                    TextBoxStart.Text = start.ToString();
                    TextBoxEnd.Text = end.ToString();
                    TextBoxName.Text = master.name;
                    break;
                case EventMode.Exception:
                    _rule = RecurrenceRule.Exception;
                    TextBoxStart.Text = target.eventstart.ToString();
                    TextBoxEnd.Text = target.eventend.ToString();
                    TextBoxName.Text = target.name;
                    break;
                case EventMode.Regular:
                    _rule = RecurrenceRule.NoRepeat;
                    TextBoxStart.Text = target.eventstart.ToString();
                    TextBoxEnd.Text = target.eventend.ToString();
                    TextBoxName.Text = target.name;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }

    private EventMode Mode
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["master"])) 
            {
                if (!String.IsNullOrEmpty(Request.QueryString["start"]))
                {
                    return EventMode.NewException;
                }
                if (!String.IsNullOrEmpty(Request.QueryString["id"])) {
                    return EventMode.Exception;
                }
                return EventMode.Master;
            }

            return EventMode.Regular;
        }
        
    }

    enum EventMode
    {
        Master,
        NewException,
        Exception,
        Regular
    }

    private DateTime Occurrence
    {
        get
        {
            return Convert.ToDateTime(Request.QueryString["start"]);
        }
    }

    protected string RecurrenceJson
    {
        get
        {
            return _rule.ToJson();
        }
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        var db = new DayPilotDataClassesDataContext();

        int targetId = Convert.ToInt32(Request.QueryString["id"]);
        int masterId = Convert.ToInt32(Request.QueryString["master"]);

        Event target = (from ev in db.Events where ev.id == targetId select ev).SingleOrDefault();
        Event master = (from ev in db.Events where ev.id == masterId select ev).SingleOrDefault();

/*
        string id = Request.QueryString["id"];
        string masterId = Request.QueryString["master"];

        DataRow master = table.Rows.Find(masterId);
        DataRow row = table.Rows.Find(id);
*/

        switch (Mode)
        {
            case EventMode.Master:
                RecurrenceRule rule = RecurrenceRule.FromJson(masterId.ToString(), master.eventstart, Recurrence.Value);
                master.name = TextBoxName.Text;
                master.eventstart = Convert.ToDateTime(TextBoxStart.Text);
                master.eventend = Convert.ToDateTime(TextBoxEnd.Text);
                master.recurrence = rule.Encode();
                db.SubmitChanges();
                break;
            case EventMode.NewException:
                Event created = new Event
                              {
                                  name = TextBoxName.Text,
                                  eventstart = Convert.ToDateTime(TextBoxStart.Text),
                                  eventend = Convert.ToDateTime(TextBoxEnd.Text),
                                  recurrence = RecurrenceRule.EncodeExceptionModified(masterId.ToString(), Occurrence)
                              };
                db.Events.InsertOnSubmit(created);
                db.SubmitChanges();
                break;
            case EventMode.Exception:
                target.name = TextBoxName.Text;
                target.eventstart = Convert.ToDateTime(TextBoxStart.Text);
                target.eventend = Convert.ToDateTime(TextBoxEnd.Text);
                db.SubmitChanges();
                break;
            case EventMode.Regular:
                target.name = TextBoxName.Text;
                target.eventstart = Convert.ToDateTime(TextBoxStart.Text);
                target.eventend = Convert.ToDateTime(TextBoxEnd.Text);
                target.recurrence = RecurrenceRule.FromJson(targetId.ToString(), target.eventstart, Recurrence.Value).Encode();
                db.SubmitChanges();
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }

        Modal.Close(this, "OK");
    }
    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
        Modal.Close(this);
    }
}
