// 
// Decompiled by Procyon v0.6.0
// 

package org.jline.reader.impl.completer;

import org.jline.utils.AttributedStyle;
import org.jline.utils.AttributedStringBuilder;
import org.jline.terminal.Terminal;
import java.nio.file.Paths;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import org.jline.reader.Candidate;
import java.util.List;
import org.jline.reader.ParsedLine;
import org.jline.reader.LineReader;
import org.jline.reader.Completer;

@Deprecated
public class FileNameCompleter implements Completer
{
    @Override
    public void complete(final LineReader reader, final ParsedLine commandLine, final List<Candidate> candidates) {
        assert commandLine != null;
        assert candidates != null;
        final String buffer = commandLine.word().substring(0, commandLine.wordCursor());
        final String sep = this.getUserDir().getFileSystem().getSeparator();
        final int lastSep = buffer.lastIndexOf(sep);
        String curBuf;
        Path current;
        if (lastSep >= 0) {
            curBuf = buffer.substring(0, lastSep + 1);
            if (curBuf.startsWith("~")) {
                if (curBuf.startsWith("~" + sep)) {
                    current = this.getUserHome().resolve(curBuf.substring(2));
                }
                else {
                    current = this.getUserHome().getParent().resolve(curBuf.substring(1));
                }
            }
            else {
                current = this.getUserDir().resolve(curBuf);
            }
        }
        else {
            curBuf = "";
            current = this.getUserDir();
        }
        try (final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(current, this::accept)) {
            directoryStream.forEach(p -> {
                final String value = curBuf + p.getFileName().toString();
                if (Files.isDirectory(p, new LinkOption[0])) {
                    new Candidate(value + (reader.isSet(LineReader.Option.AUTO_PARAM_SLASH) ? sep : ""), this.getDisplay(reader.getTerminal(), p), null, null, reader.isSet(LineReader.Option.AUTO_REMOVE_SLASH) ? sep : null, null, false);
                    final Candidate candidate;
                    candidates.add(candidate);
                }
                else {
                    candidates.add(new Candidate(value, this.getDisplay(reader.getTerminal(), p), null, null, null, null, true));
                }
                return;
            });
        }
        catch (final IOException ex) {}
    }
    
    protected boolean accept(final Path path) {
        try {
            return !Files.isHidden(path);
        }
        catch (final IOException e) {
            return false;
        }
    }
    
    protected Path getUserDir() {
        return Paths.get(System.getProperty("user.dir"), new String[0]);
    }
    
    protected Path getUserHome() {
        return Paths.get(System.getProperty("user.home"), new String[0]);
    }
    
    protected String getDisplay(final Terminal terminal, final Path p) {
        String name = p.getFileName().toString();
        if (Files.isDirectory(p, new LinkOption[0])) {
            final AttributedStringBuilder sb = new AttributedStringBuilder();
            sb.styled(AttributedStyle.BOLD.foreground(1), name);
            sb.append("/");
            name = sb.toAnsi(terminal);
        }
        else if (Files.isSymbolicLink(p)) {
            final AttributedStringBuilder sb = new AttributedStringBuilder();
            sb.styled(AttributedStyle.BOLD.foreground(1), name);
            sb.append("@");
            name = sb.toAnsi(terminal);
        }
        return name;
    }
}
