Imports System.ComponentModel

Imports Stax
Imports Stax.UI
Imports Stax.AppFramework

Public Class ProfilesForm
    Inherits ExtendedForm

#Region " Windows Form Designer generated code "

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lb As System.Windows.Forms.ListBox
    Friend WithEvents bLoad As System.Windows.Forms.Button
    Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
    Friend WithEvents bDown As System.Windows.Forms.Button
    Friend WithEvents bUp As System.Windows.Forms.Button
    Friend WithEvents bRemove As System.Windows.Forms.Button
    Friend WithEvents bAdd As System.Windows.Forms.Button
    Friend WithEvents bOK As System.Windows.Forms.Button
    Friend WithEvents bCancel As System.Windows.Forms.Button
    Friend WithEvents Timer As System.Windows.Forms.Timer
    Friend WithEvents TipProvider As Stax.UI.TipProvider
    Friend WithEvents bHelp As System.Windows.Forms.Button
    Friend WithEvents LineControl1 As Stax.UI.LineControl
    Friend WithEvents bEdit As System.Windows.Forms.Button
    Friend WithEvents bInfo As System.Windows.Forms.Button
    Friend WithEvents bRename As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.lb = New System.Windows.Forms.ListBox
        Me.bLoad = New System.Windows.Forms.Button
        Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
        Me.bDown = New System.Windows.Forms.Button
        Me.bUp = New System.Windows.Forms.Button
        Me.bRemove = New System.Windows.Forms.Button
        Me.bAdd = New System.Windows.Forms.Button
        Me.bOK = New System.Windows.Forms.Button
        Me.bCancel = New System.Windows.Forms.Button
        Me.Timer = New System.Windows.Forms.Timer(Me.components)
        Me.TipProvider = New Stax.UI.TipProvider(Me.components)
        Me.bRename = New System.Windows.Forms.Button
        Me.bEdit = New System.Windows.Forms.Button
        Me.bInfo = New System.Windows.Forms.Button
        Me.bHelp = New System.Windows.Forms.Button
        Me.LineControl1 = New Stax.UI.LineControl
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'lb
        '
        Me.lb.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lb.FormattingEnabled = True
        Me.lb.IntegralHeight = False
        Me.lb.Location = New System.Drawing.Point(13, 14)
        Me.lb.Name = "lb"
        Me.lb.Size = New System.Drawing.Size(244, 234)
        Me.lb.TabIndex = 0
        '
        'bLoad
        '
        Me.bLoad.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bLoad.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bLoad.Location = New System.Drawing.Point(268, 14)
        Me.bLoad.Name = "bLoad"
        Me.bLoad.Size = New System.Drawing.Size(75, 24)
        Me.bLoad.TabIndex = 1
        Me.bLoad.Text = "Load"
        Me.TipProvider.SetTipText(Me.bLoad, "Loads the selected profile into the current project.")
        '
        'bDown
        '
        Me.bDown.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bDown.Image = Global.StaxRip.My.Resources.Resources.Down16
        Me.bDown.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bDown.Location = New System.Drawing.Point(268, 134)
        Me.bDown.Name = "bDown"
        Me.bDown.Size = New System.Drawing.Size(75, 24)
        Me.bDown.TabIndex = 10
        Me.bDown.Text = "Down"
        Me.TipProvider.SetTipText(Me.bDown, "Moves the selected profile down.")
        '
        'bUp
        '
        Me.bUp.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bUp.Image = Global.StaxRip.My.Resources.Resources.Up16
        Me.bUp.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bUp.Location = New System.Drawing.Point(268, 104)
        Me.bUp.Name = "bUp"
        Me.bUp.Size = New System.Drawing.Size(75, 24)
        Me.bUp.TabIndex = 9
        Me.bUp.Text = "Up"
        Me.TipProvider.SetTipText(Me.bUp, "Moves the selected profile up.")
        '
        'bRemove
        '
        Me.bRemove.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bRemove.Image = Global.StaxRip.My.Resources.Resources.Minus16
        Me.bRemove.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bRemove.Location = New System.Drawing.Point(268, 74)
        Me.bRemove.Name = "bRemove"
        Me.bRemove.Size = New System.Drawing.Size(75, 24)
        Me.bRemove.TabIndex = 8
        Me.bRemove.Text = "Remove"
        Me.bRemove.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText
        Me.TipProvider.SetTipText(Me.bRemove, "Removes the selected profile.")
        '
        'bNew
        '
        Me.bAdd.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bAdd.Image = Global.StaxRip.My.Resources.Resources.Plus16
        Me.bAdd.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bAdd.Location = New System.Drawing.Point(268, 44)
        Me.bAdd.Name = "bAdd"
        Me.bAdd.Size = New System.Drawing.Size(75, 24)
        Me.bAdd.TabIndex = 7
        Me.bAdd.Text = "Add..."
        Me.TipProvider.SetTipText(Me.bAdd, "Opens a dialog in order to create a new profile.")
        '
        'bOK
        '
        Me.bOK.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bOK.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.bOK.Location = New System.Drawing.Point(106, 270)
        Me.bOK.Name = "bOK"
        Me.bOK.Size = New System.Drawing.Size(75, 24)
        Me.bOK.TabIndex = 11
        Me.bOK.Text = "OK"
        '
        'bCancel
        '
        Me.bCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.bCancel.Location = New System.Drawing.Point(187, 270)
        Me.bCancel.Name = "bCancel"
        Me.bCancel.Size = New System.Drawing.Size(75, 24)
        Me.bCancel.TabIndex = 12
        Me.bCancel.Text = "Cancel"
        '
        'bRename
        '
        Me.bRename.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bRename.Location = New System.Drawing.Point(268, 194)
        Me.bRename.Name = "bRename"
        Me.bRename.Size = New System.Drawing.Size(75, 24)
        Me.bRename.TabIndex = 17
        Me.bRename.Text = "Rename..."
        Me.TipProvider.SetTipText(Me.bRename, "Opens a dialog to rename the selected profile.")
        Me.bRename.UseVisualStyleBackColor = True
        '
        'bEdit
        '
        Me.bEdit.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bEdit.Location = New System.Drawing.Point(268, 164)
        Me.bEdit.Name = "bEdit"
        Me.bEdit.Size = New System.Drawing.Size(75, 24)
        Me.bEdit.TabIndex = 19
        Me.bEdit.Text = "Edit..."
        Me.TipProvider.SetTipText(Me.bEdit, "Opend the configuration dialog of selected profile.")
        Me.bEdit.UseVisualStyleBackColor = True
        '
        'bInfo
        '
        Me.bInfo.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bInfo.Location = New System.Drawing.Point(268, 224)
        Me.bInfo.Name = "bInfo"
        Me.bInfo.Size = New System.Drawing.Size(75, 24)
        Me.bInfo.TabIndex = 20
        Me.bInfo.Text = "Info..."
        Me.TipProvider.SetTipText(Me.bInfo, "Opens a dialog to display information about the selected profile.")
        Me.bInfo.UseVisualStyleBackColor = True
        '
        'bHelp
        '
        Me.bHelp.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bHelp.Location = New System.Drawing.Point(268, 270)
        Me.bHelp.Name = "bHelp"
        Me.bHelp.Size = New System.Drawing.Size(75, 24)
        Me.bHelp.TabIndex = 15
        Me.bHelp.Text = "Help..."
        Me.bHelp.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText
        Me.bHelp.UseVisualStyleBackColor = True
        '
        'LineControl1
        '
        Me.LineControl1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.LineControl1.Location = New System.Drawing.Point(12, 254)
        Me.LineControl1.Name = "LineControl1"
        Me.LineControl1.Size = New System.Drawing.Size(331, 10)
        Me.LineControl1.TabIndex = 18
        Me.LineControl1.TabStop = False
        '
        'ProfilesForm
        '
        Me.AcceptButton = Me.bOK
        Me.CancelButton = Me.bCancel
        Me.ClientSize = New System.Drawing.Size(355, 306)
        Me.Controls.Add(Me.bInfo)
        Me.Controls.Add(Me.bEdit)
        Me.Controls.Add(Me.bCancel)
        Me.Controls.Add(Me.LineControl1)
        Me.Controls.Add(Me.bRename)
        Me.Controls.Add(Me.bHelp)
        Me.Controls.Add(Me.bRemove)
        Me.Controls.Add(Me.bOK)
        Me.Controls.Add(Me.bDown)
        Me.Controls.Add(Me.bUp)
        Me.Controls.Add(Me.bAdd)
        Me.Controls.Add(Me.bLoad)
        Me.Controls.Add(Me.lb)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.HelpButton = True
        Me.KeyPreview = True
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "ProfilesForm"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Profiles"
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Profiles As IList
    Private AddProfileMethod As FunctionDelegate(Of Profile)
    Private LoadProfileMethod As ParamDelegate(Of Profile)
    Private Counter As Integer
    Private TextValues As New Dictionary(Of Button, String)
    Private HasChanged As Boolean

    Public Sub New( _
        ByVal title As String, _
        ByVal profiles As IList, _
        ByVal loadProfileMethod As ParamDelegate(Of Profile), _
        ByVal addProfileMethod As FunctionDelegate(Of Profile))

        MyBase.New()
        InitializeComponent()

        Text = title

        Me.Profiles = profiles
        Me.LoadProfileMethod = loadProfileMethod
        Me.AddProfileMethod = addProfileMethod

        For Each i As Profile In profiles
            lb.Items.Add(i.GetCopy)
        Next

        If lb.Items.Count > 0 Then
            lb.SelectedIndex = 0

            If lb.Height < lb.ItemHeight * lb.Items.Count Then
                Height += lb.ItemHeight * lb.Items.Count - lb.Height + 5
            End If
        End If

        UpdateControls()
    End Sub

    Private Sub UpdateControls()
        Dim p As Profile = DirectCast(lb.SelectedItem, Profile)
        Dim isSelected As Boolean = Not p Is Nothing

        bLoad.Enabled = isSelected
        bRemove.Enabled = isSelected
        bRename.Enabled = isSelected
        bInfo.Enabled = isSelected
        bEdit.Enabled = isSelected AndAlso p.CanEdit
        bUp.Enabled = ListBoxHelp.CanMoveUp(lb)
        bDown.Enabled = ListBoxHelp.CanMoveDown(lb)
        bOK.Enabled = HasChanged
    End Sub

    Private Sub lb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lb.SelectedIndexChanged
        UpdateControls()
    End Sub

    Private Sub bLoad_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bLoad.Click
        LoadProfileMethod(DirectCast(lb.SelectedItem, Profile))
        TextValues(bLoad) = bLoad.Text
        ButtonHelp.ShowBold(bLoad)
    End Sub

    Private Sub lb_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles lb.DoubleClick
        If Not lb.SelectedItem Is Nothing Then
            bLoad.PerformClick()
        End If
    End Sub

    Private Sub bNew_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bAdd.Click
        Dim p As Profile = AddProfileMethod()

        If Not p Is Nothing Then
            p = DirectCast(ObjectHelp.GetCopy(p), Profile)
            p.Name = InputBox.Show("Please enter the name of the new profile.", "Name", p.Name)

            If p.Name <> "" Then
                Dim remove As Profile = Nothing

                For Each i As Profile In lb.Items
                    If i.Name = p.Name Then
                        If Msg.Show("There is already a profile with this name, overwrite?", _
                            "Overwrite", MsgIcon.Question, MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then

                            remove = i
                        Else
                            Exit Sub
                        End If
                    End If
                Next

                If Not remove Is Nothing Then
                    lb.Items.Remove(remove)
                End If

                p.Clean()
                lb.Items.Add(p)
                lb.SelectedItem = p
                HasChanged = True
                UpdateControls()
            End If
        End If
    End Sub

    Private Sub bRemove_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bRemove.Click
        ListBoxHelp.RemoveSelection(lb)
        HasChanged = True
        UpdateControls()
    End Sub

    Private Sub bUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bUp.Click
        ListBoxHelp.MoveSelectionUp(lb)
        HasChanged = True
        UpdateControls()
    End Sub

    Private Sub bDown_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bDown.Click
        ListBoxHelp.MoveSelectionDown(lb)
        HasChanged = True
        UpdateControls()
    End Sub

    Private Sub bOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bOK.Click
        Profiles.Clear()

        For Each i As Profile In lb.Items
            Profiles.Add(i)
        Next
    End Sub

    Private Sub ProfilesForm_ActivatedFirstTime() Handles Me.ActivatedFirstTime
        If Not s.StringList.Contains(Me.GetType.FullName) Then
            bHelp.PerformClick()
            s.StringList.Add(Me.GetType.FullName)
        End If
    End Sub

    Private Sub bHelp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bHelp.Click
        HelpForm.ShowDialog("Profiles", TipProvider.GetTips, "Profiles allow to save frequently used settings for later reuse.")
    End Sub

    Private Sub bRename_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bRename.Click
        Dim p As Profile = DirectCast(lb.SelectedItem, Profile)
        Dim ret As String = InputBox.Show("Please enter the new name.", "Rename Profile", p.Name)

        If ret <> "" Then
            p.Name = ret
            lb.Items(lb.SelectedIndex) = lb.SelectedItem
            HasChanged = True
            UpdateControls()
        End If
    End Sub

    Private Sub bEdit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bEdit.Click
        If DirectCast(lb.SelectedItem, Profile).Edit() = Windows.Forms.DialogResult.OK Then
            lb.Items(lb.SelectedIndex) = lb.SelectedItem
            HasChanged = True
            UpdateControls()
        End If
    End Sub

    Private Sub bInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bInfo.Click
        DirectCast(lb.SelectedItem, Profile).ShowInfoDialog()
    End Sub
End Class