//
//  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:
                MenuMain(taskViewModel: taskViewModel)
            case .FuelSwitching:
                FuelSwitchingMain(taskViewModel: taskViewModel)
            case .EcaList:
                EcaListMain(taskViewModel: taskViewModel)
            case .EcaSetting:
                EcaSettingMain(taskViewModel: taskViewModel)
            case .NgaNotification:
                NgaNotificationMain(taskViewModel: taskViewModel)
            case .NgaSetting:
                NgaSettingMain(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("")
                    }
                }
            }
        }
    }
}

/**
 * Menu画面
 */
struct MenuMain: View {
    @ObservedObject var taskViewModel = TaskViewModel()

    var body: some View {
        //タイトルエリア
        HStack{
            Spacer()

            Text(TaskViewMode.MenuList.title)
                .font(FontStyle.TitleL.font)
                .frame(height: 20)
                .padding(.vertical, 14)

            Spacer()
        }
        .padding(EdgeInsets(top: 10, leading: 8, bottom: 13, trailing: 17))

        Divider()
            .background(ColorSet.LineColor03.color)

        ScrollViewReader { proxy in
            ScrollView(.vertical){
                Button(action: {
                    taskViewModel.viewMode = .FuelSwitching
                }, label: {
                    Text(TaskViewMode.FuelSwitching.title)
                        .font(FontStyle.DefaultText.font)
                        .padding()
                })
                .frame(width: 153, height: 42)
                .foregroundColor(ColorSet.ButtonText.color)
                .background(!SharingData.my.isFuelSwitchTask ? ColorSet.SecondaryDisable.color : ColorSet.PrimaryActiveIcon.color)
                .cornerRadius(30)
                .disabled(!SharingData.my.isFuelSwitchTask)

                Button(action: {
                    taskViewModel.viewMode = .NgaNotification
                }, label: {
                    Text(TaskViewMode.NgaNotification.title)
                        .font(FontStyle.DefaultText.font)
                        .padding()
                })
                .frame(width: 153, height: 42)
                .foregroundColor(ColorSet.ButtonText.color)
                .background(!SharingData.my.isNga ? ColorSet.SecondaryDisable.color : ColorSet.PrimaryActiveIcon.color)
                .cornerRadius(30)
                .disabled(!SharingData.my.isNga)
            }
            .onChange(of: taskViewModel.viewMode){ newViewMode in
                proxy.scrollTo(0, anchor: .top)
            }
        }

        Spacer()
            .frame(height: 55)
    }
}

/**
 * Fuel Switching画面
 */
struct FuelSwitchingMain: View {
    @ObservedObject var taskViewModel = TaskViewModel()

    var body: some View {
        //タイトルエリア
        HStack{
            Button(action: {
                taskViewModel.viewMode = .MenuList
            }, label: {
                Image("ink_02")
            })
            .frame(width: 48, height: 48)

            Spacer()

            Text(TaskViewMode.FuelSwitching.title)
                .font(FontStyle.TitleL.font)
                .frame(height: 20)
                .padding(.vertical, 14)

            Spacer()

            Button(action: {
            }, label: {
            })
            .frame(width: 48, height: 48)
        }
        .padding(EdgeInsets(top: 10, leading: 8, bottom: 13, trailing: 17))

        Divider()
            .background(ColorSet.LineColor03.color)

        //ECAリスト
        ScrollViewReader { proxy in
            ScrollView(.vertical){
                FuelSwitchingView(taskViewModel: taskViewModel)
            }
            .onChange(of: taskViewModel.viewMode){ newViewMode in
                proxy.scrollTo(0, anchor: .top)
            }
        }

        Spacer()
            .frame(height: 55)
    }
}

/**
 * ECA List画面
 */
struct EcaListMain: View {
    @ObservedObject var taskViewModel = TaskViewModel()

    var body: some View {
        //タイトルエリア
        HStack{
            Button(action: {
                taskViewModel.viewMode = .FuelSwitching
            }, label: {
                Image("ink_02")
            })
            .frame(width: 48, height: 48)

            Spacer()

            Text(TaskViewMode.EcaList.title)
                .font(FontStyle.TitleL.font)
                .frame(height: 20)
                .padding(.vertical, 14)

            Spacer()

            Button(action: {
            }, label: {
            })
            .frame(width: 48, height: 48)
        }
        .padding(EdgeInsets(top: 10, leading: 8, bottom: 13, trailing: 17))

        Divider()
            .background(ColorSet.LineColor03.color)

        ScrollViewReader { proxy in
            ScrollView(.vertical){
                EcaListView(taskViewModel: taskViewModel)
            }
            .onChange(of: taskViewModel.viewMode){ newViewMode in
                proxy.scrollTo(0, anchor: .top)
            }
        }

        Spacer()
            .frame(height: 55)
    }
}

/**
 * ECA  Setting画面
 */
struct EcaSettingMain: View {
    @ObservedObject var taskViewModel = TaskViewModel()

    var body: some View {
        //タイトルエリア
        HStack{
            Button(action: {
                taskViewModel.viewMode = .FuelSwitching
            }, label: {
                Image("ink_02")
            })
            .frame(width: 48, height: 48)

            Spacer()

            Text(TaskViewMode.EcaSetting.title)
                .font(FontStyle.TitleL.font)
                .frame(height: 20)
                .padding(.vertical, 14)

            Spacer()

            Button(action: {
            }, label: {
            })
            .frame(width: 48, height: 48)
        }
        .padding(EdgeInsets(top: 10, leading: 8, bottom: 13, trailing: 17))

        Divider()
            .background(ColorSet.LineColor03.color)

        ScrollViewReader { proxy in
            ScrollView(.vertical){
                if let edittingEcaArea = taskViewModel.edittingEcaArea {
                    EcaSettingView(taskViewModel: taskViewModel, edittingEca: edittingEcaArea)
                }
            }
            .onChange(of: taskViewModel.viewMode){ newViewMode in
                proxy.scrollTo(0, anchor: .top)
            }
        }

        Spacer()
            .frame(height: 55)
    }
}

/**
 * NGA Notification画面
 */
struct NgaNotificationMain: View {
    @ObservedObject var taskViewModel = TaskViewModel()

    var body: some View {
        //タイトルエリア
        HStack{
            Button(action: {
                taskViewModel.viewMode = .MenuList
            }, label: {
                Image("ink_02")
            })
            .frame(width: 48, height: 48)

            Spacer()

            Text(TaskViewMode.NgaNotification.title)
                .font(FontStyle.TitleL.font)
                .frame(height: 20)
                .padding(.vertical, 14)

            Spacer()

            Button(action: {
            }, label: {
            })
            .frame(width: 48, height: 48)
        }
        .padding(EdgeInsets(top: 10, leading: 8, bottom: 13, trailing: 17))

        Divider()
            .background(ColorSet.LineColor03.color)

        ScrollViewReader { proxy in
            ScrollView(.vertical){
                NgaNotificationView(taskViewModel: taskViewModel)
            }
            .onChange(of: taskViewModel.viewMode){ newViewMode in
                proxy.scrollTo(0, anchor: .top)
            }
        }

        Spacer()
            .frame(height: 55)
    }
}

/**
 * NGA設定画面
 */
struct NgaSettingMain: View {
    @ObservedObject var taskViewModel = TaskViewModel()
    @ObservedObject var nga = SharingData.nga
    @ObservedObject var map = SharingData.map

    var body: some View {
        //タイトルエリア
        HStack{
            Button(action: {
                if taskViewModel.viewMode == .NgaSetting && nga.editType == EditNgaType.movePoint {
                    nga.editNga = nga.moveNga
                    nga.editType = EditNgaType.addPoint
                    map.isMapFree = true
                } else {
                    taskViewModel.viewMode = .NgaNotification
                    nga.editType = EditNgaType.registered
                }
            }, label: {
                Image("ink_02")
            })
            .frame(width: 48, height: 48)

            Spacer()

            Text(TaskViewMode.NgaSetting.title)
                .font(FontStyle.TitleL.font)
                .frame(height: 20)
                .padding(.vertical, 14)

            Spacer()

            Button(action: {
            }, label: {
            })
            .frame(width: 48, height: 48)
        }
        .padding(EdgeInsets(top: 10, leading: 8, bottom: 13, trailing: 17))

        Divider()
            .background(ColorSet.LineColor03.color)

        ScrollViewReader { proxy in
            ScrollView(.vertical){
                 NgaSettingView(taskViewModel: taskViewModel)
            }
            .onChange(of: taskViewModel.viewMode){ newViewMode in
                proxy.scrollTo(0, anchor: .top)
            }
        }

        Spacer()
            .frame(height: 55)
    }
}

#Preview {
    MenuTaskView()
}

#Preview {
    MenuMain(taskViewModel: TaskViewModel())
}

#Preview {
    FuelSwitchingMain(taskViewModel: TaskViewModel())
}

#Preview {
    EcaListMain(taskViewModel: TaskViewModel())
}

#Preview {
    EcaSettingMain(taskViewModel: TaskViewModel())
}

#Preview {
    NgaNotificationMain(taskViewModel: TaskViewModel())
}

#Preview {
    NgaSettingMain(taskViewModel: TaskViewModel())
}
