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

import SwiftUI
import PDFKit

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 TermsOfUse
    case PrivacyStatement
    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 .TermsOfUse:
            "Terms Of Use"
        case .PrivacyStatement:
            "Privacy Statement"
        case .ContactUs:
            "Contact Us"
        case .SignOut:
            "Sign Out"
        }
    }
}

struct MenuView: View {
    @EnvironmentObject private var sceneDelegate: SceneDelegate
    @State var path: [MenuPath] = []
    @Binding var isSignout: Bool
    @Binding var isLogin: 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{
                    VStack(spacing: 0) {
                        MenuTitleView(path: $path, title: goto.title)
                        
                        if let url = Preferences.ECDISBasicUrl{
                            PDFViewer(url: url)
                        }else{
                            Spacer()
                        }
                    }
                        .navigationBarHidden(true)
                        .background(ColorSet.BackgroundPrimary.color)
                }
            }

            .alert(isPresented: $isSignout) {
                return Alert(title: Text("Sign Out"),
                     message: Text("Are you sure?"),
                            primaryButton: .default(Text("No")), secondaryButton: .cancel(Text("Yes"), action: {
                    Preferences.Id = ""
                    Preferences.Password = ""
                    Preferences.UserName = ""
                    isLogin = false
                                   }))
            }
        }
    }
}

#Preview {
    MenuView(isSignout: .constant(false), isLogin: .constant(false))
}
