import java.util.Scanner;

public class Authentification {

    private Node root;

    // Ajouter un utilisateur
    public void addUser(String username, String password) {
        root = addUserRec(root, username, password);
        System.out.println("Utilisateur \"" + username + "\" ajouté");
    }

    private Node addUserRec(Node node, String username, String password) {
        if (node == null) {
            return new Node(username, password);
        }
        if (username.compareTo(node.username) < 0) {
            node.left = addUserRec(node.left, username, password);
        } else if (username.compareTo(node.username) > 0) {
            node.right = addUserRec(node.right, username, password);
        } else {
            node.password = password; // Mise à jour du mot de passe si utilisateur existe
        }
        return node;
    }

    // Supprimer un utilisateur
    public void removeUser(String username) {
        root = removeUserRec(root, username);
    }

    private Node removeUserRec(Node node, String username) {
        if (node == null) {
            System.out.println("Utilisateur \"" + username + "\" non trouvé.");
            return null;
        }

        if (username.compareTo(node.username) < 0) {
            node.left = removeUserRec(node.left, username);
        } else if (username.compareTo(node.username) > 0) {
            node.right = removeUserRec(node.right, username);
        } else {
            // Cas où l'utilisateur est trouvé
            if (node.left == null){
                System.out.println("Utilisateur \"" + username + "\" retiré");
                return node.right;
            }
            if (node.right == null){
                System.out.println("Utilisateur \"" + username + "\" retiré");
                return node.left;
            }

            // Remplacer par le successeur in-order
            Node minNode = findMin(node.right);
            node.username = minNode.username;
            node.password = minNode.password;
            node.right = removeUserRec(node.right, minNode.username);
        }
        System.out.println("Utilisateur \"" + username + "\" retiré");
        return node;
    }

    private Node findMin(Node node) {
        while (node.left != null) {
            node = node.left;
        }
        return node;
    }

    // Authentification d’un utilisateur
    public void authenticate(String username, String password) {
        Node user = findUser(root, username);
        if (user != null && user.password.equals(password)) {
            System.out.println("Utilisateur \"" + username + "\" reconnu");
        } else {
            System.out.println("Utilisateur \"" + username + "\" non reconnu");
        }
    }

    private Node findUser(Node node, String username) {
        if (node == null){
             return null;
        }
        if (username.equals(node.username)){
             return node;
        }
        if (username.compareTo(node.username) < 0) {
            return findUser(node.left, username);
        }
        return findUser(node.right, username);
    }

    // Main pour le test
    public static void main(String[] args) {
        Authentification auth = new Authentification();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.print("> ");
            String command = scanner.nextLine();
            String[] parts = command.split(" ");

            if (parts.length == 0) continue;

            switch (parts[0].toLowerCase()) {
                case "add":
                    if (parts.length == 3) {
                        auth.addUser(parts[1], parts[2]);
                    } else {
                        System.out.println("Usage : add <username> <password>");
                    }
                    break;

                case "del":
                    if (parts.length == 2) {
                        auth.removeUser(parts[1]);
                    } else {
                        System.out.println("Usage : del <username>");
                    }
                    break;

                case "auth":
                    if (parts.length == 3) {
                        auth.authenticate(parts[1], parts[2]);
                    } else {
                        System.out.println("Usage : auth <username> <password>");
                    }
                    break;

                case "quit":
                    System.out.println("Au revoir");
                    scanner.close();
                    return;

                default:
                    System.out.println("Commande non reconnue.");
            }
        }
    }
}
