Imports DayPilot.Web.Ui.Enums
Imports DayPilot.Web.Ui.Recurrence
Imports DayPilot.Web.Ui.Events.Bubble
Imports DayPilot.Web.Ui.Events.Calendar

Partial Class _Default
    Inherits System.Web.UI.Page

    Private db As DayPilotDataClassesDataContext

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        db = New DayPilotDataClassesDataContext

        If Not IsPostBack Then
            DayPilotCalendar1.StartDate = DayPilot.Utils.Week.FirstDayOfWeek()
            LoadEvents()
            DataBind()
        End If

    End Sub

    Protected Sub DayPilotCalendar1_BeforeCellRender(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.BeforeCellRenderEventArgs) Handles DayPilotCalendar1.BeforeCellRender

    End Sub

    Protected Sub DayPilotCalendar1_BeforeEventRender(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.Calendar.BeforeEventRenderEventArgs) Handles DayPilotCalendar1.BeforeEventRender
    End Sub

    Protected Sub DayPilotCalendar1_BeforeHeaderRender(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.Calendar.BeforeHeaderRenderEventArgs) Handles DayPilotCalendar1.BeforeHeaderRender
        If (e.IsCorner) Then
            e.InnerHTML = String.Format("<div style='padding:2px; text-align:center; font-weight: bold;'>{0}</div>", DayPilotCalendar1.StartDate.Year)
        End If

    End Sub

    Protected Sub DayPilotCalendar1_Command(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.CommandEventArgs) Handles DayPilotCalendar1.Command

        Select Case e.Command
            Case "previous"
                DayPilotCalendar1.StartDate = DayPilotCalendar1.StartDate.AddDays(-7)
                LoadEvents()
                DayPilotCalendar1.DataBind()
                DayPilotCalendar1.Update(CallBackUpdateType.Full)
            Case "navigate"
                Dim start As DateTime = CType(e.Data("start"), DateTime)
                Dim eend As DateTime = CType(e.Data("end"), DateTime)

                DayPilotCalendar1.StartDate = start

                LoadEvents()
                DayPilotCalendar1.DataBind()
                DayPilotCalendar1.Update(CallBackUpdateType.Full)
            Case "refresh"
                LoadEvents()
                DayPilotCalendar1.DataBind()
                DayPilotCalendar1.Update()
        End Select
    End Sub

    Private Sub LoadEvents()
        Dim start As DateTime = DayPilotCalendar1.StartDate
        Dim ende As DateTime = DayPilotCalendar1.StartDate.AddDays(DayPilotCalendar1.Days)

        DayPilotCalendar1.DataSource = LoadEventsFromRange(start, ende)
        DayPilotNavigator1.DataSource = LoadEventsFromRange(DayPilotNavigator1.VisibleStart, DayPilotNavigator1.VisibleEnd)

    End Sub


    Private Function LoadEventsFromRange(ByVal start As DateTime, ByVal ende As DateTime) As IQueryable(Of Ev)
        Return From ev In db.Evs Where Not (ev.eventend <= start Or ev.eventstart >= ende) Or (ev.recurrence <> Nothing And ev.eventstart < ende) Select ev
    End Function


    Protected Sub DayPilotCalendar1_EventDelete(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.Calendar.EventDeleteEventArgs) Handles DayPilotCalendar1.EventDelete

        Dim target As Ev = findEvent(e.Value)

        If e.Recurrent And Not e.RecurrentException Then
            Dim master = findEvent(e.RecurrentMasterId)

            Dim created As Ev = New Ev()
            created.eventstart = e.Start
            created.eventend = e.End
            created.recurrence = RecurrenceRule.EncodeExceptionDeleted(e.RecurrentMasterId, e.Start)

            db.Evs.InsertOnSubmit(created)
            db.SubmitChanges()
        ElseIf Not target Is Nothing Then
            db.Evs.DeleteOnSubmit(target)
            db.SubmitChanges()
        End If

        LoadEvents()
        DayPilotCalendar1.DataBind()
        DayPilotCalendar1.Update("Event deleted.")


    End Sub

    Protected Sub DayPilotCalendar1_EventEdit(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.EventEditEventArgs) Handles DayPilotCalendar1.EventEdit
        Dim target As Ev = findEvent(e.Value)

        If Not target Is Nothing Then
            target.name = e.NewText
            db.SubmitChanges()
        End If

        LoadEvents()
        DayPilotCalendar1.DataBind()
        DayPilotCalendar1.Update("Event text changed.")
    End Sub


    Private Function findEvent(ByVal eventId As String) As Ev
        Dim eid As Integer = Convert.ToInt32(eventId)
        Return (From ev In db.Evs Where ev.id = eid Select ev).FirstOrDefault()
    End Function



    Protected Sub DayPilotCalendar1_EventMenuClick(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.EventMenuClickEventArgs) Handles DayPilotCalendar1.EventMenuClick
        If e.Command = "Delete" Then
            Dim target As Ev = findEvent(e.Value)

            If Not target Is Nothing Then
                db.Evs.DeleteOnSubmit(target)
                db.SubmitChanges()
            End If

            LoadEvents()
            DayPilotCalendar1.DataBind()
            DayPilotCalendar1.Update()
        End If
    End Sub

    Protected Sub DayPilotCalendar1_EventMove(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.EventMoveEventArgs) Handles DayPilotCalendar1.EventMove
        Dim target = findEvent(e.Value)

        ' recurrent event but no exception
        If e.Recurrent And Not e.RecurrentException Then
            Dim master As Ev = (From ev In db.Evs Select ev).FirstOrDefault()

            Dim created As Ev = New Ev()
            created.name = master.name
            created.eventstart = e.NewStart
            created.eventend = e.NewEnd
            created.recurrence = RecurrenceRule.EncodeExceptionModified(e.RecurrentMasterId, e.OldStart)

            db.Evs.InsertOnSubmit(created)
            db.SubmitChanges()

        ElseIf Not target Is Nothing Then
            target.eventstart = e.NewStart
            target.eventend = e.NewEnd
            db.SubmitChanges()
        End If

        LoadEvents()
        DayPilotCalendar1.DataBind()

        Dim ht As Hashtable = New Hashtable()
        ht("message") = "Event moved."
        ht("id") = e.Value
        DayPilotCalendar1.Update(ht)


    End Sub

    Protected Sub DayPilotCalendar1_EventResize(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.EventResizeEventArgs) Handles DayPilotCalendar1.EventResize
        Dim target = findEvent(e.Value)

        ' recurrent event but no exception
        If e.Recurrent And Not e.RecurrentException Then
            Dim master As Ev = (From ev In db.Evs Select ev).FirstOrDefault()

            Dim created As Ev = New Ev()
            created.name = master.name
            created.eventstart = e.NewStart
            created.eventend = e.NewEnd
            created.recurrence = RecurrenceRule.EncodeExceptionModified(e.RecurrentMasterId, e.OldStart)

            db.Evs.InsertOnSubmit(created)
            db.SubmitChanges()

        ElseIf Not target Is Nothing Then
            target.eventstart = e.NewStart
            target.eventend = e.NewEnd
            db.SubmitChanges()
        End If

        LoadEvents()
        DayPilotCalendar1.DataBind()

        Dim ht As Hashtable = New Hashtable()
        ht("message") = "Event moved."
        ht("id") = e.Value
        DayPilotCalendar1.Update(ht)


    End Sub

    Protected Sub DayPilotCalendar1_EventSelect(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.DayPilotEventArgs) Handles DayPilotCalendar1.EventSelect
        DayPilotCalendar1.Update()
    End Sub

    Protected Sub DayPilotCalendar1_TimeRangeSelected(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.TimeRangeSelectedEventArgs) Handles DayPilotCalendar1.TimeRangeSelected

        Dim created = New Ev
        created.name = "New event"
        created.eventstart = e.Start
        created.eventend = e.End

        db.Evs.InsertOnSubmit(created)
        db.SubmitChanges()

        LoadEvents()
        DayPilotCalendar1.DataBind()
        DayPilotCalendar1.Update("New event created.")
    End Sub

    Protected Sub DayPilotNavigator1_VisibleRangeChanged(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.Navigator.VisibleRangeChangedEventArgs) Handles DayPilotNavigator1.VisibleRangeChanged
        LoadEvents()
        DayPilotNavigator1.DataBind()
    End Sub

    Protected Sub DayPilotBubble1_RenderContent(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.Bubble.RenderEventArgs) Handles DayPilotBubble1.RenderContent
        If TypeOf e Is RenderEventBubbleEventArgs Then
            Dim re As RenderEventBubbleEventArgs = e

            Dim sb As StringBuilder = New StringBuilder()
            sb.AppendFormat("<b>{0}</b><br />", re.Text)
            sb.AppendFormat("Start: {0}<br />", re.Start)
            sb.AppendFormat("End: {0}<br />", re.End)
            sb.AppendFormat("<br />")
            sb.AppendFormat("<b>Double-click the event to enter inline edit mode.</b>")

            're.InnerHTML = "<b>Event details</b><br />Here is the right place to show details about the event with ID: " + re.Value + ". This text is loaded dynamically from the server.<br/>";
            re.InnerHTML = sb.ToString()
        ElseIf TypeOf e Is RenderTimeBubbleEventArgs Then
            Dim re As RenderTimeBubbleEventArgs = e
            e.InnerHTML = "<b>Time header details</b><br />From:" + re.Start + "<br />To: " + re.End
        ElseIf TypeOf e Is RenderCellBubbleEventArgs Then
            Dim re As RenderCellBubbleEventArgs = e
            e.InnerHTML = "<b>Cell details</b><br />Column:" + re.ResourceId + "<br />From:" + re.Start + "<br />To: " + re.End
        End If
    End Sub

End Class
