//
//  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()
        }
    }
}

struct EcaMenuView: View {
    @ObservedObject var taskViewModel: TaskViewModel
    @ObservedObject var ecaData = SharingData.eca
    @Environment(\ .colorScheme) var colorScheme
    @State var isDeleteAlert: Bool = false
    let deleteEcaArea = DeleteEcaArea()
    var eca: RegisteredEca

    var body: some View {
        Menu {
            Text(eca.name)

            Button{
                taskViewModel.edittingEcaArea = eca
                taskViewModel.ecaName = eca.name
                taskViewModel.viewMode = .EcaSetting
            } label: {
                Text("Edit Notice Setting")
            }

            Button{
                taskViewModel.edittingEcaArea = eca
                taskViewModel.ecaName = eca.name
                isDeleteAlert = true
            } label: {
                Text("Delete ECA Task")
            }
        } label: {
            Image(systemName: "ellipsis")
                .frame(width: 22, height: 22)
                .opacity(eca.isRunning == true ? 0.2 : 1.0)
                .foregroundColor(colorScheme == .light ? .black : .white)
        }
        .disabled(eca.isRunning)
        .alert("Delete", isPresented: $isDeleteAlert) {
            Button("Yes") {
                if let ecaArea = taskViewModel.edittingEcaArea {
                    var newData = ecaArea
                    newData.isEnable = false
                    newData.status = EcaState.cancel

                    ecaData.editEcaArea(key: ecaArea.areaId, value: newData, type: EcaOperation.Delete)
                    deleteEcaArea.start(ecaId: ecaArea.areaId)
                }
                taskViewModel.edittingEcaArea = nil
            }
            Button("No"){}
        } message: {
            Text("Have you finished " + taskViewModel.ecaName + " fuel switching?")
        }
    }
}

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