package com.jscape.inet.sftp.protocol.v4.messages;

import com.jscape.util.file.UnixFileType;

/**
 * @author Alex Usun
 */
public enum FileType {

    SSH_FILEXFER_TYPE_REGULAR(1),
    SSH_FILEXFER_TYPE_DIRECTORY(2),
    SSH_FILEXFER_TYPE_SYMLINK(3),
    SSH_FILEXFER_TYPE_SPECIAL(4),
    SSH_FILEXFER_TYPE_UNKNOWN(5);

    public final int code;

    public static FileType typeFor(int code) {
        for (FileType type : values()) {
            if (type.code == code) {
                return type;
            }
        }

        return SSH_FILEXFER_TYPE_UNKNOWN;
    }

    public static FileType typeFor(UnixFileType type) {
        switch (type) {
            case REGULAR:
                return SSH_FILEXFER_TYPE_REGULAR;
            case DIRECTORY:
                return SSH_FILEXFER_TYPE_DIRECTORY;
            case SYMBOLIC_LINK:
                return SSH_FILEXFER_TYPE_SYMLINK;
            default:
                return SSH_FILEXFER_TYPE_SPECIAL;
        }
    }

    public static UnixFileType unixFileTypeFor(int code) {
        FileType type = typeFor(code);

        switch (type) {
            case SSH_FILEXFER_TYPE_REGULAR:
                return UnixFileType.REGULAR;
            case SSH_FILEXFER_TYPE_DIRECTORY:
                return UnixFileType.DIRECTORY;
            case SSH_FILEXFER_TYPE_SYMLINK:
                return UnixFileType.SYMBOLIC_LINK;
            default:
                return UnixFileType.REGULAR;
        }
    }

    FileType(int code) {
        this.code = code;
    }
}
