//
//  TaskSwitchingMenuView.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/18.
//

import SwiftUI

struct TaskSwitchingMenuView: View {
    @ObservedObject var taskViewModel: TaskViewModel
    @ObservedObject var ecaData = SharingData.eca
    @State var isDeleteAlert: Bool = false
    @State var isRunningStopAlert: Bool = false
    let deleteEcaArea = DeleteEcaArea()

    var body: some View {
        VStack{
            ForEach(ecaData.ecaArea.map{ $0.1 }.filter{ $0.isEnable }.sorted{ $0.id < $1.id }, id: \.name){ eca in
                VStack {
                    HStack {
                        //ECA開始・終了ボタン
                        Button {
                            for runningEca in ecaData.ecaArea.map({ $0.1 }).filter({ $0.isRunning }){
                                taskViewModel.edittingEcaArea = runningEca
                                taskViewModel.ecaName = runningEca.name
                                isRunningStopAlert = true
                            }
                            if isRunningStopAlert == false {
                                var newData = eca
                                newData.isRunning = true
                                newData.status = EcaState.running
                                SharingData.eca.editEcaArea(key: eca.name, value: newData, type: EcaOperation.Running)
                            }
                        } label: {
                            HStack {
                                Circle()
                                    .frame(width: 14, height: 14)
                                    .foregroundColor(eca.isRunning ? ColorSet.PrimaryActiveIcon.color : ColorSet.TaskStateIcon.color)
                                Spacer()
                                    .frame(width: 10)
                            }
                        }

                        Text(eca.name)
                            .font(FontStyle.DefaultText.font)
                            .foregroundColor(ColorSet.Body.color)
                            .onTapGesture {
                                ecaData.focusEca = eca.name
                            }

                        Spacer()

                        Menu {
                            Text(eca.name)

                            Button{
                                taskViewModel.edittingEcaArea = eca
                                taskViewModel.ecaName = eca.name
                                taskViewModel.isShowSettingView = true
                            } label: {
                                Text("Edit Notice Setting")
                            }

                            Button{
                                taskViewModel.edittingEcaArea = eca
                                taskViewModel.ecaName = eca.name
                                isDeleteAlert = true
                            } label: {
                                Text("Delete ECA Task")
                            }
                        } label: {
                            Image(systemName: "ellipsis")
                                .frame(width: 22, height: 22)
                                .foregroundColor(eca.isRunning ? ColorSet.Splash.color : ColorSet.Slidebar.color)
                        }
                        .disabled(eca.isRunning)
                        .alert("Delete", isPresented: $isDeleteAlert) {
                            Button("Yes") {
                                if let ecaArea = taskViewModel.edittingEcaArea {
                                    var newData = ecaArea
                                    newData.isEnable = false
                                    newData.status = EcaState.cancel

                                    ecaData.editEcaArea(key: ecaArea.name, value: newData, type: EcaOperation.Delete)
                                    deleteEcaArea.start(ecaId: ecaArea.id)
                                }
                                taskViewModel.edittingEcaArea = nil
                            }
                            Button("No"){}
                        } message: {
                            Text("Have you finished " + taskViewModel.ecaName + " fuel switching?")
                        }
                        .alert("Fuel Switching", isPresented:$isRunningStopAlert) {
                            Button("Yes") {
                                if let ecaArea = taskViewModel.edittingEcaArea {
                                    var newData = ecaArea
                                    newData.isRunning = false
                                    newData.status = EcaState.end
                                    ecaData.editEcaArea(key: ecaArea.name, value: newData, type: EcaOperation.End)
                                }
                                taskViewModel.edittingEcaArea = nil
                            }
                            Button("No"){}
//                            Button("Cancel"){¥
//                                if let ecaArea = taskViewModel.edittingEcaArea {
//                                    var newData = ecaArea
//                                    newData.isRunning = false
//                                    newData.status = EcaState.cancel
//                                    ecaData.editEcaArea(key: ecaArea.name, value: newData, type: EcaOperation.Cancel)
//                                }
//                                taskViewModel.edittingEcaArea = nil
//                            }
                        } message: {
                            Text("Have you finished " + taskViewModel.ecaName + " fuel switching?")
                        }
                    }
                    .frame(height: 60)
                    .padding(.horizontal, 30)
                    
                    Divider()
                        .background(.white)
                }
            }
            //AddECAボタン
            Button(action: {
                taskViewModel.viewMode = .EcaList
            }, label: {
                Text("Add ECA Task")
                    .font(FontStyle.DefaultText.font)
                    .padding()
            })
            .frame(width: 153, height: 42)
            .foregroundColor(ColorSet.ButtonText.color)
            .background(ColorSet.PrimaryActiveIcon.color)
            .cornerRadius(30)
            
            Spacer()
        }
//        .padding()
    }
}


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