Option Strict On
Option Explicit On

Imports System

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only event edit result for AgendaView move/resize proof paths.
    ''' </summary>
    Friend NotInheritable Class MASAgendaEventEditResult
        Friend Sub New(accepted As Boolean,
                       eventKey As String,
                       eventDay As Date,
                       startMinute As Integer,
                       endMinute As Integer,
                       detachedSource As Boolean,
                       reason As String)
            Me.Accepted = accepted
            Me.EventKey = If(eventKey, String.Empty).Trim()
            Me.EventDay = MASAgendaViewPolicy.NormalizeDay(eventDay)
            Me.StartMinute = Math.Max(0, Math.Min(1439, startMinute))
            Me.EndMinute = Math.Max(0, Math.Min(1439, endMinute))
            Me.DetachedSource = detachedSource
            Me.Reason = If(reason, String.Empty).Trim()
        End Sub

        Friend ReadOnly Property Accepted As Boolean
        Friend ReadOnly Property EventKey As String
        Friend ReadOnly Property EventDay As Date
        Friend ReadOnly Property StartMinute As Integer
        Friend ReadOnly Property EndMinute As Integer
        Friend ReadOnly Property DetachedSource As Boolean
        Friend ReadOnly Property Reason As String

        Friend ReadOnly Property DurationMinutes As Integer
            Get
                Return Math.Max(0, EndMinute - StartMinute)
            End Get
        End Property
    End Class

    Partial Public NotInheritable Class MASAgendaView

        Friend Function MoveEventForProductionProof(eventKey As String,
                                                    newStartTime As DateTime,
                                                    newEndTime As DateTime,
                                                    Optional reason As String = "MASAgendaView.MoveEvent") As MASAgendaEventEditResult
            Return ApplyEventWindowForProductionProof(eventKey, newStartTime, newEndTime, reason)
        End Function

        Friend Function ResizeEventForProductionProof(eventKey As String,
                                                      newStartTime As DateTime,
                                                      newEndTime As DateTime,
                                                      Optional reason As String = "MASAgendaView.ResizeEvent") As MASAgendaEventEditResult
            Return ApplyEventWindowForProductionProof(eventKey, newStartTime, newEndTime, reason)
        End Function

        Private Function ApplyEventWindowForProductionProof(eventKey As String,
                                                           newStartTime As DateTime,
                                                           newEndTime As DateTime,
                                                           reason As String) As MASAgendaEventEditResult
            Dim normalizedKey As String = If(eventKey, String.Empty).Trim()
            If normalizedKey.Length = 0 Then Return New MASAgendaEventEditResult(False, String.Empty, _day, 0, 0, False, "Missing event key.")

            Dim index As Integer = FindEventIndex(normalizedKey)
            If index < 0 Then Return New MASAgendaEventEditResult(False, normalizedKey, _day, 0, 0, False, "Event key was not found.")

            Dim window As MASAgendaVisualTimeWindow = MASAgendaTemporalPolicy.CreateWindow(newStartTime, newEndTime, _day, _startHour, _endHour)
            If window Is Nothing OrElse Not window.IsValid Then
                Return New MASAgendaEventEditResult(False, normalizedKey, _day, 0, 0, False, If(window Is Nothing, "Window was not created.", window.BoundarySummary))
            End If

            Dim wasSourceLinked As Boolean = HasAgendaSource
            ClearAgendaSourceLinkForManualMutation()
            Dim item As AgendaEventState = _events(index)
            item.EventDay = window.EventDay
            item.StartMinute = window.StartMinute
            item.EndMinute = window.EndMinute
            NormalizeEventOrderAndSelection()
            _selectedEventIndex = FindEventIndex(normalizedKey)
            _topEventIndex = MASAgendaViewPolicy.EnsureVisibleEventIndex(_selectedEventIndex, _topEventIndex, _events.Count)
            RequestSizeLayoutRefreshForSizeAffectingChange(If(reason, "MASAgendaView.EventEdit"))
            InvalidateVisual()
            RaiseAgendaChangedSafe()
            RaiseSelectedEventChangedSafe()
            Return New MASAgendaEventEditResult(True, normalizedKey, window.EventDay, window.StartMinute, window.EndMinute, wasSourceLinked AndAlso Not HasAgendaSource, "Accepted bounded visual event edit.")
        End Function

    End Class

End Namespace
