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

import SwiftUI

struct TaskSwitchingMenuView: View {
    @Binding var viewMode: TaskViewMode
    @ObservedObject var sharingData = SharingData.shared
    var body: some View {
        VStack{
            ForEach(sharingData.ecaArea.map{ $0.1 }.filter{ $0.isEnable }, id: \.name){ eca in
                
                VStack {
                    HStack {
                        Button {
                            for running in sharingData.ecaArea.map{ $0.1 }.filter{ $0.isRunning }{
                                running.isRunning = false
                                sharingData.editEcaArea(key: running.name, value: running)
                            }
                            eca.isRunning = true
                            sharingData.editEcaArea(key: eca.name, value: eca)
                        } label: {
                            Circle()
                                .frame(width: 10)
                                .foregroundColor(eca.isRunning ? .red : .blue)
                            
                            Text(eca.name)
                                .foregroundColor(ColorSet.Body.color)
                        }
                        
                        Spacer()
                        
                        
                        
                        Menu {
                            Label{
                                Text(eca.name)
                            } icon: {
                                Image("closeIcon")
                            }
                            
                            Button{
                                viewMode = .EcaSetting
                            } label: {
                                Text("Edit Notice Setting")
                            }
                            
                            Button{
                                
                            } label: {
                                Text("Delete ECA Task")
                            }
                        } label: {
                            Image(systemName: "ellipsis")
                        }
                        .disabled(eca.isRunning)
                    }
                    .frame(height: 50)
                    
                    Divider()
                }
            }
            
//            TaskEcaView()
            //AddECAボタン
            Button(action: {
                viewMode = .EcaList
            }, label: {
                Text("Add ECA Task")
                    .padding()
            })
            .background(ColorSet.PrimaryActiveIcon.color)
            .cornerRadius(30)
            
            Spacer()
        }
        .padding()
    }
}


#Preview {
    TaskSwitchingMenuView(viewMode: .constant(.SwitchingMenu))
}
