package io.xpipe.app.browser.menu.impl;

import io.xpipe.app.browser.file.BrowserEntry;
import io.xpipe.app.browser.file.BrowserFileSystemTabModel;
import io.xpipe.app.browser.menu.BrowserMenuCategory;
import io.xpipe.app.browser.menu.BrowserMenuLeafProvider;
import io.xpipe.app.core.AppI18n;
import io.xpipe.app.ext.FileKind;
import io.xpipe.app.platform.LabelGraphic;
import io.xpipe.app.util.OsType;

import javafx.beans.value.ObservableValue;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;

import java.util.List;

public class RenameMenuProvider implements BrowserMenuLeafProvider {

    @Override
    public void execute(BrowserFileSystemTabModel model, List<BrowserEntry> entries) {
        model.getFileList().getEditing().setValue(entries.getFirst());
    }

    @Override
    public boolean isApplicable(BrowserFileSystemTabModel model, List<BrowserEntry> entries) {
        return entries.size() == 1 && entries.getFirst().getRawFileEntry().getKind() != FileKind.LINK;
    }

    @Override
    public boolean automaticallyResolveLinks() {
        return false;
    }

    @Override
    public LabelGraphic getIcon() {
        return new LabelGraphic.IconGraphic("mdi2r-rename-box");
    }

    @Override
    public BrowserMenuCategory getCategory() {
        return BrowserMenuCategory.MUTATION;
    }

    @Override
    public KeyCombination getShortcut() {
        return switch (OsType.ofLocal()) {
            case OsType.Linux linux -> new KeyCodeCombination(KeyCode.F2);
            case OsType.MacOs macOs -> new KeyCodeCombination(KeyCode.ENTER);
            case OsType.Windows windows -> new KeyCodeCombination(KeyCode.F2);
        };
    }

    @Override
    public ObservableValue<String> getName(BrowserFileSystemTabModel model, List<BrowserEntry> entries) {
        return AppI18n.observable("rename");
    }

    @Override
    public String getId() {
        return "renameFile";
    }
}
