import Foundation

final class Settings: ObservableObject {

    @Published
    var paths: PathSettings

    /// The items to show in the navigation bar
    @Published
    var navigation: NavigationSettings

    @Published
    var posts: PostSettings

    @Published
    var pages: PageSettings

    @Published
    var audioPlayer: AudioPlayerSettings

    @Published
    var german: LocalizedPostSettings

    @Published
    var english: LocalizedPostSettings


    init(paths: PathSettings,
         navigation: NavigationSettings,
         posts: PostSettings,
         pages: PageSettings,
         audioPlayer: AudioPlayerSettings,
         german: LocalizedPostSettings,
         english: LocalizedPostSettings) {
        self.paths = paths
        self.navigation = navigation
        self.posts = posts
        self.pages = pages
        self.audioPlayer = audioPlayer
        self.german = german
        self.english = english
    }

    func localized(in language: ContentLanguage) -> LocalizedPostSettings {
        switch language {
        case .english: return english
        case .german: return german
        }
    }

    init(file: SettingsFile, files: [String : FileResource], map: (String) -> Item?) {
        self.navigation = NavigationSettings(file: file.navigation, map: map)

        self.posts = PostSettings(file: file.posts, files: files)
        self.pages = PageSettings(file: file.pages, files: files)
        self.paths = PathSettings(file: file.paths)
        self.audioPlayer = .init(file: file.audioPlayer, files: files)

        self.german = .init(file: file.german)
        self.english = .init(file: file.english)
    }

    var file: SettingsFile {
        .init(
            paths: paths.file,
            navigation: navigation.file,
            posts: posts.file,
            pages: pages.file,
            audioPlayer: audioPlayer.file,
            german: german.file,
            english: english.file)
    }
}

extension Settings {

    static let `default`: Settings = .init(
        paths: .default,
        navigation: .default,
        posts: .default,
        pages: .default,
        audioPlayer: .default,
        german: .german,
        english: .english)
}
