Imports System.Collections

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

Imports Stax
Imports Stax.UI
Imports Stax.Win32
Imports Stax.DynamicObject

<Serializable()> _
Public Class Project
    Implements ISafeSerialization

    Public Audio0 As AudioSettings
    Public Audio1 As AudioSettings
    Public AutoARSignaling As New SettingBag(Of Boolean)(True)
    Public AutoSmartCrop As New SettingBag(Of Boolean)(False)
    Public AutoCompCheck As Boolean
    Public AutoCrop As Boolean = True
    Public AutoResizeImage As Integer = 195000
    Public AutoSourceFiles As List(Of String)
    Public BatchMode As New SettingBag(Of Boolean)(False)
    Public CompCheckAction As CompCheckAction = StaxRip.CompCheckAction.AdjustImageSize
    Public Compressibility As Double
    Public CompCheckRange As New SettingBag(Of Integer)(5)
    Public CropBottom As Integer
    Public CropLeft As Integer
    Public CropRight As Integer
    Public CropTop As Integer
    Public ITU As New SettingBag(Of Boolean)(True)
    Public Name As String
    Public Preparers As List(Of Preparer)
    Public PreselectionSubtitleLanguages As List(Of Language)
    Public Ranges As List(Of Range)
    Public Size As Integer = 700
    Public SkippedAssistantTips As List(Of String)
    Public Source169 As Boolean
    Public SourceAviSynthDocument As SourceAviSynthDocument
    Public SourceFile As String
    Public SourceFiles As List(Of String)
    Public SourceFrames As Integer
    Public SourceFramerate As Double
    Public SourceHeight As Integer
    Public SourceSeconds As Integer
    Public SourceWidth As Integer
    Public Storage As ObjectStorage
    Public TargetAVSDoc As TargetAviSynthDocument
    Public TargetFile As String
    Public TargetHeight As Integer = 416
    Public TargetSeconds As Integer = 5400
    Public TargetWidth As Integer = 512
    Public VideoBitrate As Integer = 1000
    Public VideoEncoder As VideoEncoder
    Public Versions As Dictionary(Of String, Integer)

    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 PreselectionSubtitleLanguages Is Nothing Then
            PreselectionSubtitleLanguages = New List(Of Language)
            PreselectionSubtitleLanguages.Add(New Language())

            If CultureInfo.CurrentCulture.TwoLetterISOLanguageName <> "en" Then
                PreselectionSubtitleLanguages.Add(New Language(New CultureInfo("en")))
            End If
        End If

        If Ranges Is Nothing Then
            Ranges = New List(Of Range)
        End If

        If TargetFile Is Nothing Then
            TargetFile = ""
        End If

        If SourceAviSynthDocument Is Nothing Then
            SourceAviSynthDocument = New SourceAviSynthDocument
        End If

        If Name Is Nothing Then
            Name = ""
        End If

        If SourceFile Is Nothing Then
            SourceFile = ""
        End If

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

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

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

        If Check(Preparers, "Preparers", 6, 9, Nothing) Then
            Preparers = Preparer.GetDefaults(False)
        End If

        If Check(Audio0, "Audio0", 7, 7, "Audio Track 1") Then
            Audio0 = New AudioSettings(AudioProfile.GetDefaults(True)(0), New Language())
        End If

        If Check(Audio1, "Audio1", 7, 7, "Audio Track 2") Then
            Audio1 = New AudioSettings(New NullAudioProfile, New Language(New CultureInfo("en")))
        End If

        If Check(VideoEncoder, "VideoEncoder", 5, 5, "Video Encoder") Then
            Dim xvid As New XviDVirtualDubEncoder
            xvid.Name = "XviD - Exact File Size"
            xvid.Passes = 2
            xvid.QualityMode = False
            xvid.SetTwoPassRegDefaults()
            xvid.SetCompCheckRegDefaults()
            VideoEncoder = xvid
        End If

        If TargetAVSDoc Is Nothing Then
            TargetAVSDoc = AviSynthDocument.GetDefaults()(0)
        End If
    End Sub
End Class