package com.jscape.inet.sftp;

import com.jscape.util.file.PosixFilePermissions;
import com.jscape.util.file.PosixFilePermissionsFormat;
import com.jscape.util.file.UnixFileType;

import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Usun
 */
public class SftpFile {

    private final String name;
    private final UnixFileType type;
    private final long size;
    private final String owner;
    private final String group;
    private final PosixFilePermissions permissions;
    private final long accessTime;
    private final long modificationTime;

    public SftpFile(String name, UnixFileType type, long size, String owner, String group, PosixFilePermissions permissions, long accessTime, long modificationTime) {
        assertNotNull(name);
        this.name = name;

        assertNotNull(type);
        this.type = type;

        this.size = size;
        this.owner = owner != null ? owner : "";
        this.group = group != null ? group : "";
        this.permissions = permissions;
        this.accessTime = accessTime;
        this.modificationTime = modificationTime;
    }

    /**
     * Returns the name of the file or directory denoted by
     * this abstract pathname.
     *
     * @return the name of the file or directory denoted
     *         by this abstract pathname
     */
    public String getFilename() {
        return this.name;
    }

    /**
     * @deprecated
     */
    public String getLongName() {
        return "";
    }

    /**
     * Tests whether the file denoted by this abstract pathname is a directory.
     *
     * @return <code>true</code> if and only if the file denoted by this
     *         abstract pathname exists and is a directory; <code>false</code> otherwise
     *
     * @throws SftpException if an I/O error occurred
     */
    public boolean isDirectory() {
        return this.type == UnixFileType.DIRECTORY;
    }

    /**
     * Returns the size of the file denoted by this abstract pathname.
     *
     * @return the size, in bytes, of the file denoted by this
     *         abstract pathname, or 0L if the file does not exist
     *
     * @throws SftpException if an I/O error occurred
     */
    public long getFilesize() {
        return this.size;
    }

    /**
     * Returns the owner name for this abstract pathname as string.
     *
     * @return the owner name for this abstract pathname as string
     *
     * @throws SftpException if an I/O error occurred
     */
    public String getOwner() {
        return this.owner;
    }

    /**
     * Returns the owner group name for this abstract pathname as string.
     *
     * @return the owner gropu name for this abstract pathname as string
     *
     * @throws SftpException if an I/O error occurred
     */
    public String getGroup() {
        return this.group;
    }

    /**
     * Returns POSIX string representation of the file permissions.
     * Legacy method from the FtpFile.
     *
     * @return POSIX string representation of the file permissions
     *
     * @throws SftpException if an I/O error occurred
     */
    public int getPermissions() {
        return this.permissions != null ? PosixFilePermissionsFormat.format(this.permissions) : 0;
    }

    /**
     * Returns POSIX string representation of the file permissions.
     * Legacy method from the FtpFile.
     *
     * @return POSIX string representation of the file permissions
     *
     * @throws SftpException if an I/O error occurred
     * @deprecated
     */
    public String getPermission() {
        return this.permissions != null ? this.type.lsCode + PosixFilePermissionsFormat.formatString(this.permissions) : "";
    }

    /**
     * Returns the file access time as milliseconds from Jan 1, 1970 in UTC.
     * A negative value indicates number  of milliseconds before Jan 1, 1970.
     *
     * @return the file access time in milliseconds
     *
     * @throws SftpException if an I/O error occurred
     */
    public long getAccessTime() {
        return this.accessTime;
    }

    /**
     * Returns the file modification time as milliseconds from Jan 1, 1970 in UTC.
     * A negative value indicates number  of milliseconds before Jan 1, 1970.
     *
     * @return the file modification time in milliseconds
     *
     * @throws SftpException if an I/O error occurred
     */
    public long getModificationTime() {
        return this.modificationTime;
    }

    @Override
    public String toString() {
        return this.name;
    }
}
