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

import SwiftUI

struct TaskSwitchingMenuView: View {
    @Binding var viewMode: TaskViewMode
    @Binding var edittingEcaArea: RegisteredEca?
    @Binding var isShowSettingEca : Bool
    @ObservedObject var sharingData = SharingData.shared
    @State var isDeleteAlert : Bool = false
    
    var body: some View {
        VStack{
            ForEach(sharingData.ecaArea.map{ $0.1 }.filter{ $0.isEnable }, id: \.name){ eca in
                
                VStack {
                    HStack {
                        Button {
                            for runningEca in sharingData.ecaArea.map{ $0.1 }.filter{ $0.isRunning }{
                                var newData = runningEca
                                newData.isRunning = false
                                sharingData.editEcaArea(key: runningEca.name, value: newData)
                            }
                            var newData = eca
                            newData.isRunning = true
                            sharingData.editEcaArea(key: eca.name, value: newData)
                        } label: {
                            Circle()
                                .frame(width: 10)
                                .foregroundColor(eca.isRunning ? .red : .blue)
                            
                            Text(eca.name)
                                .foregroundColor(ColorSet.Body.color)
                        }
                        
                        Spacer()
                        
                        Menu {
                            Text(eca.name)

                            Button{
                                edittingEcaArea = eca
                                isShowSettingEca = true
                            } label: {
                                Text("Edit Notice Setting")
                            }
                            
                            Button{
                                edittingEcaArea = eca
                                isDeleteAlert = true
                            } label: {
                                Text("Delete ECA Task")
                            }
                        } label: {
                            Image(systemName: "ellipsis")
                                .padding()
                        }
                        .disabled(eca.isRunning)
                        .alert("delete", isPresented: $isDeleteAlert) {
                            Button(role: .destructive) {
                                if let ecaArea = edittingEcaArea{
                                    var newData = ecaArea
                                    newData.isEnable = false
                                    sharingData.editEcaArea(key: ecaArea.name, value: newData)
                                }
                                edittingEcaArea = nil
                            } label: {
                                Text("delete")
                            }

                            Button(role: .cancel) {
                                
                            } label: {
                                Text("cancel")
                            }

                        } message: {
                            Text("eca delete")
                        }
                    }
                    .frame(height: 50)
                    
                    Divider()
                }
            }
            //AddECAボタン
            Button(action: {
                viewMode = .EcaList
            }, label: {
                Text("Add ECA Task")
                    .padding()
            })
            .background(ColorSet.PrimaryActiveIcon.color)
            .cornerRadius(30)
            
            Spacer()
        }
        .padding()
    }
}


#Preview {
    TaskSwitchingMenuView(viewMode: .constant(.SwitchingMenu), edittingEcaArea: .constant(nil), isShowSettingEca: .constant(false))
}
