//
//  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.areaId < $1.areaId }, id: \.name){ eca in
            HStack {
                Text(eca.name)
                    .font(FontStyle.DefaultText.font)
                    .foregroundColor(ColorSet.Body.color)
                    .onTapGesture {
                        ecaData.focusEca = eca.areaId
                    }
                Spacer()
                if !eca.isEnable{
                    Button(action: {
                        var newData = eca
                        newData.isEnable.toggle()
                        ecaData.editEcaArea(key: eca.areaId, value: newData, type: EcaOperation.Insert)
                        taskViewModel.viewMode = .FuelSwitching
                        taskViewModel.edittingEcaArea = eca
                        taskViewModel.viewMode = .EcaSetting
                    }, label: {
                        Image("icon_plus")
                            .resizable()
                            .frame(width: 12, height: 12)
                            .padding(8)
                    })
                    .frame(width: 28, height: 28)
                    .background(.primaryActiveIcon)
                    .cornerRadius(50)
                    .padding()
                }else{
                    Text("Registered")
                        .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())
}

