package com.huangzj.protoplugin.json;

import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

/** 在目录上打开「Json 转 Java」对话框。 */
public final class JsonToJavaAction extends AnAction {

    @Override
    public void update(@NotNull AnActionEvent e) {
        Project project = e.getProject();
        VirtualFile vf = e.getData(CommonDataKeys.VIRTUAL_FILE);
        boolean dir = vf != null && vf.isDirectory();
        String place = e.getPlace();
        if (ActionPlaces.isPopupPlace(place)) {
            e.getPresentation().setEnabledAndVisible(dir);
        } else {
            e.getPresentation().setVisible(true);
            e.getPresentation().setEnabled(project != null);
        }
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Project project = e.getProject();
        if (project == null) {
            return;
        }
        VirtualFile vf = e.getData(CommonDataKeys.VIRTUAL_FILE);
        VirtualFile folder = vf != null && vf.isDirectory() ? vf : null;
        new JsonToJavaDialog(project, folder).showAndGet();
    }
}
