Imports DayPilot.Web.Ui.Recurrence

Partial Class RecurrentEventEdit
    Inherits System.Web.UI.Page

    Private _rule As RecurrenceRule = RecurrenceRule.NoRepeat

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        Response.Cache.SetNoStore()

        Dim db = New DayPilotDataClassesDataContext()

        Dim targetId As Integer = Convert.ToInt32(Request.QueryString("id"))
        Dim masterId As Integer = Convert.ToInt32(Request.QueryString("master"))

        Dim target As Ev = (From ev In db.Evs Where ev.id = targetId Select ev).SingleOrDefault()
        Dim master As Ev = (From ev In db.Evs Where ev.id = masterId Select ev).SingleOrDefault()

        If Not IsPostBack Then
            Select Case Mode
                Case EventMode.Master
                    _rule = RecurrenceRule.Decode(master.recurrence)
                    TextBoxStart.Text = master.eventstart.ToString()
                    TextBoxEnd.Text = master.eventend.ToString()
                    TextBoxName.Text = master.name
                    Exit Sub
                Case EventMode.NewException
                    _rule = RecurrenceRule.Exception
                    Dim start As DateTime = Occurrence
                    Dim duration As TimeSpan = master.eventend - master.eventstart
                    Dim ende As DateTime = start + duration
                    TextBoxStart.Text = start.ToString()
                    TextBoxEnd.Text = ende.ToString()
                    TextBoxName.Text = master.name
                    Exit Sub
                Case EventMode.Exception
                    _rule = RecurrenceRule.Exception
                    TextBoxStart.Text = target.eventstart.ToString()
                    TextBoxEnd.Text = target.eventend.ToString()
                    TextBoxName.Text = target.name
                    Exit Sub
                Case EventMode.Regular
                    _rule = RecurrenceRule.NoRepeat
                    TextBoxStart.Text = target.eventstart.ToString()
                    TextBoxEnd.Text = target.eventend.ToString()
                    TextBoxName.Text = target.name
                    Exit Sub
                Case Else
                    Throw New ArgumentOutOfRangeException()
            End Select
        End If
    End Sub

    Private ReadOnly Property Mode() As EventMode
        Get
            If Not String.IsNullOrEmpty(Request.QueryString("master")) Then
                If Not String.IsNullOrEmpty(Request.QueryString("start")) Then
                    Return EventMode.NewException
                End If
                If Not String.IsNullOrEmpty(Request.QueryString("id")) Then
                    Return EventMode.Exception
                End If
                Return EventMode.Master
            End If

            Return EventMode.Regular
        End Get
    End Property

    Enum EventMode
        Master
        NewException
        Exception
        Regular
    End Enum

    Private ReadOnly Property Occurrence() As DateTime
        Get
            Return Convert.ToDateTime(Request.QueryString("start"))
        End Get
    End Property

    Protected ReadOnly Property RecurrenceJson() As String
        Get
            Return _rule.ToJson()
        End Get
    End Property

    Protected Sub ButtonSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonSave.Click

        Dim db = New DayPilotDataClassesDataContext()

        Dim targetId As Integer = Convert.ToInt32(Request.QueryString("id"))
        Dim masterId As Integer = Convert.ToInt32(Request.QueryString("master"))

        Dim target As Ev = (From ev In db.Evs Where ev.id = targetId Select ev).SingleOrDefault()
        Dim master As Ev = (From ev In db.Evs Where ev.id = masterId Select ev).SingleOrDefault()

        Select Case Mode
            Case EventMode.Master
                Dim rule As RecurrenceRule = 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()
            Case EventMode.NewException
                Dim created = New Ev
                created.name = TextBoxName.Text
                created.eventstart = Convert.ToDateTime(TextBoxStart.Text)
                created.eventend = Convert.ToDateTime(TextBoxEnd.Text)
                created.recurrence = RecurrenceRule.EncodeExceptionModified(masterId.ToString(), Occurrence)

                db.Evs.InsertOnSubmit(created)
                db.SubmitChanges()
            Case EventMode.Exception
                target.name = TextBoxName.Text
                target.eventstart = Convert.ToDateTime(TextBoxStart.Text)
                target.eventend = Convert.ToDateTime(TextBoxEnd.Text)
                db.SubmitChanges()
            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()
            Case Else
                Throw New ArgumentOutOfRangeException()
        End Select

        Modal.Close(Me, "OK")

    End Sub

    Protected Sub ButtonCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonCancel.Click
        Modal.Close(Me)
    End Sub


End Class


