Imports System.Windows.Forms
Imports DevExpress.Utils.Menu
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.ZooFlow
Imports DigitalData.Modules.EDMI.API.Rights
Imports DigitalData.Modules.EDMI.API
Imports DigitalData.GUIs.Common

Public Class DocumentPropertyMenu
    Private ReadOnly _Logger As Logger
    Private ReadOnly _LogConfig As LogConfig
    Private ReadOnly _Environment As Environment
    Private ReadOnly _Client As Client
    Private ReadOnly _File As Modules.Windows.File

    Private ReadOnly _FilePath As String
    Private ReadOnly _ObjectId As Long

    Public Const OPEN_FILE As String = "Datei öffnen"
    Public Const OPEN_DIRECTORY As String = "Ordner öffnen"
    Public Const COPY_FILE_PATH As String = "Dateipfad kopieren"
    Public Const COPY_FOLDER_PATH As String = "Ordnerpfad kopieren"
    Public Const OPEN_PROPERTIES As String = "Eigenschaften"

    Public Event FileOpened As EventHandler(Of String)
    Public Event FileClosed As EventHandler(Of String)

    Public Sub New(LogConfig As LogConfig, Environment As Environment, Client As Client, FilePath As String, Optional ObjectId As Long = 0)
        _LogConfig = LogConfig
        _Logger = LogConfig.GetLogger()
        _Environment = Environment
        _Client = Client
        _FilePath = FilePath
        _ObjectId = ObjectId
        _File = New Modules.Windows.File(LogConfig)
    End Sub

    Public Function GetMenuItems(OperationMode As IResultForm.Mode, AccessRight As AccessRight) As List(Of DXMenuItem)
        If OperationMode = IResultForm.Mode.NoAppServer Then
            Return GetLegacyMenuItems()
        Else
            Return GetIDBMenuItems(AccessRight)
        End If
    End Function

    Public Function GetIDBMenuItems(AccessRight As AccessRight) As List(Of DXMenuItem)
        Dim oOpenFile = New DXMenuItem(OPEN_FILE, AddressOf OpenFile_Click)
        Dim oOpenFolder = New DXMenuItem(OPEN_DIRECTORY, AddressOf OpenFolder_Click)
        Dim oCopyFilePath = New DXMenuItem(COPY_FILE_PATH, AddressOf CopyFilePath_Click)
        Dim oCopyFolderPath = New DXMenuItem(COPY_FOLDER_PATH, AddressOf CopyFolderPath_Click)
        Dim oProperties = New DXMenuItem(OPEN_PROPERTIES, AddressOf Properties_Click)

        If AccessRight = AccessRight.VIEW_ONLY Then
            Return New List(Of DXMenuItem) From {
                oProperties
            }
        Else
            Return New List(Of DXMenuItem) From {
                oOpenFile,
                oOpenFolder,
                oCopyFilePath,
                oCopyFolderPath,
                oProperties
            }
        End If
    End Function

    Public Function GetLegacyMenuItems() As List(Of DXMenuItem)
        Dim oOpenFile = New DXMenuItem(OPEN_FILE, AddressOf OpenFile_Click)
        Dim oOpenFolder = New DXMenuItem(OPEN_DIRECTORY, AddressOf OpenFolder_Click)
        Dim oCopyFilePath = New DXMenuItem(COPY_FILE_PATH, AddressOf CopyFilePath_Click)
        Dim oCopyFolderPath = New DXMenuItem(COPY_FOLDER_PATH, AddressOf CopyFolderPath_Click)
        Dim oProperties = New DXMenuItem(OPEN_PROPERTIES, AddressOf Properties_Click_Legacy)

        Return New List(Of DXMenuItem) From {
            oOpenFile,
            oOpenFolder,
            oCopyFilePath,
            oCopyFolderPath,
            oProperties
        }
    End Function

    Public Sub Properties_Click(sender As Object, e As EventArgs)
        If TestObjectIdExists(OPEN_PROPERTIES) = False Then
            Exit Sub
        End If

        Dim oPropertyDialog As New frmObjectPropertyDialog(_LogConfig, _Environment, _Client, _ObjectId)
        oPropertyDialog.Show()
    End Sub

    Public Sub Properties_Click_Legacy(sender As Object, e As EventArgs)
        If TestPathExists(OPEN_PROPERTIES) = False Then
            Exit Sub
        End If

        _File.OpenFileProperties(_FilePath)
    End Sub

    Public Sub CopyFilePath_Click(sender As Object, e As EventArgs)
        Clipboard.SetText(_FilePath)
    End Sub

    Public Sub CopyFolderPath_Click(sender As Object, e As EventArgs)
        Dim oFolderPath = IO.Path.GetDirectoryName(_FilePath)
        Clipboard.SetText(oFolderPath)
    End Sub

    Public Sub OpenFile_Click(sender As Object, e As EventArgs)
        If TestPathExists(OPEN_FILE) = False Then
            Exit Sub
        End If

        Try
            RaiseEvent FileOpened(Me, _FilePath)
        Catch ex As Exception
            _Logger.Error(ex)
        End Try
    End Sub

    Public Sub OpenFolder_Click(sender As Object, e As EventArgs)
        If TestPathExists(OPEN_DIRECTORY) = False Then
            Exit Sub
        End If

        Dim oArgs As String = $"/e, /select, ""{_FilePath}"""
        Dim oInfo As New ProcessStartInfo() With {
            .Arguments = oArgs,
            .FileName = "explorer"
        }

        Process.Start(oInfo)
    End Sub

    Private Function TestPathExists(Title As String) As Boolean
        If IO.File.Exists(_FilePath) = False Then
            MessageBox.Show($"Datei {_FilePath} existiert nicht oder wurde verschoben!", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return False
        Else
            Return True
        End If
    End Function

    Private Function TestObjectIdExists(Title As String) As Boolean
        If _ObjectId = 0 Then
            MessageBox.Show($"Objekt {_ObjectId} existiert nicht oder wurde verschoben!", Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return False
        Else
            Return True
        End If
    End Function
End Class
