package org.academiadecodigo.bootcamp11.exceptions.file;

import org.academiadecodigo.bootcamp11.exceptions.exceptions.*;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class FileManager {
    private File[] disk;
    private String loggedUser;

    public FileManager(int diskSpace) {
        disk = new File[Math.max(0, diskSpace)];
        loggedUser = null;
    }

    public void login(String user) {
        if (user.length() < 1) {
            System.out.println("\n!! That's probably not a correct username. !!\n");
            return;
        }
        System.out.println("========================");
        System.out.printf("User '%s' logged in\n", user);
        System.out.println("========================");

        loggedUser = user;
    }

    public void logout() {
        if (loggedUser == null) {
            System.out.println("\n!! Can't logout if there's no user. !!\n");
            return;
        }
        System.out.println("\n=======================");
        System.out.printf("User '%s' logged out\n", loggedUser);
        System.out.println("=======================\n");
        loggedUser = null;
    }

    public File getFile(String fileName) {
        System.out.printf("\n* Retrieving file '%s' *\n", fileName);
        try {
            if (loggedUser == null)
                throw new NotEnoughPermissionsException("No user logged in.");
            File found = lookForFile(fileName);
            return accessFile(found);
        }
        catch (FileException e) {
            System.out.printf(e.getMessage() + "\n");
        }
        return null;
    }

    public void createFile(String fileName) {
        System.out.printf("\n* Creating file '%s' *\n", fileName);
        try {
            if (loggedUser == null)
                throw new NotEnoughPermissionsException("No user logged in.");
            int newPosInDisk = nextFreeSpace();
            File newFile = new File(fileName, loggedUser);
            disk[newPosInDisk] = newFile;
        }
        catch (FileException e) {
            System.out.printf(e.getMessage() + "\n");
        }
    }

    private int nextFreeSpace() throws NotEnoughSpaceException {
        for (int i = 0; i < disk.length; i++) {
            if (disk[i] == null)
                return i;
        }
        throw new NotEnoughSpaceException();
    }

    private File lookForFile(String fileName) throws FileNotFoundException {
        for (File file : disk) {
            if (file == null)
                continue;
            if (file.getName().equals(fileName))
                return file;
        }
        throw new FileNotFoundException(String.format("'%s' not found", fileName));
    }

    private File accessFile(File file) throws NotEnoughPermissionsException {
        if (file.getOwner().equals(loggedUser))
            return file;
        throw new NotEnoughPermissionsException(
                String.format("'%s' not accessible by user %s",
                        file.getName(), loggedUser));
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder("\nFiles in disk: {\n");
        for (int i = 0; i < disk.length-1; i++) {
            res.append("\t");
            res.append((disk[i] != null) ? disk[i] : "");
            res.append(",\n");
        }
        res.append("\t");
        res.append((disk[disk.length-1] != null) ? disk[disk.length-1] : "");
        res.append("\n;");
        return res.toString();
    }
}
