//
//  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: {
                            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)
                            }
                            
                            
                        }
                        
                        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")
                                .frame(width: 22, height: 22)
                                .foregroundColor(ColorSet.Slidebar.color)
                        }
                        .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: 60)
                    .padding(.horizontal, 30)
                    
                    Divider()
                        .background(.white)
                }
            }
            //AddECAボタン
            Button(action: {
                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(viewMode: .constant(.SwitchingMenu), edittingEcaArea: .constant(nil), isShowSettingEca: .constant(false))
}
