//
//  MenuTaskView.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/17.
//
import SwiftUI

enum TaskViewMode {
    case MenuList
    case FuelSwitching
    case EcaList
    case EcaSetting
    case NgaNotification
    case NgaSetting

    var title: String {
        switch self {
        case .MenuList:
            "Map Functions"
        case .FuelSwitching:
            "Fuel Switching"
        case .EcaList:
            "ECA List"
        case .EcaSetting:
            "ECA Setting"
        case .NgaNotification:
            "NGA Notification"
        case .NgaSetting:
            "NGA Setting"
        }
    }
}

struct MenuTaskView: View {
    @ObservedObject var taskViewModel = TaskViewModel()
    @ObservedObject var eca = SharingData.eca
    @ObservedObject var nga = SharingData.nga

    var body: some View {
        VStack{
            if UIDevice.current.userInterfaceIdiom == .phone {
                Capsule()
                    .foregroundColor(ColorSet.ScrollBarColor.color)
                    .frame(width: 45, height: 3)
                    .padding(.top, 10)
            }

            switch taskViewModel.viewMode {
            case .MenuList:
                MenuMainView(taskViewModel: taskViewModel)
            case .FuelSwitching:
                FuelSwitchingMainView(taskViewModel: taskViewModel)
            case .EcaList:
                EcaListMainView(taskViewModel: taskViewModel)
            case .EcaSetting:
                EcaSettingMainView(taskViewModel: taskViewModel)
            case .NgaNotification:
                NgaNotificationMainView(taskViewModel: taskViewModel)
            case .NgaSetting:
                NgaSettingMainView(taskViewModel: taskViewModel)
            }
        }
        .onAppear{
            let appVersionModel = AppVersionModel()
            appVersionModel.start()

            if UIDevice.current.userInterfaceIdiom == .phone {
                nga.editType = EditNgaType.registered
            } else if UIDevice.current.userInterfaceIdiom == .pad {
                switch nga.editType {
                case EditNgaType.nonEdit:
                    print(debug: "Test: nonEdit")
                    nga.editType = EditNgaType.registered
                    break
                case EditNgaType.registered:
                    print(debug: "Test: registered")
                    taskViewModel.viewMode = .NgaNotification
                    break
                case EditNgaType.addPoint:
                    print(debug: "Test: addPoint")
                    taskViewModel.viewMode = .NgaSetting
                    break
                case EditNgaType.movePoint:
                    print(debug: "Test: movePoint")
                    taskViewModel.viewMode = .NgaSetting
                    break
                case EditNgaType.deletePoint:
                    print(debug: "Test: deletePoint")
                    taskViewModel.viewMode = .NgaSetting
                    break
                }
            }
        }
        .alert("", isPresented: $eca.isShowEcaAlert) {
//            if let ecaArea = eca.ecaArea.map({ $0.1 }).filter({ $0.isRunning }).first{
                Button("OK"){
//                    let ecaTask = EcaTask()
//                    ecaTask.chengeEcaStatus(eca: ecaArea)

//                    if let status = SharingData.my.ecaStatus {
//                        if status == .finishPass {
//                            var newData = ecaArea
//                            newData.isRunning = false
//                            newData.status = EcaState.incomplete
//                            SharingData.eca.editEcaArea(key: ecaArea.name, value: newData, type: EcaOperation.Incomplete)
//                        }
//                    }
                }
//            }
        } message: {
            if let ecaArea = eca.ecaArea.map({ $0.1 }).filter({ $0.isRunning }).first{
                if let status = SharingData.my.ecaStatus {
                    switch status {
                    case .noticePass:
                        Text("Arrived at advance notice point.")
                    case .startPass:
                        Text("Arrived at switching start point.")
                    case .finishPass:
                        Text("Have you finished \(ecaArea.name) fuel switching?\nIf you finish, you disable it.")
                    case .incomplete:
                        Text("Have you finished \(ecaArea.name) fuel switching?\nIf you finish, you disable it.")
                    default:
                        Text("")
                    }
                }
            }
        }
    }
}

#Preview {
    MenuTaskView()
}
