Imports System
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Reflection
Imports System.Resources
Imports System.Globalization

Imports Stax
Imports Stax.UI

<Serializable()> _
Public Class ApplicationSettings
    Implements ISafeSerialization

    Public AudioProfiles As List(Of AudioProfile)
    Public AviSynthCategories As List(Of AviSynthCategory)
    Public AviSynthProfiles As List(Of TargetAviSynthDocument)
    Public DownloadDir As New SettingBag(Of String)("Downloads")
    Public ExtractDir As New SettingBag(Of String)("Applications")
    Public GenericMenuMainForm As GenericMenuItem
    Public GenericMenuPreview As GenericMenuItem
    Public GenericMenuSize As GenericMenuItem
    Public HidePreviewButtons As Boolean
    Public Jobs As Dictionary(Of String, Boolean)
    Public LastSourceDir As String
    Public MuxerProfiles As List(Of Muxer)
    Public PackagePaths As Dictionary(Of String, String)
    Public ProcessPriority As New SettingBag(Of ProcessPriorityClass)(ProcessPriorityClass.Idle)
    Public RecentFramePositions As List(Of String)
    Public RecentOptionsPage As String
    Public RecentPackageSearchDirs As List(Of String)
    Public RecentProjects As List(Of String)
    Public ResizeSliderMax As New SettingBag(Of Integer)(720)
    Public ResizeSliderMin As New SettingBag(Of Integer)(320)
    Public RunAppsMinimized As New SettingBag(Of Boolean)(False)
    Public Storage As ObjectStorage
    Public StringDictionary As Dictionary(Of String, String)
    Public StringList As List(Of String)
    Public Versions As Dictionary(Of String, Integer)
    Public VideoEncoderProfiles As List(Of VideoEncoder)
    Public WindowPositions As New WindowPositions
    Public WindowStatePreview As FormWindowState
    Public WrongVersionWarnings As List(Of String)
    Public YV12Decoder As New SettingBag(Of String)("xvidvfw.dll")
    Public VersionHelpTemplates As String

    Private IsDirtyValue As Boolean

    Public Property IsDirty() As Boolean Implements Stax.ISafeSerialization.IsDirty
        Get
            Return IsDirtyValue
        End Get
        Set(ByVal value As Boolean)
            IsDirtyValue = value
        End Set
    End Property

    Public ReadOnly Property VersionsProperty() As Dictionary(Of String, Integer) Implements ISafeSerialization.Versions
        Get
            Return Versions
        End Get
    End Property

    Private Function Check(ByVal obj As Object, ByVal key As String, ByVal minimalVersion As Integer, ByVal currentVersion As Integer, ByVal settingName As String) As Boolean
        Return SafeSerialization.Check(Me, obj, key, minimalVersion, currentVersion, settingName)
    End Function

    Public Sub Init() Implements ISafeSerialization.Init
        If Versions Is Nothing Then
            Versions = New Dictionary(Of String, Integer)
        End If

        If Storage Is Nothing Then
            Storage = New ObjectStorage
        End If

        If Check(VideoEncoderProfiles, "VideoEncoderProfiles", 16, 16, "Video Encoder Profiles") Then
            VideoEncoderProfiles = VideoEncoder.GetDefaults()
        End If

        If Check(AudioProfiles, "AudioProfiles", 22, 23, "Audio Profiles") Then
            AudioProfiles = AudioProfile.GetDefaults(False)
        End If

        If PackagePaths Is Nothing Then
            PackagePaths = New Dictionary(Of String, String)
        End If

        If RecentProjects Is Nothing Then
            RecentProjects = New List(Of String)
        End If

        If Check(MuxerProfiles, "Container Profiles", 3, 3, Nothing) Then
            MuxerProfiles = New List(Of Muxer)
            MuxerProfiles.AddRange(Muxer.GetDefaults(False))
        End If

        If StringDictionary Is Nothing Then
            StringDictionary = New Dictionary(Of String, String)
        End If

        If RecentOptionsPage Is Nothing Then
            RecentOptionsPage = ""
        End If

        If Jobs Is Nothing Then
            Jobs = New Dictionary(Of String, Boolean)
        End If

        If LastSourceDir Is Nothing Then
            LastSourceDir = ""
        End If

        If WrongVersionWarnings Is Nothing Then
            WrongVersionWarnings = New List(Of String)
        End If

        If RecentPackageSearchDirs Is Nothing Then
            RecentPackageSearchDirs = New List(Of String)
        End If

        If StringList Is Nothing Then
            StringList = New List(Of String)
        End If

        If RecentFramePositions Is Nothing Then
            RecentFramePositions = New List(Of String)
        End If

        'search 'obsolete
        If Check(GenericMenuMainForm, "GenericMenuMainForm", 5, 5, "Main Menu") Then
            GenericMenuMainForm = MainForm.GetDefaultMainMenu
        End If

        'search 'obsolete
        If GenericMenuPreview Is Nothing Then
            GenericMenuPreview = PreviewForm.GetDefaultMenu
        End If

        If Check(GenericMenuSize, "Target size menu in main dialog", 0, 1, Nothing) Then
            GenericMenuSize = MainForm.GetDefaultMenuSize
        End If

        If Check(AviSynthCategories, "AviSynthCategories", 0, 1, "Filter Profiles") Then
            AviSynthCategories = New List(Of AviSynthCategory)

            Dim src As New AviSynthCategory("Source")
            src.Filters.Add(New AviSynthFilter("Source", "MPEG-2", "MPEG2Source(""%source_file%"")", True))
            src.Filters.Add(New AviSynthFilter("Source", "AVI", "AVISource(%source_files_comma%,audio=false)", True))
            src.Filters.Add(New AviSynthFilter("Source", "DirectShow", "DirectShowSource(""%source_file%"")", True))

            AviSynthCategories.Add(src)

            Dim crop As New AviSynthCategory("Crop")
            crop.Filters.Add(New AviSynthFilter("Crop", "Crop Borders", "Crop(%crop_left%,%crop_top%,-%crop_right%,-%crop_bottom%)", True))
            AviSynthCategories.Add(crop)

            Dim field As New AviSynthCategory("Field")
            field.Filters.Add(New AviSynthFilter("Field", "Deinterlace", "FieldDeinterlace()", True))
            field.Filters.Add(New AviSynthFilter("Field", "Fast Deinterlace", "SeparateFields().SelectEven()", True))
            field.Filters.Add(New AviSynthFilter("Field", "IVTC", "Telecide(guide=1).Decimate()", True))
            AviSynthCategories.Add(field)

            Dim resize As New AviSynthCategory("Resize")
            resize.Filters.Add(New AviSynthFilter("Resize", "Sharp", "LanczosResize(%target_width%,%target_height%)", True))
            resize.Filters.Add(New AviSynthFilter("Resize", "Neutral", "BicubicResize(%target_width%,%target_height%,0,0.5)", True))
            resize.Filters.Add(New AviSynthFilter("Resize", "Soft", "BilinearResize(%target_width%,%target_height%)", True))
            AviSynthCategories.Add(resize)

            Dim noise As New AviSynthCategory("Noise")
            noise.Filters.Add(New AviSynthFilter("Noise", "Low", "FluxSmoothT(2)", True))
            noise.Filters.Add(New AviSynthFilter("Noise", "Medium", "FluxSmoothT(4)", True))
            noise.Filters.Add(New AviSynthFilter("Noise", "Heavy", "FluxSmoothT(8)", True))
            noise.Filters.Add(New AviSynthFilter("Noise", "Extreme", "UnDot.Deen()", True))
            AviSynthCategories.Add(noise)

            Dim subtitle As New AviSynthCategory("Subtitle")
            subtitle.Filters.Add(New AviSynthFilter("Subtitle", "VobSub", "VobSub()", True))
            subtitle.Filters.Add(New AviSynthFilter("Subtitle", "TextSub", "TextSub()", True))
            AviSynthCategories.Add(subtitle)

            Dim misc As New AviSynthCategory("Misc")
            misc.Filters.Add(New AviSynthFilter("Misc", "ConvertToRGB()", "ConvertToRGB()", True))
            misc.Filters.Add(New AviSynthFilter("Misc", "ConvertToYV12()", "ConvertToYV12()", True))
            AviSynthCategories.Add(misc)
        End If

        If Check(AviSynthProfiles, "AviSynthProfiles", 0, 5, "Filter Setup Profiles") Then
            AviSynthProfiles = AviSynthDocument.GetDefaults
        End If

        'can later be removed
        If Directory.Exists(Paths.SettingsDir + "Templates") Then
            VersionHelpTemplates = ""
        End If

        If Check(VersionHelpTemplates, "Templates", 0, 10, Nothing) Then
            VersionHelpTemplates = ""
            Directory.Delete(Paths.TemplateDir, True)
        End If
    End Sub
End Class