//
//  SoundHolder.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 09/02/2023.
//

import Foundation
import SwiftUI

protocol SoundHolder {
    func selectSound(_ sound: Sound, selected: Bool)
    func selectPlaylist(_ playlist: Playlist, selected: Bool)
}

class TimerModel : ObservableObject, SoundHolder {
    
    @Published var playlists: Set<Playlist> = []
    @Published var sounds: Set<Sound> = [] {
        didSet {
            self._selectPlaylists()
        }
    }
    @Published var group: CountdownIntervalGroup =
        CountdownIntervalGroup(repeatCount: 0, intervals: [])

    var soundSelection: String {
        if !sounds.isEmpty {
            if sounds.count == 1 {
                return sounds.first!.localizedString
            } else {
                return "\(sounds.count) \(NSLocalizedString("sounds", comment: "sounds"))"
            }
        }
        return ""
    }
    
    func binding(sound: Sound) -> Binding<Bool> {
        return Binding<Bool>(get: {
            return self.sounds.contains(sound)
        }, set: { value in
            if value { self.sounds.insert(sound) }
            else { self.sounds.remove(sound) }
        })
    }
    
    func binding(playlist: Playlist) -> Binding<Bool> {
        return Binding<Bool>(get: {
            return self.playlists.contains(playlist)
        }, set: { value in
            if value { self.playlists.insert(playlist) }
            else { self.playlists.remove(playlist) }
        })
    }
    
    // MARK: - SoundHolder
    
    func selectSound(_ sound: Sound, selected: Bool) {
        
        if selected {
            self.sounds.insert(sound)
        } else {
            self.sounds.remove(sound)
        }
        
        self._togglePlaylist(sound.playlist)
    }
    
    fileprivate func _selectPlaylists() {
        let playlists: Set<Playlist> = Set(self.sounds.map { $0.playlist })
        for playlist in playlists {
            self._togglePlaylist(playlist)
        }
    }
    
    fileprivate func _togglePlaylist(_ playlist: Playlist) {
        // toggle playlist if necessary
        let playlistSounds: [Sound] = SoundCatalog.main.sounds(for: playlist)
        if self.sounds.isSuperset(of: playlistSounds) {
            self.playlists.insert(playlist)
        } else {
            self.playlists.remove(playlist)
        }

    }
    
    func selectPlaylist(_ playlist: Playlist, selected: Bool) {

        let sounds: [Sound] = SoundCatalog.main.sounds(for: playlist)
        if selected {
            self.playlists.insert(playlist)
            self.sounds.formUnion(sounds)
        } else {
            self.playlists.remove(playlist)
            if self.sounds.isSuperset(of: sounds) {
                self.sounds.formSymmetricDifference(sounds)
            }
        }
        
    }
    
//    func isSelected(sound: Sound) -> Bool {
//        self.sounds.contains(sound)
//    }
//    
//    func isSelected(playlist: Playlist) -> Bool {
//        self.playlists.contains(playlist)
//    }
    
}
