//
//  MenuView.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/16.
//

import SwiftUI
import PDFKit
import WindowsAzureMessaging

enum MenuPath: String, Hashable{
    case Manual
    case ManualECDIS
    case ManualECDISBasic
    case ManualECDISFunction
    case ManualRADAR
    case ManualRADARBasic
    case ManualRADARFunction
    case ManualAIS
    case ManualInmalsatC
    case ManualInternationalVHF
    case ManualVDR
//    case Information
    case Setting
    case GpsSelect
    case Help
    case AboutApp
    case ContactUs
    case SignOut

    var title: String{
        switch self {
        case .Manual:
            "Equipment Manual"
        case .ManualECDIS:
            "ECDIS"
        case .ManualECDISBasic:
            "BasicOperation"
        case .ManualECDISFunction:
            "Function"
        case .ManualRADAR:
            "RADAR"
        case .ManualRADARBasic:
            "BasicOperation"
        case .ManualRADARFunction:
            "Function"
        case .ManualAIS:
            "AIS"
        case .ManualInmalsatC:
            "Inmalsat C"
        case .ManualInternationalVHF:
            "International VHF"
        case .ManualVDR:
            "VDR"
//        case .Information:
//            "Information"
        case .Setting:
            "Setting"
        case .GpsSelect:
            "Fuel Switching GPS Select"
        case .Help:
            "Help"
        case .AboutApp:
            "About this App"
        case .ContactUs:
            "Contact Us"
        case .SignOut:
            "Sign Out"
        }
    }
    
    var fileIdntifer: String?{
        switch self {
        case .ManualECDISBasic:
            "JAN-7201_9201(E)7ZPNA4449M"
        case .ManualECDISFunction:
            "JAN-7201_9201(E)7ZPNA4450K"
        case .ManualVDR:
            "JCY-1900(E)7ZPNA4384H"
        case .ManualAIS:
            "JHS-183(E)7ZPJD0553E"
        case .ManualInternationalVHF:
            "JHS-800S(E)7ZPJD0714B"
        case .ManualRADARBasic:
            "JMR-7200_9200(E)7ZPNA4446K"
        case .ManualRADARFunction:
            "JMR-7200_9200(E)7ZPNA4447H"
        case .ManualInmalsatC:
            "JUE-87(E)7ZPSC0444A"
        default:
            nil
        }
    }
    
    var fileSavePath: URL?{
        guard let docPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last else { return nil }
        switch self {
        case .ManualECDISBasic, .ManualECDISFunction, .ManualVDR, .ManualAIS, .ManualInternationalVHF, .ManualRADARBasic, .ManualRADARFunction, .ManualInmalsatC:
            return docPath.appendingPathComponent("\(self.rawValue).pdf")
//        case .Help:
//            return Bundle.main.url(forResource: "Sail Assist_Help_20231219", withExtension: "pdf")
        default:
            return nil
        }
    }
}

struct MenuView: View {
    @EnvironmentObject private var sceneDelegate: SceneDelegate
    @EnvironmentObject private var loginViewModel : LoginViewModel
    @Environment(\.openURL) var openURL
    @State var path: [MenuPath] = []
    @Binding var isSignout: Bool

    var body: some View {
        NavigationStack(path: $path){
            VStack(spacing: 0) {
                MenuTitleView(path: $path)
                
                HStack {
                    VStack(alignment: .leading, spacing: 10){
                        Text(SharingData.my.shipName)
                            .font(FontStyle.TitleL.font)
                            .foregroundColor(ColorSet.Body.color)
                        Text(Preferences.UserName)
                            .font(FontStyle.EmphasisText.font)
                            .foregroundColor(ColorSet.BodyDescriptiion.color)
                    }
                    .padding(.horizontal, 30)
                    .padding(.vertical, 17)
                    Spacer()
                }
                
                Divider()
                    .background(ColorSet.LineColor04.color)
                Button{
                    path.append(.Manual)
                }label: {
                    MenuContentView(content: .Manual)
                }
                
//                Button{
//                    path.append(.Information)
//                }label: {
//                    MenuContentView(content: .Information)
//                }
                
                Button{
                    path.append(.Setting)
                }label: {
                    MenuContentView(content: .Setting)
                }
                
                Button{
                    isSignout = true
                }label: {
                    MenuContentView(content: .SignOut)
                }
                
                Spacer()
            }
            .background(ColorSet.BackgroundPrimary.color)
            .navigationTitle("")
            .navigationBarHidden(true)
            .navigationDestination(for: MenuPath.self) { goto in
                if goto == .Manual{
                    MenuManualView(path: $path)
                        .navigationBarHidden(true)
                }else if goto == .Setting{
                    MenuSettingView(path: $path)
                        .navigationBarHidden(true)
                }else if goto == .GpsSelect{
                    MenuGpsSelectView(path: $path)
                        .navigationBarHidden(true)
                }else if goto == .ManualECDIS{
                    MenuManualECDISView(path: $path)
                        .navigationBarHidden(true)
                }else if goto == .ManualRADAR{
                    MenuManualRADARView(path: $path)
                        .navigationBarHidden(true)
                }else if goto == .AboutApp{
                    MenuAboutAppView(path: $path)
                        .navigationBarHidden(true)
                }else if goto == .Help{
                    if let htmlPath = Bundle.main.path(forResource: "Manual/help", ofType: "html") {
                        WebView(isLoading: .constant(false), url: htmlPath)
                    }
//                }else if goto == .Information{
//                    MenuInfomarionView(path: $path)
//                        .navigationBarHidden(true)
                }else{
                    VStack(spacing: 0) {
                        MenuTitleView(path: $path, title: goto.title)
                        
                        if let url = goto.fileSavePath{
                            PDFViewer(url: url)
                        }else{
                            Spacer()
                        }
                    }
                    .navigationBarHidden(true)
                    .background(ColorSet.BackgroundPrimary.color)
                }
            }
            
            .alert("Sign Out", isPresented: $isSignout) {
                Button("Yes") {
                    MSNotificationHub.clearTags()
                    Preferences.Id = ""
                    Preferences.Password = ""
                    Preferences.UserName = ""
                    loginViewModel.isLogin = false
                    sceneDelegate.tabWindow?.isHidden = true
                }
                Button("No") {}
            } message: { Text("Are you sure?") }
        }
    }
}

#Preview {
    MenuView(isSignout: .constant(false))
        .environmentObject(LoginViewModel())
}
