/*
 * Copyright 2013-2016 consulo.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package consulo.backgroundTaskByVfsChange.ui;

import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.ui.Gray;
import consulo.annotation.access.RequiredReadAction;
import consulo.backgroundTaskByVfsChange.BackgroundTaskByVfsChangeManager;
import consulo.backgroundTaskByVfsChange.BackgroundTaskByVfsChangeProvider;
import consulo.backgroundTaskByVfsChange.BackgroundTaskByVfsChangeProviders;
import consulo.backgroundTaskByVfsChange.BackgroundTaskByVfsChangeTask;
import consulo.editor.notifications.EditorNotificationProvider;
import jakarta.inject.Inject;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

/**
 * @author VISTALL
 * @since 26.10.2015
 */
public class BackgroundTaskEditorNotificationProvider implements EditorNotificationProvider<EditorNotificationPanel>, DumbAware {
  private final Project myProject;

  @Inject
  public BackgroundTaskEditorNotificationProvider(Project project) {
    myProject = project;
  }

  @RequiredReadAction
  @Nullable
  @Override
  public EditorNotificationPanel createNotificationPanel(@Nonnull final VirtualFile file, @Nonnull FileEditor fileEditor) {
    List<BackgroundTaskByVfsChangeProvider> providers = BackgroundTaskByVfsChangeProviders.getProviders(myProject, file);
    if (providers.isEmpty()) {
      return null;
    }

    EditorNotificationPanel panel = new EditorNotificationPanel(Gray._220);

    List<BackgroundTaskByVfsChangeTask> tasks = BackgroundTaskByVfsChangeManager.getInstance(myProject).findTasks(file);
    if (!tasks.isEmpty()) {
      panel.text("Task(s): " + StringUtil.join(tasks, it -> it.getName() + (!it.isEnabled() ? " (disabled)" : ""), ", "));

      panel.createActionLabel("Force Run", () -> {
        BackgroundTaskByVfsChangeManager.getInstance(myProject).runTasks(file);
      });
    }
    else {
      panel.text("Background task(s) on file change is available");
    }

    panel.createActionLabel("Manage", () -> {
      BackgroundTaskByVfsChangeManager.getInstance(myProject).openManageDialog(file);
    });
    return panel;
  }
}
