package co.com.bancolombia.devsecopsenginetools.actions;

import co.com.bancolombia.devsecopsenginetools.tasks.ScanImageTask;
import co.com.bancolombia.devsecopsenginetools.ui.tool.LogPanelLogger;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsActions;
import icons.DevSecOpsIcons;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

import static com.intellij.openapi.actionSystem.ActionUpdateThread.BGT;

public class ScanImageAction extends AnAction {
    private boolean isTaskRunning = false;

    public ScanImageAction() {
        super();
    }

    public ScanImageAction(@Nullable @NlsActions.ActionText String text,
                           @Nullable @NlsActions.ActionDescription String description, @Nullable Icon icon) {
        super(text, description, icon);
    }

    public static ScanImageAction forUI() {
        return new ScanImageAction("Scan Image", "Run image scan", DevSecOpsIcons.ScanImage);
    }

    @SneakyThrows
    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();
        LogPanelLogger.activate(project);
        if (project != null) {
            isTaskRunning = true;
            ScanImageTask task = new ScanImageTask(project, "Scanning image", () -> isTaskRunning = false);
            ProgressManager.getInstance().run(task);
        }
    }

    @Override
    public @NotNull ActionUpdateThread getActionUpdateThread() {
        return BGT;
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        Presentation presentation = e.getPresentation();
        presentation.setEnabled(!isTaskRunning);
    }
}
