//
//  MapTaskView.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/17.
//

import SwiftUI

enum TaskViewMode{
    case SwitchingMenu
    case EcaList
    
    var title: String{
        switch self{
        case .SwitchingMenu:
            "Fuel Switching"
        case .EcaList:
            "ECA List"
        }
    }
}


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

    var body: some View {
        VStack{
            Capsule()
                .foregroundColor(ColorSet.ScrollBarColor.color)
                .frame(width: 45, height: 3)
                .padding(.top, 10)

            //タイトルエリア
            HStack{
                Button(action: {
                    taskViewModel.viewMode = .SwitchingMenu
                    taskViewModel.isShowSettingView = false
                }, label: {
                    if taskViewModel.viewMode != .SwitchingMenu || taskViewModel.isShowSettingView{
                        Image("ink_02")
                    }
                })
                .frame(width: 48, height: 48)

                Spacer()

                Text(taskViewModel.isShowSettingView ? taskViewModel.edittingEcaArea?.name ?? "" : taskViewModel.viewMode.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)

            ScrollView(.vertical){
                switch taskViewModel.viewMode {
                case .SwitchingMenu:
                    if let edittingEcaArea = taskViewModel.edittingEcaArea, taskViewModel.isShowSettingView{
                        EcaSettingView(isShowSettingEca: $taskViewModel.isShowSettingView, edittingEca: edittingEcaArea)
                    }else{
                        TaskSwitchingMenuView(taskViewModel: taskViewModel)
                    }
                case .EcaList:
                    EcaListView(taskViewModel: taskViewModel)
                }
            }

            Spacer()
                .frame(height: 55)
        }
        .onAppear{
            let appVersionModel = AppVersionModel()
            appVersionModel.start()
            EcaCoordinatesTable().setEcaData()
        }
        .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)
                }
            }
        } 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 {
    MapTaskView()
}
