package chapter4.demo49;

import java.util.Scanner;

public class Main {
    // 处理用户的输入
    private static Scanner scanner = new Scanner(System.in);
    // 用户管理器
    private static UserManager userManager = UserManager.getInstance();
    // 当前登录用户
    private static AbstractUser currentUser = null;

    public static void main(String[] args) {
        while (true) {
            printMainMenu();

            try {
                int choice = scanner.nextInt();
                switch (choice) {
                    case 1:
                        login();
                        break;
                    case 2:
                        register();
                        break;
                    case 3:
                        if (currentUser instanceof AdminUser) {
                            userManager.showAllUsers();
                        } else {
                            System.out.println("权限不足！");
                        }
                        break;
                    case 4:
                        if (currentUser instanceof AdminUser) {
                            System.out.print("请输入要删除的用户名：");
                            String username = scanner.next();
                            userManager.removeUser(username);
                            System.out.println("删除用户成功！");
                        } else {
                            System.out.println("权限不足！");
                        }
                        break;
                    case 0:
                        System.out.println("谢谢使用，再见！");
                        System.exit(0);
                    default:
                        System.out.println("无效的选择！");
                }
            } catch (Exception e) {
                System.out.println("发生错误：" + e.getMessage());
                scanner.nextLine(); // 清除输入缓冲
            }
        }
    }

    private static void printMainMenu() {
        System.out.println("\n=====用户管理系统=====");
        System.out.println("1. 用户登录");
        System.out.println("2. 用户注册");
        System.out.println("3. 查看用户列表（需管理员权限）");
        System.out.println("4. 删除用户（需管理员权限）");
        System.out.println("0. 退出系统");
        System.out.println("请选择功能：");
    }

    private static void login() throws UserException {
        System.out.print("请输入用户名：");
        String username = scanner.next();
        System.out.print("请输入密码：");
        String password = scanner.next();

        currentUser = userManager.login(username, password);
        System.out.println("登录成功！欢迎 " + currentUser.getUsername());

        if (currentUser instanceof AdminUser) {
            System.out.println("您以管理员身份登录");
        }
    }

    private static void register() throws UserException {
        System.out.print("请输入用户名：");
        String username = scanner.next();
        System.out.print("请输入密码：");
        String password = scanner.next();

        NormalUser newUser = new NormalUser(username, password);
        userManager.addUser(newUser);
        System.out.println("注册成功！");
    }
}
