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

import SwiftUI

struct EcaListView: View {
    @ObservedObject var taskViewModel : TaskViewModel
    @ObservedObject var ecaData = SharingData.eca
    
    var body: some View {
        ForEach(ecaData.ecaArea.map{ $0.1 }.sorted{ $0.id < $1.id }, id: \.name){ eca in
            HStack {
                Text(eca.name)
                    .font(FontStyle.DefaultText.font)
                    .foregroundColor(ColorSet.Body.color)
                    .onTapGesture {
                        ecaData.focusEca = eca.name
                    }
                Spacer()
                if !eca.isEnable{
                    Button(action: {
                        var newData = eca
                        newData.isEnable.toggle()
                        ecaData.editEcaArea(key: eca.name, value: newData, type: EcaOperation.Insert)
                        taskViewModel.viewMode = .SwitchingMenu
                        taskViewModel.edittingEcaArea = eca
                        taskViewModel.isShowSettingView = true
                    }, label: {
                        Image("icon_plus")
                            .resizable()
                            .frame(width: 12, height: 12)
                            .padding(8)
                    })
                    .frame(width: 28, height: 28)
                    .background(.primaryActiveIcon)
                    .cornerRadius(50)
                    .padding()
                }else{
                    Text("Registerd")
                        .font(FontStyle.SupplementText2.font)
                        .frame(height: 25)
                        .foregroundColor(ColorSet.ButtonDisable.color)
                        .padding(.vertical, 4)
                        .padding(.horizontal, 10)
                        .background(ColorSet.PrimaryDisable.color)
                        .cornerRadius(99)
                }
                
            }
            .frame(height: 62)
            .padding(.horizontal, 30)
            
            Divider()
                .background(.white)
        }
    }
}

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

