import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.*;
/**
 * This is a file to investigate issues with passwords in programming. This should never be used in a production system.
 * @author cpresser
 *
 */
public class Passwords {
        
        private String passwordFile;

        
        /**
         * Construct a password object for user authentication.
         * @param filename The name of the password file.
         */
        public Passwords (String filename) {
                passwordFile = filename;


        }
        
        /**
         * 
         * Check if a user exists in the password file with the given password.
         * 
         * @param user String containing a user name.
         * @param password character array containing a password
         * @return true if the user name is in the password file, with the
         * correct password, false otherwise.
         */
        public boolean authenticateUser(String user, char[] password) {
                
                try (
                                Scanner reader = new Scanner(new File(passwordFile))
                        )
                {
                        String line = null;
                        while(reader.hasNextLine()) {
                                line = reader.nextLine();
                                String[] pieces = line.split(":");
                                if(pieces.length >= 2 && pieces[0].equals(user) && equals(pieces[1], password)) {

                                        return true;
                                }
                        }
                }
                catch(FileNotFoundException fnfEx) {
                        return false;
                }
                //reader is automatically closed
                return false;
        }
        
        /**
         * Add a user to the password file.
         * 
         * @param user String containing a user name.
         * @param password character array containing a password
         * @return true if the user does not exist in the file and is successfully added, false otherwise.
         */
        public boolean addUser(String user, char[] password) {
                //probably should check if the user exists
                if(isUser(user)) {
                        //user exists
                        return false;
                }
                try (
                        PrintWriter writer = new PrintWriter(new FileWriter(passwordFile, true), false);
                )
                {
                        writer.printf("%s:", user);
                        for(int i = 0; i < password.length; i++) {
                                writer.print(password[i]);
                        }
                        writer.println();
                }
                catch(IOException ioe) {
                        return false;
                }
                return true;
        }
        
        /**
         * Remove a user from the password file.
         * @param user String containing a user name.
         * @return true if the user is in the file and was removed, false otherwise.
         */
        public boolean removeUser(String user) {
                StringBuilder builder = new StringBuilder();
                boolean removed = false;
                try (
                                Scanner reader = new Scanner(new File(passwordFile))
                        )
                {
                        String line = null;     
                        while(reader.hasNextLine()) {
                                line = reader.nextLine();
                                String[] pieces = line.split(":");
                                //skip this user
                                if(pieces.length < 1 || !pieces[0].equals(user)) {
                                        builder.append(line);
                                        builder.append(System.lineSeparator());
                                }
                                else {
                                        removed = true;
                                }
                        }
                }
                catch(FileNotFoundException fnfEx) {
                        return false;
                }
                //reader is automatically closed
                try(    
                                PrintWriter writer = new PrintWriter(new FileWriter(passwordFile, false), false);
                        )
                {
                        writer.print(builder.toString());
                        writer.flush();
                        
                } catch (IOException ioe) {
                        //e.printStackTrace();
                        return false;
                }

        
                return removed;
        }
        
        /**
         * Check if the user exists in the password file.
         * @param user String containing a user name.
         * @return true if the user exists.
         */
        public boolean isUser(String user) {
                
                try (
                                Scanner reader = new Scanner(new File(passwordFile))
                        )
                {
                        String line = null;
                        while(reader.hasNextLine()) {
                                line = reader.nextLine();
                                String[] pieces = line.split(":");
                                if(pieces.length >= 1 && pieces[0].equals(user)) {
                                        return true;
                                }
                        }
                }
                catch(FileNotFoundException fnfEx) {
                        return false;
                }
                //reader is automatically closed
                return false;
        }
        
        
        private boolean equals(String s, char[] cArr) {
                if(s.length() != cArr.length)
                        return false;
                for(int i = 0; i < cArr.length; i++) {
                        if(cArr[i] != s.charAt(i)) {
                                return false;
                        }
                }
                return true;
        }
        
}