import Foundation

final class PageSettings: ObservableObject {

    @Published
    var contentWidth: Int

    @Published
    var largeImageWidth: Int

    @Published
    var pageLinkImageSize: Int

    @Published
    var defaultCssFile: FileResource?

    @Published
    var codeHighlightingJsFile: FileResource?

    @Published
    var modelViewerJsFile: FileResource?

    @Published
    var imageCompareJsFile: FileResource?

    @Published
    var imageCompareCssFile: FileResource?

    @Published
    var manifestFile: FileResource?

    @Published
    var routeJsFile: FileResource?

    @Published
    var german: LocalizedPageSettings

    @Published
    var english: LocalizedPageSettings

    init(contentWidth: Int,
         largeImageWidth: Int,
         pageLinkImageSize: Int,
         defaultCssFile: FileResource? = nil,
         codeHighlightingJsFile: FileResource? = nil,
         modelViewerJsFile: FileResource? = nil,
         imageCompareJsFile: FileResource? = nil,
         imageCompareCssFile: FileResource? = nil,
         manifestFile: FileResource? = nil,
         routeJsFile: FileResource? = nil,
         german: LocalizedPageSettings,
         english: LocalizedPageSettings) {
        self.contentWidth = contentWidth
        self.largeImageWidth = largeImageWidth
        self.pageLinkImageSize = pageLinkImageSize
        self.defaultCssFile = defaultCssFile
        self.codeHighlightingJsFile = codeHighlightingJsFile
        self.modelViewerJsFile = modelViewerJsFile
        self.imageCompareJsFile = imageCompareJsFile
        self.imageCompareCssFile = imageCompareCssFile
        self.manifestFile = manifestFile
        self.routeJsFile = routeJsFile
        self.german = german
        self.english = english
    }

    func remove(_ file: FileResource) {
        if codeHighlightingJsFile == file {
            codeHighlightingJsFile = nil
        }
        if modelViewerJsFile == file {
            modelViewerJsFile = nil
        }
        if defaultCssFile == file {
            defaultCssFile = nil
        }
        if imageCompareJsFile == file {
            imageCompareJsFile = nil
        }
        if imageCompareCssFile == file {
            imageCompareCssFile = nil
        }
        if manifestFile == file {
            manifestFile = nil
        }
        if routeJsFile == file {
            routeJsFile = nil
        }
    }
}

// MARK: Storage

extension PageSettings {

    convenience init(context: LoadingContext, data: Data) {
        self.init(
            contentWidth: data.contentWidth,
            largeImageWidth: data.largeImageWidth,
            pageLinkImageSize: data.pageLinkImageSize,
            defaultCssFile: data.defaultCssFile.map(context.file),
            codeHighlightingJsFile: data.codeHighlightingJsFile.map(context.file),
            modelViewerJsFile: data.modelViewerJsFile.map(context.file),
            imageCompareJsFile: data.imageCompareJsFile.map(context.file),
            imageCompareCssFile: data.imageCompareCssFile.map(context.file),
            manifestFile: data.manifestFile.map(context.file),
            routeJsFile: data.routeJsFile.map(context.file),
            german: .init(data: data.german),
            english: .init(data: data.english))
    }

    var data: Data {
        .init(contentWidth: contentWidth,
              largeImageWidth: largeImageWidth,
              pageLinkImageSize: pageLinkImageSize,
              defaultCssFile: defaultCssFile?.identifier,
              codeHighlightingJsFile: codeHighlightingJsFile?.identifier,
              modelViewerJsFile: modelViewerJsFile?.identifier,
              imageCompareJsFile: imageCompareJsFile?.identifier,
              imageCompareCssFile: imageCompareCssFile?.identifier,
              manifestFile: manifestFile?.identifier,
              routeJsFile: routeJsFile?.identifier,
              german: german.data,
              english: english.data)
    }

    struct Data: Codable, Equatable {
        let contentWidth: Int
        let largeImageWidth: Int
        let pageLinkImageSize: Int
        let defaultCssFile: String?
        let codeHighlightingJsFile: String?
        let modelViewerJsFile: String?
        let imageCompareJsFile: String?
        let imageCompareCssFile: String?
        let manifestFile: String?
        let routeJsFile: String?
        let german: LocalizedPageSettings.Data
        let english: LocalizedPageSettings.Data
    }

}

extension PageSettings: LocalizedItem {
    
}
