//
//  FuelSwitchingView.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/18.
//
import SwiftUI

struct FuelSwitchingView: View {
    @ObservedObject var taskViewModel: TaskViewModel
    @ObservedObject var ecaData = SharingData.eca
    @State var isRunningStopAlert: Bool = false
    @State var isRunningButton: Bool = false

    var body: some View {
        VStack {
            ForEach(ecaData.ecaArea.map{ $0.1 }.filter{ $0.isEnable }.sorted{ $0.areaId < $1.areaId }, id: \.name){ eca in
                VStack {
                    HStack {
                        //ECA開始・終了ボタン
                        Button {
                            for runningEca in ecaData.ecaArea.map({ $0.1 }).filter({ $0.isRunning }){
                                taskViewModel.edittingEcaArea = runningEca
                                taskViewModel.ecaName = runningEca.name
                                isRunningStopAlert = true
                            }
                            if isRunningStopAlert == false {
                                var newData = eca
                                newData.isRunning = true
                                newData.status = EcaState.running
                                SharingData.eca.editEcaArea(key: eca.areaId, value: newData, type: EcaOperation.Running)

                                isRunningButton = true
                                Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { timer in
                                    isRunningButton = false
                                }
                            }
                        } label: {
                            HStack {
                                Circle()
                                    .frame(width: 14, height: 14)
                                    .foregroundColor(eca.isRunning == true ? ColorSet.PrimaryActiveIcon.color : ColorSet.TaskStateIcon.color)
                                Spacer()
                                    .frame(width: 10)
                            }
                        }.disabled(isRunningButton)

                        Text(eca.name)
                            .font(FontStyle.DefaultText.font)
                            .foregroundColor(ColorSet.Body.color)
                            .onTapGesture {
                                ecaData.focusEca = eca.areaId
                            }

                        Spacer()

                        EcaMenuView(taskViewModel: taskViewModel, ecaData: ecaData, eca: eca)
                            .alert("Fuel Switching", isPresented: $isRunningStopAlert) {
                                Button("Yes") {
                                    if let ecaArea = taskViewModel.edittingEcaArea {
                                        var newData = ecaArea
                                        newData.isRunning = false
                                        newData.status = EcaState.end
                                        ecaData.editEcaArea(key: ecaArea.areaId, value: newData, type: EcaOperation.End)
                                    }
                                    taskViewModel.edittingEcaArea = nil
                                }
                                Button("No"){}
                            } message: {
                                Text("Have you finished " + taskViewModel.ecaName + " fuel switching?")
                            }
                    }
                    .frame(height: 60)
                    .padding(.horizontal, 30)
                    
                    Divider()
                        .background(.white)
                }
            }
            //AddECAボタン
            Button(action: {
                taskViewModel.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()
        }
    }
}

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