Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Web.UI.WebControls
Imports System.Workflow.ComponentModel
Imports Microsoft.IdentityManagement.WebUI.Controls
Imports Microsoft.ResourceManagement.Workflow.Activities
Imports FIM.CustomWorkflowActivitiesLibrary.Activities

Namespace WebUIs
    Public Class GenerateUniqueValueSettingsPart
        Inherits ActivitySettingsPart

         Public Overrides Function GenerateActivityOnWorkflow(ByVal workflow As SequentialWorkflow) As Activity
            If Not Me.ValidateInputs() Then
                Return Nothing
            End If
            Dim GenerateUniqueValue As New GenerateUniqueValue()
            GenerateUniqueValue.GUAttribute = Me.GetText("txtGUAttribute")
            GenerateUniqueValue.GUOption1 = Me.GetText("txtGUOption1")
            GenerateUniqueValue.GUOption2 = Me.GetText("txtGUOption2")
            GenerateUniqueValue.GUOption3 = Me.GetText("txtGUOption3")
            GenerateUniqueValue.GUOption4 = Me.GetText("txtGUOption4")
            Return GenerateUniqueValue
        End Function

        ''' Called when editing the workflow activity settings.
        Public Overrides Sub LoadActivitySettings(ByVal activity As Activity)
            Dim GenerateUniqueValue As GenerateUniqueValue = TryCast(activity, GenerateUniqueValue)
            If GenerateUniqueValue IsNot Nothing Then
                Me.SetText("txtGUAttribute", GenerateUniqueValue.GUAttribute)
                Me.SetText("txtGUOption1", GenerateUniqueValue.GUOption1)
                Me.SetText("txtGUOption2", GenerateUniqueValue.GUOption2)
                Me.SetText("txtGUOption3", GenerateUniqueValue.GUOption3)
                Me.SetText("txtGUOption4", GenerateUniqueValue.GUOption4)
            End If
        End Sub

        ''' Saves the activity settings.
        Public Overrides Function PersistSettings() As ActivitySettingsPartData
            Dim data As New ActivitySettingsPartData()
            data("GUAttribute") = Me.GetText("txtGUAttribute")
            data("GUOption1") = Me.GetText("txtGUOption1")
            data("GUOption2") = Me.GetText("txtGUOption2")
            data("GUOption3") = Me.GetText("txtGUOption3")
            data("GUOption4") = Me.GetText("txtGUOption4")
            Return data
        End Function

        '''  Restores the activity settings in the UI
        Public Overrides Sub RestoreSettings(ByVal data As ActivitySettingsPartData)
            If data IsNot Nothing Then
                Me.SetText("txtGUAttribute", DirectCast(data("GUAttribute"), String))
                Me.SetText("txtGUOption1", DirectCast(data("GUOption1"), String))
                Me.SetText("txtGUOption2", DirectCast(data("GUOption2"), String))
                Me.SetText("txtGUOption3", DirectCast(data("GUOption3"), String))
                Me.SetText("txtGUOption4", DirectCast(data("GUOption4"), String))
            End If
        End Sub

        '''  Switches the activity between read only and read/write mode
        Public Overrides Sub SwitchMode(ByVal mode As ActivitySettingsPartMode)
            Dim [readOnly] As Boolean = (mode = ActivitySettingsPartMode.View)
            Me.SetTextBoxReadOnlyOption("txtGUAttribute", [readOnly])
            Me.SetTextBoxReadOnlyOption("txtGUOption1", [readOnly])
            Me.SetTextBoxReadOnlyOption("txtGUOption2", [readOnly])
            Me.SetTextBoxReadOnlyOption("txtGUOption3", [readOnly])
            Me.SetTextBoxReadOnlyOption("txtGUOption4", [readOnly])
        End Sub

        '''  Returns the activity naMe.
        Public Overrides ReadOnly Property Title() As String
                Return "Generate Unique Attribute"
            End Get
        End Property

        '''  In general, this method should be used to validate information entered
        '''  by the user when the activity is added to a workflow in the Workflow
        '''  Designer.
        '''  We could add code to verify that the log file path already exists on
        '''  the server that is hosting the FIM Portal and check that the activity
        '''  has permission to write to that location. However, the code
        '''  would only check if the log file path exists when the
        '''  activity is added to a workflow in the workflow designer. This class
        '''  will not be used when the activity is actually run.
        '''  For this activity we will just return true.
        Public Overrides Function ValidateInputs() As Boolean
            Return True
        End Function

        '''  Creates a Table that contains the controls used by the activity UI
        '''  in the Workflow Designer of the FIM portal. Adds that Table to the
        '''  collection of Controls that defines each activity that can be selected
        '''  in the Workflow Designer of the FIM Portal. Calls the base class of 
        '''  ActivitySettingsPart to render the controls in the UI.
        Protected Overrides Sub CreateChildControls()
            Dim controlLayoutTable As Table
            controlLayoutTable = New Table()

            'Width is set to 100% of the control size
            controlLayoutTable.Width = Unit.Percentage(100.0)
            controlLayoutTable.BorderWidth = 0
            controlLayoutTable.CellPadding = 2
            'Add a TableRow for each textbox in the UI 
            controlLayoutTable.Rows.Add(Me.AddTableRowTextBox("Target Attribute:", "txtGUAttribute", 400, 100, False, "Enter the target attribute.", False))
            controlLayoutTable.Rows.Add(Me.AddTableRowTextBox("Function 1:", "txtGUOption1", 400, 100, False, "(Required) The function to generate the attribute.", False))
            controlLayoutTable.Rows.Add(Me.AddTableRowTextBox("Function 2:", "txtGUOption2", 400, 100, False, "(Optional) The second choice function if the first is not unique.", False))
            controlLayoutTable.Rows.Add(Me.AddTableRowTextBox("Function 3:", "txtGUOption3", 400, 100, False, "((Optional) The third choice function if the second is not unique.", False))
            controlLayoutTable.Rows.Add(Me.AddTableRowTextBox("Function 4:", "txtGUOption4", 400, 100, False, "(Optional) The fourth choice function if the third is not unique.", False))

        End Sub

#Region "WebUI Functions"
        'Create a TableRow that contains a label and a textbox.
        Private Function AddTableRowTextBox(ByVal labelText As [String], ByVal controlID As [String], ByVal width As Integer, _
                                                  ByVal maxLength As Integer, ByVal multiLine As [Boolean], ByVal defaultValue As [String], _
                                                  ByVal hideText As Boolean) As TableRow
            Dim row As New TableRow()
            Dim labelCell As New TableCell()
            Dim controlCell As New TableCell()
            Dim oLabel As New Label()
            Dim oText As New TextBox()

            If hideText Then
                oText.TextMode = TextBoxMode.Password
            End If

            oLabel.Text = labelText
            oLabel.CssClass = MyBase.LabelCssClass
            oText.ID = controlID
            oText.CssClass = MyBase.TextBoxCssClass
            oText.Text = defaultValue
            oText.MaxLength = maxLength
            oText.Width = width
            If multiLine Then
                oText.TextMode = TextBoxMode.MultiLine
                oText.Rows = System.Math.Min(6, (maxLength + 60) \ 60)
                oText.Wrap = True
            End If
            Return row
        End Function

        Private Function GetText(ByVal textBoxID As String) As String
            Dim textBox As TextBox = DirectCast(Me.FindControl(textBoxID), TextBox)
            Return If(textBox.Text, [String].Empty)
        End Function

        Private Sub SetText(ByVal textBoxID As String, ByVal text As String)
            Dim textBox As TextBox = DirectCast(Me.FindControl(textBoxID), TextBox)
            If textBox IsNot Nothing Then
                textBox.Text = text
                textBox.Text = ""
            End If
        End Sub

        'Set the text box to read mode or read/write mode
        Private Sub SetTextBoxReadOnlyOption(ByVal textBoxID As String, ByVal [readOnly] As Boolean)
            Dim textBox As TextBox = DirectCast(Me.FindControl(textBoxID), TextBox)
            textBox.[ReadOnly] = [readOnly]
        End Sub
#End Region

    End Class
End Namespace