package com.huangzj.protoplugin;

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 com.huangzj.protoplugin.mysql.MysqlEntityDialog;
import org.jetbrains.annotations.NotNull;

/** 在项目目录上打开「从 MySQL 生成 JPA 实体」对话框。 */
public class MysqlEntityFromFolderAction 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;
        MysqlEntityDialog dialog = new MysqlEntityDialog(project, folder);
        dialog.showAndGet();
    }
}
