
Imports System
Imports System.ComponentModel
Imports System.Web.UI.WebControls
Imports Telerik.Web.UI
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data

Public Enum AdvancedFormMode
    Insert
    Edit
End Enum

Partial Class VB_AdvancedForm
    Inherits System.Web.UI.UserControl

#Region "Private members"

    Private _selectedCountry As String

    Private Property FormInitialized() As Boolean
        Get
            Return DirectCast((If(ViewState("FormInitialized"), False)), Boolean)
        End Get

        Set(value As Boolean)
            ViewState("FormInitialized") = value
        End Set
    End Property

    Private _mode As AdvancedFormMode = AdvancedFormMode.Insert
#End Region

#Region "Protected properties"

    Protected ReadOnly Property Owner() As RadScheduler
        Get
            Return Appointment.Owner
        End Get
    End Property

    Protected ReadOnly Property Appointment() As Appointment
        Get
            Dim container As SchedulerFormContainer = DirectCast(BindingContainer, SchedulerFormContainer)
            Return container.Appointment
        End Get
    End Property
#End Region

#Region "Attributes and resources"

    'Continent, Coutry   
    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Property Continent() As String
        Get
            Return ContinentsRadComboBox.SelectedValue
        End Get

        Set(value As String)
            ContinentsRadComboBox.SelectedValue = value
        End Set
    End Property

    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Property Country() As String
        Get
            Return CountriesRadComboBox.SelectedValue
        End Get

        Set(value As String)
            ' Store the selected country ID until we obtain the continent ID   
            _selectedCountry = value
        End Set
    End Property


    Protected Sub LoadContinents()
        Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("TelerikConnectionString").ConnectionString)

        Dim adapter As New SqlDataAdapter("SELECT * FROM Continents ORDER By Name", connection)
        Dim dt As New DataTable()
        adapter.Fill(dt)
        ContinentsRadComboBox.Items.Clear()
        ContinentsRadComboBox.Items.Insert(0, New RadComboBoxItem("- Select a continent -"))
        ContinentsRadComboBox.AppendDataBoundItems = True
        ContinentsRadComboBox.DataTextField = "Name"
        ContinentsRadComboBox.DataValueField = "ID"
        ContinentsRadComboBox.DataSource = dt
        ContinentsRadComboBox.DataBind()
        ContinentsRadComboBox.DataSource = Nothing
        'insert the first item   

    End Sub

    Protected Sub LoadCountries(continentID As String)
        Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("TelerikConnectionString").ConnectionString)

        'select a country based on the continentID   
        Dim adapter As New SqlDataAdapter("SELECT * FROM Countries WHERE ContinentID=@ContinentID ORDER By Name", connection)
        adapter.SelectCommand.Parameters.AddWithValue("@ContinentID", continentID)

        Dim dt As New DataTable()
        adapter.Fill(dt)

        CountriesRadComboBox.DataTextField = "Name"
        CountriesRadComboBox.DataValueField = "ID"
        CountriesRadComboBox.DataSource = dt
        CountriesRadComboBox.DataBind()
    End Sub

    Protected Sub CountriesRadComboBox_ItemsRequested(o As Object, e As RadComboBoxItemsRequestedEventArgs)
        ' e.Text is the first parameter of the requestItems method    
        ' invoked in LoadCountries method    
        LoadCountries(e.Text)
    End Sub


    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Property Description() As String
        Get
            Return DescriptionText.Text
        End Get

        Set(value As String)
            DescriptionText.Text = value
        End Set
    End Property
#End Region

#Region "Public properties"

    Public Property Mode() As AdvancedFormMode
        Get
            Return _mode
        End Get
        Set(value As AdvancedFormMode)
            _mode = value
        End Set
    End Property

    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Property Subject() As String
        Get
            Return SubjectText.Text
        End Get

        Set(value As String)
            SubjectText.Text = value
        End Set
    End Property

    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Property Start() As DateTime
        Get
            Dim result As DateTime = StartDate.SelectedDate.Value.[Date]

            If AllDayEvent.Checked Then
                result = result.[Date]
            Else
                Dim time As TimeSpan = StartTime.SelectedDate.Value.TimeOfDay
                result = result.Add(time)
            End If

            Return Owner.DisplayToUtc(result)
        End Get

        Set(value As DateTime)
            StartDate.SelectedDate = Owner.UtcToDisplay(value)
            StartTime.SelectedDate = Owner.UtcToDisplay(value)
        End Set
    End Property

    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Property [End]() As DateTime
        Get
            Dim result As DateTime = EndDate.SelectedDate.Value.[Date]

            If AllDayEvent.Checked Then
                result = result.[Date].AddDays(1)
            Else
                Dim time As TimeSpan = EndTime.SelectedDate.Value.TimeOfDay
                result = result.Add(time)
            End If

            Return Owner.DisplayToUtc(result)
        End Get

        Set(value As DateTime)
            EndDate.SelectedDate = Owner.UtcToDisplay(value)
            EndTime.SelectedDate = Owner.UtcToDisplay(value)
        End Set
    End Property


    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Property RecurrenceRuleText() As String
        Get
            If Owner.RecurrenceSupport Then
                Dim dateSpecified As Boolean = StartDate.SelectedDate.HasValue AndAlso EndDate.SelectedDate.HasValue
                Dim timeSpecified As Boolean = StartTime.SelectedDate.HasValue AndAlso EndTime.SelectedDate.HasValue

                If (AllDayEvent.Checked AndAlso Not dateSpecified) OrElse (Not AllDayEvent.Checked AndAlso Not (dateSpecified AndAlso timeSpecified)) Then
                    Return String.Empty
                End If

                AppointmentRecurrenceEditor.StartDate = Start
                AppointmentRecurrenceEditor.EndDate = [End]

                Dim rrule As RecurrenceRule = AppointmentRecurrenceEditor.RecurrenceRule

                If rrule = Nothing Then
                    Return String.Empty
                End If

                Dim originalRule As RecurrenceRule
                If RecurrenceRule.TryParse(OriginalRecurrenceRule.Value, originalRule) Then
                    rrule.Exceptions = originalRule.Exceptions
                End If

                If rrule.Range.RecursUntil <> DateTime.MaxValue Then
                    rrule.Range.RecursUntil = Owner.DisplayToUtc(rrule.Range.RecursUntil)

                    If Not AllDayEvent.Checked Then
                        rrule.Range.RecursUntil = rrule.Range.RecursUntil.AddDays(1)
                    End If
                End If

                Return rrule.ToString()
            End If

            Return String.Empty
        End Get

        Set(value As String)
            Dim rrule As RecurrenceRule
            RecurrenceRule.TryParse(value, rrule)

            If rrule <> Nothing Then
                If rrule.Range.RecursUntil <> DateTime.MaxValue Then
                    Dim recursUntil As DateTime = Owner.UtcToDisplay(rrule.Range.RecursUntil)

                    If Not IsAllDayAppointment(Appointment) Then
                        recursUntil = recursUntil.AddDays(-1)
                    End If

                    rrule.Range.RecursUntil = recursUntil
                End If

                AppointmentRecurrenceEditor.RecurrenceRule = rrule

                OriginalRecurrenceRule.Value = value
            End If
        End Set
    End Property

    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Property Reminder() As String
        Get
            If Owner.RemindersSupport AndAlso ReminderDropDown.SelectedValue <> String.Empty Then
                Return ReminderDropDown.SelectedValue
            End If

            Return String.Empty
        End Get

        Set(value As String)
            Dim item As RadComboBoxItem = ReminderDropDown.Items.FindItemByValue(value)
            If Not item Is Nothing Then
                item.Selected = True
            End If
        End Set
    End Property

    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Property TimeZoneID() As String
        Get
            Return TimeZonesDropDown.SelectedValue
        End Get

        Set(value As String)

            Dim item As RadComboBoxItem = TimeZonesDropDown.Items.FindItemByValue(value)
            If Not item Is Nothing Then
                item.Selected = True
            End If
        End Set
    End Property
#End Region

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If ContinentsRadComboBox.Items.Count = 0 Then
            ' Fill the continents combo   
            LoadContinents()
        End If

        UpdateButton.ValidationGroup = Owner.ValidationGroup
        UpdateButton.CommandName = If(Mode = AdvancedFormMode.Edit, "Update", "Insert")

        If Owner.AdvancedForm.EnableTimeZonesEditing Then
            PopulateTimeZonesDropDown()
        Else
            TimeZonesDropDown.Visible = False
        End If

        If Not Owner.Reminders.Enabled Then
            ReminderDropDown.Visible = False
        End If

        InitializeStrings()
        InitializeRecurrenceEditor()

        If Not FormInitialized Then
            UpdateResetExceptionsVisibility()
        End If
    End Sub

    Protected Overloads Overrides Sub OnPreRender(e As EventArgs)
        MyBase.OnPreRender(e)

        If Not String.IsNullOrEmpty(_selectedCountry) Then
            ' Fill the countries combo.   
            ' Note that Continent value is available in PreRender   
            LoadCountries(Continent)
            CountriesRadComboBox.SelectedValue = _selectedCountry
        End If


        If Not FormInitialized Then
            If IsAllDayAppointment(Appointment) Then
                EndDate.SelectedDate = EndDate.SelectedDate.Value.AddDays(-1)
            End If

            FormInitialized = True
        End If

        If [String].IsNullOrEmpty(Appointment.TimeZoneID) Then
            TimeZoneID = Owner.TimeZonesProvider.OperationTimeZone.TimeZoneId
        Else
            TimeZoneID = Appointment.TimeZoneID
        End If
    End Sub

    Private Sub PopulateTimeZonesDropDown()
        TimeZonesDropDown.DataSource = Owner.TimeZonesProvider.GetAllTimeZones()
        TimeZonesDropDown.DataTextField = "DisplayName"
        TimeZonesDropDown.DataValueField = "Id"
    End Sub

    Protected Sub BasicControlsPanel_DataBinding(sender As Object, e As EventArgs)
        AllDayEvent.Checked = IsAllDayAppointment(Appointment)
    End Sub

    Protected Sub DurationValidator_OnServerValidate(source As Object, args As ServerValidateEventArgs)
        args.IsValid = ([End] - Start) > TimeSpan.Zero
    End Sub

    Protected Sub ResetExceptions_OnClick(sender As Object, e As EventArgs)
        Owner.RemoveRecurrenceExceptions(Appointment)
        OriginalRecurrenceRule.Value = Appointment.RecurrenceRule
        ResetExceptions.Text = Owner.Localization.AdvancedDone
    End Sub

#Region "Private methods"

    Private Sub InitializeStrings()
        SubjectValidator.ErrorMessage = Owner.Localization.AdvancedSubjectRequired
        SubjectValidator.ValidationGroup = Owner.ValidationGroup

        AllDayEvent.Text = Owner.Localization.AdvancedAllDayEvent

        StartDateValidator.ErrorMessage = Owner.Localization.AdvancedStartDateRequired
        StartDateValidator.ValidationGroup = Owner.ValidationGroup

        StartTimeValidator.ErrorMessage = Owner.Localization.AdvancedStartTimeRequired
        StartTimeValidator.ValidationGroup = Owner.ValidationGroup

        EndDateValidator.ErrorMessage = Owner.Localization.AdvancedEndDateRequired
        EndDateValidator.ValidationGroup = Owner.ValidationGroup

        EndTimeValidator.ErrorMessage = Owner.Localization.AdvancedEndTimeRequired
        EndTimeValidator.ValidationGroup = Owner.ValidationGroup

        DurationValidator.ErrorMessage = Owner.Localization.AdvancedStartTimeBeforeEndTime
        DurationValidator.ValidationGroup = Owner.ValidationGroup

        ResetExceptions.Text = Owner.Localization.AdvancedReset

        SharedCalendar.FastNavigationSettings.OkButtonCaption = Owner.Localization.AdvancedCalendarOK
        SharedCalendar.FastNavigationSettings.CancelButtonCaption = Owner.Localization.AdvancedCalendarCancel
        SharedCalendar.FastNavigationSettings.TodayButtonCaption = Owner.Localization.AdvancedCalendarToday
    End Sub

    Private Sub InitializeRecurrenceEditor()
        AppointmentRecurrenceEditor.SharedCalendar = SharedCalendar
        AppointmentRecurrenceEditor.Culture = Owner.Culture
        AppointmentRecurrenceEditor.StartDate = Appointment.Start
        AppointmentRecurrenceEditor.EndDate = Appointment.[End]
    End Sub

    Private Sub UpdateResetExceptionsVisibility()
        ' Render the reset exceptions checkbox when using web service binding
        If String.IsNullOrEmpty(Owner.WebServiceSettings.Path) Then
            ResetExceptions.Visible = False
            Dim rrule As RecurrenceRule
            If RecurrenceRule.TryParse(Appointment.RecurrenceRule, rrule) Then
                ResetExceptions.Visible = rrule.Exceptions.Count > 0
            End If
        End If
    End Sub

    Private Function IsAllDayAppointment(appointment As Appointment) As Boolean
        Dim displayStart As DateTime = Owner.UtcToDisplay(appointment.Start)
        Dim displayEnd As DateTime = Owner.UtcToDisplay(appointment.[End])
        Return displayStart.CompareTo(displayStart.[Date]) = 0 AndAlso displayEnd.CompareTo(displayEnd.[Date]) = 0
    End Function
#End Region

End Class
