//
//  SailassistApp.swift
//  Sailassist
//
//  Created by Mamoru Sugita on 2023/10/13.
//

import SwiftUI

@main
struct SailassistApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) private var delegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate{
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        let config = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
        config.delegateClass = SceneDelegate.self
        return config
    }
}

//@Observable
class SceneDelegate: UIResponder, UIWindowSceneDelegate, ObservableObject {
    weak var windowScene: UIWindowScene?
    var tabWindow: UIWindow?
    var overlayWindow: UIWindow?
    var tag : Int = 0
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        windowScene = scene as? UIWindowScene
        setupOverlayWindow()
    }
    
    
    func addTabBar(_ selectedTabModel: SelectedTabModel){
        guard let scene = windowScene else{
            return
        }
        
        let tabBarController = UIHostingController(rootView:
            CustomTabBar()
                .environmentObject(selectedTabModel)
                .frame(maxHeight: .infinity, alignment: .bottom)
        )
        
        tabBarController.view.backgroundColor = .clear
        
        let tabWindow = PassThroughWindow(windowScene: scene)
        tabWindow.rootViewController = tabBarController
        tabWindow.isHidden = false
        
        self.tabWindow = tabWindow
    }
    
    func setupOverlayWindow(){
        guard let windowScene = windowScene else { return }
        let window = UIWindow(windowScene: windowScene)
        window.isHidden = true
        window.isUserInteractionEnabled = false
        self.overlayWindow = window
    }
    
    fileprivate func alert<Content: View>(config: Binding<AlertConfig>, @ViewBuilder content: @escaping () -> Content, viewTag: @escaping (Int) -> ()) {
        
        guard let alertWindow = overlayWindow else { return }
        let viewController = UIHostingController(rootView:
            AlertView(config: config, tag: tag, content: {
                content()
            })
        )
        viewController.view.backgroundColor = .clear
        viewController.view.tag = tag
        viewTag(tag)
        tag += 1
        
        if alertWindow.rootViewController == nil {
            alertWindow.rootViewController = viewController
            alertWindow.isHidden = false
            alertWindow.isUserInteractionEnabled = true
        }
    }
}


class PassThroughWindow: UIWindow{
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        guard let view = super.hitTest(point, with: event) else { return nil}
        return rootViewController?.view == view ? nil : view
    }
}


struct AlertConfig {
    var enableBackgroundBlur: Bool = true
    var disableOutsideTap : Bool = true
    var transitionType: TransitionType = .slide
    var slideEdge : Edge = .bottom
    
    private var show : Bool = false
    
    enum TransitionType{
        case slide
        case opacity
    }
    
    mutating
    func present(){
        show = true
    }
    
    mutating
    func dismiss(){
        show = false
    }
}

fileprivate struct AlertView<Content: View>: View {
    @Binding var config: AlertConfig
    
    var tag: Int
    @ViewBuilder var content: () -> Content
    
    var body: some View {
        GeometryReader(content: { geometry in
            Color.red
        })
    }
}

extension View {
    @ViewBuilder
    func alert<Content: View>(config: Binding<AlertConfig>, @ViewBuilder content: @escaping () -> Content, viewTag: @escaping (Int) -> ()) -> some View {
        self
    }
}

fileprivate struct AlertModifier<AlertContent: View>: ViewModifier {
    @Binding var config: AlertConfig
    @ViewBuilder var alertContent: () -> AlertContent
    
    @Environment private var sceneDelegate: SceneDelegate
    
    @State private var viewTag: Int = 0
    func body(content: Content) -> some View {
        content
    }
}

