'  
'   Rebex Sample Code License
' 
'   Copyright 2026, REBEX CR s.r.o.
'   All rights reserved.
'   https://www.rebex.net/
' 
'   Permission to use, copy, modify, and/or distribute this software for any
'   purpose with or without fee is hereby granted.
' 
'   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'   OTHER DEALINGS IN THE SOFTWARE.
' 

Imports System.IO
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms

Imports Rebex.IO
Imports Rebex.Net

''' <summary>
''' Problem handling form.
''' </summary>
Public Class ProblemDetectedForm
    Inherits System.Windows.Forms.Form
    ''' <summary>
    ''' Required designer variable.
    ''' </summary>
    Private components As System.ComponentModel.Container = Nothing

    Private lblMessage As System.Windows.Forms.Label
    Private btnCancel As System.Windows.Forms.Button
    Private btnSkip As System.Windows.Forms.Button
    Private btnRename As System.Windows.Forms.Button
    Private btnRetry As System.Windows.Forms.Button
    Private btnOverwrite As System.Windows.Forms.Button
    Private btnOverwriteAll As System.Windows.Forms.Button
    Private btnOverwriteOlder As System.Windows.Forms.Button
    Private btnOverwriteDiffSize As System.Windows.Forms.Button
    Private btnSkipAll As System.Windows.Forms.Button
    Private btnFollowLink As System.Windows.Forms.Button
    Private btnResume As System.Windows.Forms.Button

    ' event arguments
    Private _arguments As ProblemDetectedEventArgs

    ' list of problem types the user chose to skip
    Private ReadOnly _skipProblemTypes As New Hashtable()

    ' true if the user chose to overwrite all existing files
    Private _overwriteAll As Boolean

    ' true if the user chose to overwrite all older files
    Private _overwriteOlder As Boolean
    Private btnOverwriteDiffChecksum As Button

    ' true if the user chose to overwrite all files with a different size
    Private _overwriteDifferentSize As Boolean

    ' true if the user chose to overwrite all files with a different checksum
    Private _overwriteDifferentChecksum As Boolean

    Public Sub New()
        '
        ' Required for Windows Form Designer support
        '
        InitializeComponent()
    End Sub

    ''' <summary>
    ''' Initialize the form for using it again in new batch transfer.
    ''' </summary>
    ''' <remarks>
    ''' Clears the user's chosen default actions.
    ''' </remarks>
    Public Sub Initialize()
        _skipProblemTypes.Clear()
        _overwriteAll = False
        _overwriteOlder = False
        _overwriteDifferentSize = False
        _overwriteDifferentChecksum = False
    End Sub

    ''' <summary>
    ''' Shows the form as a modal dialog box.
    ''' The form's buttons are shown according to possible actions taken from the given event argument.
    ''' </summary>
    ''' <param name="e">Event argument describing the type of a problem.</param>
    Public Sub ShowModal(form As Control, e As ProblemDetectedEventArgs)
        If e Is Nothing Then
            Throw New ArgumentNullException("e")
        End If

        ' if the user chose to skip this problem type, skip it
        If _skipProblemTypes.ContainsKey(e.ProblemType) Then
            e.Skip()
            Return
        End If

        ' if the problem is FileExists check, whether the user have chosen any default action already
        If e.ProblemType = TransferProblemType.FileExists Then
            If _overwriteAll Then
                e.Overwrite()
                Return
            End If

            If _overwriteDifferentSize Then
                e.Overwrite(OverwriteCondition.SizeDiffers)
                Return
            End If

            If _overwriteDifferentChecksum Then
                e.Overwrite(OverwriteCondition.ChecksumDiffers)
                Return
            End If

            If _overwriteOlder Then
                e.Overwrite(OverwriteCondition.Older)
                Return
            End If

            ' format the text according to action in progress (Downloading or Uploading)
            Dim messageFormat As String = "Overwrite file: {0}" & vbLf & vbTab & "{1} Bytes, {2}" & vbLf & vbLf & "with file: {3}" & vbLf & vbTab & "{4} Bytes, {5}"
            If e.Action = TransferAction.Downloading Then
                lblMessage.Text = String.Format(messageFormat, e.LocalPath, e.LocalItem.Length, e.LocalItem.LastWriteTime, e.RemotePath, e.RemoteItem.Length, _
                    e.RemoteItem.LastWriteTime)
            Else
                lblMessage.Text = String.Format(messageFormat, e.RemotePath, e.RemoteItem.Length, e.RemoteItem.LastWriteTime, e.LocalPath, e.LocalItem.Length, _
                    e.LocalItem.LastWriteTime)
            End If

            lblMessage.TextAlign = ContentAlignment.MiddleLeft
        Else
            lblMessage.Text = e.Exception.Message
            lblMessage.TextAlign = ContentAlignment.MiddleCenter
        End If

        ' store the event arguments for later use at button click handler
        _arguments = e

        ' only enable buttons that represent a possible action
        btnCancel.Enabled = e.IsReactionPossible(TransferProblemReaction.Cancel)
        btnSkip.Enabled = e.IsReactionPossible(TransferProblemReaction.Skip)
        btnSkipAll.Enabled = e.IsReactionPossible(TransferProblemReaction.Skip)
        btnRetry.Enabled = e.IsReactionPossible(TransferProblemReaction.Retry)
        btnRename.Enabled = e.IsReactionPossible(TransferProblemReaction.Rename)
        btnOverwrite.Enabled = e.IsReactionPossible(TransferProblemReaction.Overwrite)
        btnOverwriteAll.Enabled = e.IsReactionPossible(TransferProblemReaction.Overwrite)
        btnOverwriteOlder.Enabled = e.IsReactionPossible(TransferProblemReaction.Overwrite)
        btnOverwriteDiffSize.Enabled = e.IsReactionPossible(TransferProblemReaction.Overwrite)
        btnOverwriteDiffChecksum.Enabled = e.IsOverwriteConditionPossible(OverwriteCondition.ChecksumDiffers)
        btnFollowLink.Enabled = e.IsReactionPossible(TransferProblemReaction.FollowLink)
        btnResume.Enabled = e.IsReactionPossible(TransferProblemReaction.[Resume])

        ' select default action button
        SetDefaultActionButton(e)

        ShowDialog(form)
    End Sub

    ''' <summary>
    ''' Selects the button according to default action.
    ''' </summary>
    ''' <param name="e">Default action.</param>
    Private Sub SetDefaultActionButton(e As ProblemDetectedEventArgs)
        Select Case e.Reaction
            Case TransferProblemReaction.Cancel
                btnCancel.[Select]()
                Exit Select
            Case TransferProblemReaction.Fail
                btnCancel.[Select]()
                Exit Select
            Case TransferProblemReaction.Skip
                btnSkipAll.[Select]()
                Exit Select
            Case TransferProblemReaction.Retry
                btnRetry.[Select]()
                Exit Select
            Case TransferProblemReaction.Rename
                btnRename.[Select]()
                Exit Select
            Case TransferProblemReaction.FollowLink
                btnFollowLink.[Select]()
                Exit Select
            Case TransferProblemReaction.[Resume]
                btnResume.[Select]()
                Exit Select
            Case TransferProblemReaction.Overwrite
                Select Case e.OverwriteCondition
                    Case OverwriteCondition.None
                        btnOverwriteAll.[Select]()
                        Exit Select
                    Case OverwriteCondition.Older
                        btnOverwriteOlder.[Select]()
                        Exit Select
                    Case OverwriteCondition.SizeDiffers
                        btnOverwriteDiffSize.[Select]()
                        Exit Select
                    Case OverwriteCondition.ChecksumDiffers
                        btnOverwriteDiffChecksum.[Select]()
                        Exit Select
                End Select
                Exit Select
        End Select
    End Sub

    ''' <summary>
    ''' Clean up any resources being used.
    ''' </summary>
    Protected Overrides Sub Dispose(disposing As Boolean)
        If disposing Then
            If components IsNot Nothing Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    #Region "Windows Form Designer generated code"
    ''' <summary>
    ''' Required method for Designer support - do not modify
    ''' the contents of this method with the code editor.
    ''' </summary>
    Private Sub InitializeComponent()
        Me.btnCancel = New System.Windows.Forms.Button()
        Me.btnSkip = New System.Windows.Forms.Button()
        Me.btnRename = New System.Windows.Forms.Button()
        Me.btnRetry = New System.Windows.Forms.Button()
        Me.btnOverwrite = New System.Windows.Forms.Button()
        Me.btnOverwriteAll = New System.Windows.Forms.Button()
        Me.btnOverwriteOlder = New System.Windows.Forms.Button()
        Me.btnOverwriteDiffSize = New System.Windows.Forms.Button()
        Me.btnSkipAll = New System.Windows.Forms.Button()
        Me.btnFollowLink = New System.Windows.Forms.Button()
        Me.lblMessage = New System.Windows.Forms.Label()
        Me.btnResume = New System.Windows.Forms.Button()
        Me.btnOverwriteDiffChecksum = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        ' 
        ' btnCancel
        ' 
        Me.btnCancel.Location = New System.Drawing.Point(8, 112)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(157, 24)
        Me.btnCancel.TabIndex = 0
        Me.btnCancel.Text = "&Cancel"
        AddHandler Me.btnCancel.Click, New System.EventHandler(AddressOf Me.btnCancel_Click)
        ' 
        ' btnSkip
        ' 
        Me.btnSkip.Location = New System.Drawing.Point(349, 80)
        Me.btnSkip.Name = "btnSkip"
        Me.btnSkip.Size = New System.Drawing.Size(179, 24)
        Me.btnSkip.TabIndex = 1
        Me.btnSkip.Text = "&Skip"
        AddHandler Me.btnSkip.Click, New System.EventHandler(AddressOf Me.btnSkip_Click)
        ' 
        ' btnRename
        ' 
        Me.btnRename.Location = New System.Drawing.Point(8, 144)
        Me.btnRename.Name = "btnRename"
        Me.btnRename.Size = New System.Drawing.Size(157, 24)
        Me.btnRename.TabIndex = 2
        Me.btnRename.Text = "&Rename..."
        AddHandler Me.btnRename.Click, New System.EventHandler(AddressOf Me.btnRename_Click)
        ' 
        ' btnRetry
        ' 
        Me.btnRetry.Location = New System.Drawing.Point(8, 176)
        Me.btnRetry.Name = "btnRetry"
        Me.btnRetry.Size = New System.Drawing.Size(157, 24)
        Me.btnRetry.TabIndex = 3
        Me.btnRetry.Text = "Re&try"
        AddHandler Me.btnRetry.Click, New System.EventHandler(AddressOf Me.btnRetry_Click)
        ' 
        ' btnOverwrite
        ' 
        Me.btnOverwrite.Location = New System.Drawing.Point(8, 80)
        Me.btnOverwrite.Name = "btnOverwrite"
        Me.btnOverwrite.Size = New System.Drawing.Size(157, 24)
        Me.btnOverwrite.TabIndex = 4
        Me.btnOverwrite.Text = "&Overwrite"
        AddHandler Me.btnOverwrite.Click, New System.EventHandler(AddressOf Me.btnOverwrite_Click)
        ' 
        ' btnOverwriteAll
        ' 
        Me.btnOverwriteAll.Location = New System.Drawing.Point(180, 80)
        Me.btnOverwriteAll.Name = "btnOverwriteAll"
        Me.btnOverwriteAll.Size = New System.Drawing.Size(157, 24)
        Me.btnOverwriteAll.TabIndex = 5
        Me.btnOverwriteAll.Text = "Overwrite &All"
        AddHandler Me.btnOverwriteAll.Click, New System.EventHandler(AddressOf Me.btnOverwriteAll_Click)
        ' 
        ' btnOverwriteOlder
        ' 
        Me.btnOverwriteOlder.Location = New System.Drawing.Point(180, 112)
        Me.btnOverwriteOlder.Name = "btnOverwriteOlder"
        Me.btnOverwriteOlder.Size = New System.Drawing.Size(157, 24)
        Me.btnOverwriteOlder.TabIndex = 6
        Me.btnOverwriteOlder.Text = "Overwrite All Ol&der"
        AddHandler Me.btnOverwriteOlder.Click, New System.EventHandler(AddressOf Me.btnOverwriteOlder_Click)
        ' 
        ' btnOverwriteDiffSize
        ' 
        Me.btnOverwriteDiffSize.Location = New System.Drawing.Point(180, 144)
        Me.btnOverwriteDiffSize.Name = "btnOverwriteDiffSize"
        Me.btnOverwriteDiffSize.Size = New System.Drawing.Size(157, 24)
        Me.btnOverwriteDiffSize.TabIndex = 7
        Me.btnOverwriteDiffSize.Text = "Overwrite &If Size Differs"
        AddHandler Me.btnOverwriteDiffSize.Click, New System.EventHandler(AddressOf Me.btnOverwriteDiffSize_Click)
        ' 
        ' btnSkipAll
        ' 
        Me.btnSkipAll.Location = New System.Drawing.Point(349, 112)
        Me.btnSkipAll.Name = "btnSkipAll"
        Me.btnSkipAll.Size = New System.Drawing.Size(179, 24)
        Me.btnSkipAll.TabIndex = 8
        Me.btnSkipAll.Text = "S&kip All"
        AddHandler Me.btnSkipAll.Click, New System.EventHandler(AddressOf Me.btnSkipAll_Click)
        ' 
        ' btnFollowLink
        ' 
        Me.btnFollowLink.Location = New System.Drawing.Point(180, 176)
        Me.btnFollowLink.Name = "btnFollowLink"
        Me.btnFollowLink.Size = New System.Drawing.Size(157, 24)
        Me.btnFollowLink.TabIndex = 9
        Me.btnFollowLink.Text = "Follow &Link"
        AddHandler Me.btnFollowLink.Click, New System.EventHandler(AddressOf Me.btnFollowLink_Click)
        ' 
        ' lblMessage
        ' 
        Me.lblMessage.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblMessage.Location = New System.Drawing.Point(8, 8)
        Me.lblMessage.Name = "lblMessage"
        Me.lblMessage.Size = New System.Drawing.Size(530, 64)
        Me.lblMessage.TabIndex = 11
        Me.lblMessage.Text = "Message"
        Me.lblMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        ' 
        ' btnResume
        ' 
        Me.btnResume.Location = New System.Drawing.Point(349, 176)
        Me.btnResume.Name = "btnResume"
        Me.btnResume.Size = New System.Drawing.Size(179, 24)
        Me.btnResume.TabIndex = 12
        Me.btnResume.Text = "Resume"
        AddHandler Me.btnResume.Click, New System.EventHandler(AddressOf Me.btnResume_Click)
        ' 
        ' btnOverwriteDiffChecksum
        ' 
        Me.btnOverwriteDiffChecksum.Location = New System.Drawing.Point(349, 144)
        Me.btnOverwriteDiffChecksum.Name = "btnOverwriteDiffChecksum"
        Me.btnOverwriteDiffChecksum.Size = New System.Drawing.Size(179, 24)
        Me.btnOverwriteDiffChecksum.TabIndex = 13
        Me.btnOverwriteDiffChecksum.Text = "Overwrite If Checksum Differs"
        AddHandler Me.btnOverwriteDiffChecksum.Click, New System.EventHandler(AddressOf Me.btnOverwriteDiffChecksum_Click)
        ' 
        ' ProblemDetectedForm
        ' 
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(540, 208)
        Me.Controls.Add(Me.btnOverwriteDiffChecksum)
        Me.Controls.Add(Me.btnResume)
        Me.Controls.Add(Me.lblMessage)
        Me.Controls.Add(Me.btnFollowLink)
        Me.Controls.Add(Me.btnSkipAll)
        Me.Controls.Add(Me.btnOverwriteDiffSize)
        Me.Controls.Add(Me.btnOverwriteOlder)
        Me.Controls.Add(Me.btnOverwriteAll)
        Me.Controls.Add(Me.btnOverwrite)
        Me.Controls.Add(Me.btnRetry)
        Me.Controls.Add(Me.btnRename)
        Me.Controls.Add(Me.btnSkip)
        Me.Controls.Add(Me.btnCancel)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "ProblemDetectedForm"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Transfer Problem Detected"
        Me.ResumeLayout(False)

    End Sub
    #End Region

    Private Sub btnCancel_Click(sender As Object, e As System.EventArgs)
        _arguments.Cancel()
        Close()
    End Sub

    Private Sub btnSkip_Click(sender As Object, e As System.EventArgs)
        _arguments.Skip()
        Close()
    End Sub

    Private Sub btnSkipAll_Click(sender As Object, e As System.EventArgs)
        ' add the problem to the table of automatically skipped problems
        _skipProblemTypes.Add(_arguments.ProblemType, Nothing)

        _arguments.Skip()
        Close()
    End Sub

    Private Sub btnRetry_Click(sender As Object, e As System.EventArgs)
        _arguments.Retry()
        Close()
    End Sub

    Private Sub btnRename_Click(sender As Object, e As System.EventArgs)
        ' initialize the renaming form
        Dim formNewName As New NewNameForm()

        ' set the current file name
        Dim oldName As String = Path.GetFileName(_arguments.LocalPath)
        formNewName.FileName = oldName

        ' show the form
        Dim result As DialogResult = formNewName.ShowDialog(Me)

        ' get the new name
        Dim newName As String = formNewName.FileName

        ' check whether the user clicked on OK and insert something nonempty and something else
        If result <> DialogResult.OK OrElse newName.Length = 0 OrElse newName = oldName Then
            Return
        End If

        ' set the appropriate action and new name to the event arguments
        _arguments.Rename(newName)
        Close()
    End Sub

    Private Sub btnOverwrite_Click(sender As Object, e As System.EventArgs)
        _arguments.Overwrite()
        Close()
    End Sub

    Private Sub btnOverwriteAll_Click(sender As Object, e As System.EventArgs)
        _arguments.Overwrite()
        _overwriteAll = True
        Close()
    End Sub

    Private Sub btnOverwriteOlder_Click(sender As Object, e As System.EventArgs)
        _arguments.Overwrite(OverwriteCondition.Older)
        _overwriteOlder = True
        Close()
    End Sub

    Private Sub btnOverwriteDiffSize_Click(sender As Object, e As System.EventArgs)
        _arguments.Overwrite(OverwriteCondition.SizeDiffers)
        _overwriteDifferentSize = True
        Close()
    End Sub

    Private Sub btnOverwriteDiffChecksum_Click(sender As Object, e As EventArgs)
        _arguments.Overwrite(OverwriteCondition.ChecksumDiffers)
        _overwriteDifferentChecksum = True
        Close()
    End Sub

    Private Sub btnFollowLink_Click(sender As Object, e As System.EventArgs)
        _arguments.FollowLink()
        Close()
    End Sub

    Private Sub btnResume_Click(sender As Object, e As EventArgs)
        _arguments.[Resume]()
        Close()
    End Sub


End Class
