//
//  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
                HStack {
                    Circle()
                        .frame(width: 10)
                        .foregroundColor(eca.isRunning ? .red : .blue)
                    
                    Text(eca.name)
                    
                    Spacer()
                    
                    Button(action: {
                        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: {
                        Text("Button")
                    })
                }
            }
            
//            TaskEcaView()
            //AddECAボタン
            Button(action: {
                viewMode = .EcaList
            }, label: {
                Text("Add ECA Task")
                    .padding()
            })
            .background(ColorSet.PrimaryActiveIcon.color)
            .cornerRadius(30)
            
            Spacer()
        }
        .padding()
    }
}

struct TaskEcaView: View {
    @Binding var data : RegisteredEca
    @State var isPopover: Bool = false
    var body: some View {
        HStack {
            Circle()
                .frame(width: 10)
            
            Text(data.name)
            
            Spacer()
            
            Button(action: {
                
//                isPopover = true
            }, label: {
                /*@START_MENU_TOKEN@*/Text("Button")/*@END_MENU_TOKEN@*/
            })
            .popover(isPresented: $isPopover, content: {
                Text("aaaa")
            })
        }
        .padding()
    }
}


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