//
//  MenuManualView.swift
//  Sailassist
//
//  Created by Mamoru Sugita on 2023/11/10.
//

import SwiftUI
import PDFKit

struct MenuManualView: View {
    @State var isAlert: Bool = false
    @Binding var path : [MenuPath]
    var body: some View {
        VStack{
            MenuTitleView(path: $path, title: MenuPath.Manual.title)
            
            Button{
                path.append(.ManualECDIS)
            }label: {
                MenuContentView(content: .ManualECDIS)
            }
            
            Button{
                path.append(.ManualRADAR)
            }label: {
                MenuContentView(content: .ManualRADAR)
            }
            
            Button{
                if Preferences.AISUrl != nil{
                    path.append(.ManualAIS)
                }else{
                    if let url = PDFDownloadManager.main.downloadFile(urlString: "https://samsdbg.jmarinesafety.jp/gps_editor/gps_help.pdf", fileName: "ECDIS.pdf"){
                        Preferences.AISUrl = url
                        path.append(.ManualAIS)
                    }else{
                        isAlert = true
                    }
                }
            }label: {
                MenuContentView(content: .ManualAIS)
            }
            
            Button{
                if Preferences.InmalsatCUrl != nil{
                    path.append(.ManualInmalsatC)
                }else{
                    if let url = PDFDownloadManager.main.downloadFile(urlString: "https://samsdbg.jmarinesafety.jp/gps_editor/gps_help.pdf", fileName: "ECDIS.pdf"){
                        Preferences.InmalsatCUrl = url
                        path.append(.ManualInmalsatC)
                    }else{
                        isAlert = true
                    }
                }
            }label: {
                MenuContentView(content: .ManualInmalsatC)
            }

            Button{
                if Preferences.InternationalVHFUrl != nil{
                    path.append(.ManualInternationalVHF)
                }else{
                    if let url = PDFDownloadManager.main.downloadFile(urlString: "https://samsdbg.jmarinesafety.jp/gps_editor/gps_help.pdf", fileName: "ECDIS.pdf"){
                        Preferences.InternationalVHFUrl = url
                        path.append(.ManualInternationalVHF)
                    }else{
                        isAlert = true
                    }
                }
            }label: {
                MenuContentView(content: .ManualInternationalVHF)
            }

            Button{
                if Preferences.VDRURL != nil{
                    path.append(.ManualVDR)
                }else{
                    if let url = PDFDownloadManager.main.downloadFile(urlString: "https://samsdbg.jmarinesafety.jp/gps_editor/gps_help.pdf", fileName: "ECDIS.pdf"){
                        Preferences.VDRURL = url
                        path.append(.ManualVDR)
                    }else{
                        isAlert = true
                    }
                }
            }label: {
                MenuContentView(content: .ManualVDR)
            }

            Spacer()
        }
        .background(ColorSet.BackgroundPrimary.color)
        .alert("delete", isPresented: $isAlert) {
            Button(role: .destructive) {
               
            } label: {
                Text("delete")
            }

        } message: {
            Text("eca delete")
        }
    }
}

#Preview {
    MenuManualView(path: .constant([.Manual]))
}
