package com.jscape.util.file;

import com.jscape.util.FileInfo;

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

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

    public final char fileType;
    public final PosixFilePermissions permissions;
    public final int hardLinksCount;
    public final String owner;
    public final String group;
    public final long size;
    public final long lastModifiedDate;
    public final String filename;

    public static LsFileInfo infoFor(FileInfo info) {
        return new LsFileInfo(info.isDirectory() ? UnixFileType.DIRECTORY : UnixFileType.REGULAR,
                              PosixFilePermissions.permissionsOf(info),
                              1,
                              info.getUser().isEmpty() ? "user" : info.getUser(),
                              info.getGroup().isEmpty() ? "group" : info.getGroup(),
                              info.getLength(),
                              info.getModificationTime(),
                              info.getName());
    }

    public LsFileInfo(char fileType,
                      PosixFilePermissions permissions,
                      int hardLinksCount,
                      String owner,
                      String group,
                      long size,
                      long lastModifiedDate,
                      String filename) {
        this.fileType = fileType;

        assertNotNull(permissions);
        this.permissions = permissions;

        this.hardLinksCount = hardLinksCount;

        assertNotNull(owner);
        this.owner = owner;

        assertNotNull(group);
        this.group = group;

        this.size = size;

        this.lastModifiedDate = lastModifiedDate;

        assertNotNull(filename);
        this.filename = filename;
    }

    public LsFileInfo(UnixFileType fileType,
                      PosixFilePermissions permissions,
                      int hardLinksCount,
                      String owner,
                      String group,
                      long size,
                      long lastModifiedDate,
                      String filename) {
        this(fileType.lsCode, permissions, hardLinksCount, owner, group, size, lastModifiedDate, filename);
    }

    public boolean isDirectory() {
        return UnixFileType.isDirectory(this.fileType);
    }

    @Override
    public String toString() {
        return "LsFileInfo {"
                + "fileType=" + this.fileType
                + ", permissions=" + this.permissions
                + ", hardLinksCount=" + this.hardLinksCount
                + ", owner='" + this.owner + '\''
                + ", group='" + this.group + '\''
                + ", size=" + this.size
                + ", lastModifiedDate=" + this.lastModifiedDate
                + ", filename='" + this.filename + '\''
                + '}';
    }
}
