//
//  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 {
    @State var viewMode: TaskViewMode = .SwitchingMenu
    @State var edittingEcaArea: RegisteredEca? = nil
    @State var isShowSettingView : Bool = false
    @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: {
                    viewMode = .SwitchingMenu
                    isShowSettingView = false

                    if var eca = SharingData.eca.ecaArea.map{ $0.1 }.filter{ $0.isRunning }.first{
                        eca.status = .noticePass
                        //                        SharingData.eca.editEcaArea(key: eca.name, value: eca)
                        SharingData.eca.isShowEcaAlert = true

                    }
                }, label: {
                    if viewMode != .SwitchingMenu || isShowSettingView{
                        Image("ink_02")
                    }
                })
                .frame(width: 48, height: 48)

                Spacer()

                Text(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 viewMode {
                case .SwitchingMenu:
                    if let edittingEcaArea = edittingEcaArea, isShowSettingView{
                        EcaSettingView(isShowSettingEca: $isShowSettingView, edittingEca: edittingEcaArea)
                    }else{
                        TaskSwitchingMenuView(viewMode: $viewMode, edittingEcaArea: $edittingEcaArea, isShowSettingEca: $isShowSettingView)
                    }
                case .EcaList:
                    EcaListView()
                }
            }

            Spacer()
                .frame(height: 55)

        }
        .onAppear{
            EcaCoordinatesTable().setEcaData()
        }
        .alert("", isPresented: $eca.isShowEcaAlert) {
            if let ecaArea = eca.ecaArea.map{ $0.1 }.filter{ $0.isRunning }.first{
                if SharingData.my.ecaStatus == .finishPass{
                    Button("Yes"){
                        var newData = ecaArea
                        newData.isRunning = false
                        newData.status = EcaState.end
                        SharingData.eca.editEcaArea(key: ecaArea.name, value: newData, type: EcaOperation.Finish)
                    }
                    Button("No"){
                        var newData = ecaArea
                        newData.isRunning = false
                        newData.status = EcaState.incomplete
                        SharingData.eca.editEcaArea(key: ecaArea.name, value: newData, type: EcaOperation.Incomplete)
                    }
                }else{
                    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?")
                    case .incomplete:
                        Text("Have you finished \(ecaArea.name) fuel switching?\nIf you finish, you disable it.")
                    default:
                        Text("")
                    }
                }
            }
        }
    }
}

#Preview {
    MapTaskView()
}
