//
//  punchnetApp.swift
//  punchnet
//
//  Created by 安礼成 on 2025/5/12.
//

import SwiftUI
import AppKit
import SwiftData
import Combine

@main
struct punchnetApp: App {
    /*
    var sharedModelContainer: ModelContainer = {
        let schema = Schema([
            Item.self,
        ])
        let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)

        do {
            return try ModelContainer(for: schema, configurations: [modelConfiguration])
        } catch {
            fatalError("Could not create ModelContainer: \(error)")
        }
    }()
     */
    
    @Environment(\.openWindow) private var openWindow
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    @AppStorage("token") var token: String = ""
    @AppStorage("network_code") var networkCode: String = ""
    @AppStorage("hostname") var hostname: String = ""
    
    private var noticeServer: UDPNoticeCenterServer
    @State private var appContext: AppContext
    @State private var userContext = UserContext()
    
    init() {
        self.noticeServer = UDPNoticeCenterServer()
        self.noticeServer.start()
        
        self.appContext = AppContext(noticePort: self.noticeServer.port)
    }
    
    var body: some Scene {
        WindowGroup(id: "mainWindow") {
            RootView()
                .frame(width: 800, height: 500)
                .onAppear {
                    // 获取主屏幕的尺寸
                    guard let screenFrame = NSScreen.main?.frame else {
                        return
                    }
                    
                    // 获取当前应用的窗口（假设只有一个窗口）
                    NSApplication.shared.windows.forEach { window in
                        // 计算窗口的中心位置
                        let windowWidth = window.frame.width
                        let windowHeight = window.frame.height
                        let centerX = (screenFrame.width - windowWidth) / 2
                        let centerY = (screenFrame.height - windowHeight) / 2
                        
                        // 设置窗口位置
                        window.setFrameOrigin(NSPoint(x: centerX, y: centerY))
                    }
                }
                //.toolbar(.hidden)
                .navigationTitle("")
                .environment(self.appContext)
                .environment(self.userContext)
        }
        .commands {
            CommandGroup(replacing: .appInfo) {
                Button {
                    openWindow(id: "abortPunchnet")
                } label: {
                    Text("About Punchnet")
                }
            }
        }
        .windowResizability(.contentSize)
        .windowToolbarStyle(.unified)
        
        Window("设置", id: "settings") {
            SettingsView()
                .environment(self.userContext)
        }
        .defaultSize(width: 800, height: 500)
        
        MenuBarExtra("punchnet", image: "logo_32") {
            VStack {
                Button(action: {
                    self.menuClick()
                }, label: {
                    Text("启动")
                })
                
                Divider()
                
                Button(action: {
                    NSApplication.shared.terminate(nil)
                }, label: { Text("退出") })
                
            }
        }
        .menuBarExtraStyle(.menu)
    }
    
    private func menuClick() {
        
    }
}


// 处理APP的生命周期
class AppDelegate: NSObject, NSApplicationDelegate {
    
    func applicationWillFinishLaunching(_ notification: Notification) {
        
    }
    
    func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
        Task {
            try await VPNManager.shared.disableVpn()
            DispatchQueue.main.async {
                sender.reply(toApplicationShouldTerminate: true)
            }
        }
        
        return .terminateLater
    }
    
}
