//
//  Werkout_iosApp.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/13/23.
//

import SwiftUI
import Combine
import AVKit


struct Constants {
    static let phoneThotStyle = "phoneThotStyle"
    static let extThotStyle = "extThotStyle"
    static let extShowNextVideo = "extShowNextVideo"
    static let thotGenderOption = "thotGenderOption"
}

@main
struct Werkout_iosApp: App {
    let persistenceController = PersistenceController.shared
    @State var additionalWindows: [UIWindow] = []
    @State private var tabSelection = 1
    
    let pub = NotificationCenter.default.publisher(for: NSNotification.Name("CreatedNewWorkout"))
    
    private var screenDidConnectPublisher: AnyPublisher<UIScreen, Never> {
        NotificationCenter.default
            .publisher(for: UIScreen.didConnectNotification)
            .compactMap { $0.object as? UIScreen }
            .receive(on: RunLoop.main)
            .eraseToAnyPublisher()
    }
    
    private var screenDidDisconnectPublisher: AnyPublisher<UIScreen, Never> {
        NotificationCenter.default
            .publisher(for: UIScreen.didDisconnectNotification)
            .compactMap { $0.object as? UIScreen }
            .receive(on: RunLoop.main)
            .eraseToAnyPublisher()
    }
    
    var body: some Scene {
        WindowGroup {
            TabView(selection: $tabSelection) {
                AllWorkoutsView()
                    .onReceive(
                        screenDidConnectPublisher,
                        perform: screenDidConnect
                    )
                    .onReceive(
                        screenDidDisconnectPublisher,
                        perform: screenDidDisconnect
                    )
                    .tabItem {
                        Label("All Workouts", systemImage: "figure.strengthtraining.traditional")
                    }
                    .tag(1)
                
                CreateWorkoutMainView()
                    .tabItem {
                        Label("Create Workout", systemImage: "plus.app.fill")
                    }
                    .tag(2)
                
                AccountView()
                    .tabItem {
                        Label("Accounts", systemImage: "person.fill.turn.down")
                    }
                    .tag(3)
            }
            .accentColor(Color("appColor"))
            .onAppear{
                UIApplication.shared.isIdleTimerDisabled = true
                _ = try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: .default, options: .mixWithOthers)
//                UserStore.shared.logout()
            }
            .onReceive(pub) { (output) in
                self.tabSelection = 1
            }
        }
    }

    private func screenDidDisconnect(_ screen: UIScreen) {
        additionalWindows.removeAll { $0.screen == screen }
        BridgeModule.shared.isShowingOnExternalDisplay = false
    }
    
    private func screenDidConnect(_ screen: UIScreen) {
        let window = UIWindow(frame: screen.bounds)
        
        window.windowScene = UIApplication.shared.connectedScenes
            .first { ($0 as? UIWindowScene)?.screen == screen }
        as? UIWindowScene
        
        let view = ExternalWorkoutDetailView()
            .preferredColorScheme(.dark)
        let controller = UIHostingController(rootView: view)
        window.rootViewController = controller
        window.isHidden = false
        additionalWindows.append(window)
        BridgeModule.shared.isShowingOnExternalDisplay = true
    }
}
