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

import SwiftUI
import PDFKit

struct MenuManualView: View {
    @State var isAlert: Bool = false
    @State var isLoadingView: Bool = false
    @Binding var path : [MenuPath]

    var body: some View {
        VStack{
            MenuTitleView(path: $path, title: MenuPath.Manual.title)

            if !isLoadingView {
                Button{
                    path.append(.ManualECDIS)
                }label: {
                    MenuContentView(content: .ManualECDIS)
                }

                Button{
                    path.append(.ManualRADAR)
                }label: {
                    MenuContentView(content: .ManualRADAR)
                }

                if let manual = SharingData.my.manualUrls.filter({$0.identifer == MenuPath.ManualAIS.fileIdntifer}).first{
                    ManualDownloadButton(manualType: .ManualAIS, downloadUrl: manual.path,version: Preferences.AISManualVer, newVersion: manual.name, isAlert: $isAlert, isLoadingView: $isLoadingView){
                        path.append(.ManualAIS)
                        Preferences.AISManualVer = manual.name
                    }
                }

                if let manual = SharingData.my.manualUrls.filter({$0.identifer == MenuPath.ManualInmalsatC.fileIdntifer}).first{
                    ManualDownloadButton(manualType: .ManualInmalsatC, downloadUrl: manual.path,version: Preferences.InmalsatCManualVer, newVersion: manual.name, isAlert: $isAlert, isLoadingView: $isLoadingView){
                        path.append(.ManualInmalsatC)
                        Preferences.InmalsatCManualVer = manual.name
                    }
                }

                if let manual = SharingData.my.manualUrls.filter({$0.identifer == MenuPath.ManualInternationalVHF.fileIdntifer}).first{
                    ManualDownloadButton(manualType: .ManualInternationalVHF, downloadUrl: manual.path,version: Preferences.InternationalVHFManualVer, newVersion: manual.name, isAlert: $isAlert, isLoadingView: $isLoadingView){
                        path.append(.ManualInternationalVHF)
                        Preferences.InternationalVHFManualVer = manual.name
                    }
                }

                if let manual = SharingData.my.manualUrls.filter({$0.identifer == MenuPath.ManualVDR.fileIdntifer}).first{
                    ManualDownloadButton(manualType: .ManualVDR, downloadUrl: manual.path,version: Preferences.VDRManualVer, newVersion: manual.name, isAlert: $isAlert, isLoadingView: $isLoadingView){
                        path.append(.ManualVDR)
                        Preferences.VDRManualVer = manual.name
                    }
                }
            } else {
                Text("Now Loading...").padding()
                ProgressView()
            }

            Spacer()
        }
        .background(ColorSet.BackgroundPrimary.color)
        .alert("Error", isPresented: $isAlert) {
            Button("OK") {}
        } message: {
            Text("Network Error")
        }
    }
}

struct ManualDownloadButton: View {
    var manualType: MenuPath
    var downloadUrl: String
    var version: String?
    var newVersion: String
            
    @Binding var isAlert: Bool
    @Binding var isLoadingView: Bool

    var complition: () -> Void
    
    var body: some View {
        Button{
            if let version = version, version == newVersion{
                complition()
            }else{
                Task {
                    isLoadingView = true
                    if await PDFDownloadManager.main.downloadFile(urlString: downloadUrl, manualType.fileSavePath!){
                        complition()
                    }else{
                        isAlert = true
                    }
                    isLoadingView = false
                }
            }
        }label: {
            MenuContentView(content: manualType)
        }
    }
}

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