import Foundation

final class PostSettings: ObservableObject {

    /// The number of posts to show in a single page of the news feed
    @Published
    var postsPerPage: Int

    /// The maximum width of the main content
    @Published
    var contentWidth: Int

    @Published
    var swiperCssFile: FileResource?

    @Published
    var swiperJsFile: FileResource?

    @Published
    var defaultCssFile: FileResource?

    init(postsPerPage: Int,
         contentWidth: Int,
         swiperCssFile: FileResource?,
         swiperJsFile: FileResource?,
         defaultCssFile: FileResource?) {
        self.postsPerPage = postsPerPage
        self.contentWidth = contentWidth
        self.swiperCssFile = swiperCssFile
        self.swiperJsFile = swiperJsFile
        self.defaultCssFile = defaultCssFile
    }

    // MARK: Storage
    
    init(file: PostSettingsFile, files: [String : FileResource]) {
        self.postsPerPage = file.postsPerPage
        self.contentWidth = file.contentWidth
        self.swiperCssFile = file.swiperCssFile.map { files[$0] }
        self.swiperJsFile = file.swiperJsFile.map { files[$0] }
        self.defaultCssFile = file.defaultCssFile.map { files[$0] }
    }

    var file: PostSettingsFile {
        .init(postsPerPage: postsPerPage,
              contentWidth: contentWidth,
              swiperCssFile: swiperCssFile?.id,
              swiperJsFile: swiperJsFile?.id,
              defaultCssFile: defaultCssFile?.id)
    }
}
