//
//  LeCountdownApp.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 20/01/2023.
//

import SwiftUI
import CoreData
import BackgroundTasks
import AVFoundation

enum BGTaskIdentifier : String {
    case refresh = "com.staxriver.lecountdown.refresh"
}

@main
struct LeCountdownApp: App {
    
    let persistenceController = PersistenceController.shared
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    init() {
        UITabBar.appearance().backgroundColor = UIColor(white: 0.96, alpha: 1.0)
        
        UIPageControl.appearance().currentPageIndicatorTintColor = .systemPink
        UIPageControl.appearance().pageIndicatorTintColor = UIColor(white: 0.7, alpha: 1.0)
        
        self._registerBackgroundRefreshes()
    }
    
    @Environment(\.scenePhase) var scenePhase
    
    #if os(iOS)
    @Environment(\.horizontalSizeClass) private var horizontalSizeClass
    #endif
    
    var body: some Scene {
        
        WindowGroup {
            Group {
                #if os(iOS)
                if horizontalSizeClass == .compact {
                    CompactHomeView()
                        .environment(\.managedObjectContext, persistenceController.container.viewContext)
                } else {
                    RegularHomeView()
                        .environment(\.managedObjectContext, persistenceController.container.viewContext)
                }
                #else
                RegularHomeView()
                    .environment(\.managedObjectContext, persistenceController.container.viewContext)
                #endif
            }
            .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
                self._willEnterForegroundNotification()
            }
            .onAppear {
                self._onAppear()
            }
            .onChange(of: scenePhase) { newPhase in
                switch newPhase {
                case .inactive:
                    Conductor.maestro.stopSoundIfPossible()
                default:
                    break
                }
            }
        }

    }
    
    fileprivate func _willEnterForegroundNotification() {
        Conductor.maestro.cleanupCountdowns()
    }
    
    fileprivate func _onAppear() {
        
        Sound.computeSoundDurationsIfNecessary()
        
//        let voices = AVSpeechSynthesisVoice.speechVoices()
//        let grouped = Dictionary(grouping: voices, by: { $0.language })
//        for language in grouped.keys {
//            if let lvoices = grouped[language] {
//                print("language = \(language)")
//                for voice in lvoices {
//                    print("name = \(voice.name), gender = \(voice.gender)")
//                }
//                print("========")
//            }
//        }
        
    }
    
    fileprivate func _registerBackgroundRefreshes() {
        BGTaskScheduler.shared.register(forTaskWithIdentifier: BGTaskIdentifier.refresh.rawValue, using: nil) { task in
             self._handleAppRefresh(task: task as! BGAppRefreshTask)
        }
    }
    
    fileprivate func _handleAppRefresh(task: BGAppRefreshTask) {
        
        print("_handleAppRefresh = \(task.description)")
        
        task.expirationHandler = {
            print("expired")
        }
        
        DispatchQueue.main.async {
            Conductor.maestro.updateLiveActivities()
            task.setTaskCompleted(success: true)
        }
        
    }
    
}
