package interfaces;

import quick.Action;
import quick.View;
import quick.Color;

public class Login extends View {
    private String username = "";
    private String password = "";
    private boolean loginSuccess = false;
    private int loginTries = 0;
    private String editField = null;
    private String error = "";

    @Override
    public Boolean init() {
        username = "";
        password = "";
        loginSuccess = false;
        loginTries = 0;
        editField = null;
        error = "";
        viewPrompt = "> ";
        return true;
    }

    @Override
    public void draw() {
        terminal.print(Color.ANSI_CYAN + "=== Login ===" + Color.ANSI_RESET + "\n");

        terminal.print("Username: " + (username.isEmpty() ? "<not set>" : username) + "\n");
        terminal.print("Password: " + (password.isEmpty() ? "<not set>" : "*".repeat(password.length())) + "\n");

        if (loginSuccess) {
            terminal.print(Color.ANSI_GREEN + "Login successful!" + Color.ANSI_RESET + "\n");
            terminal.print("Press any key to continue..." + "\n");
            viewPrompt = "> ";
            return;
        }

        if (!error.isEmpty()) {
            terminal.print(Color.ANSI_RED + error + Color.ANSI_RESET + "\n");
        }

        terminal.print("\nCommands:\n");
        terminal.print("  edit username   - enter username\n");
        terminal.print("  edit password   - enter password\n");
        terminal.print("  login           - attempt login\n");
        terminal.print("  back            - go back\n");

        if (editField != null) {
            viewPrompt = "Enter " + editField + ": ";
        } else {
            viewPrompt = "> ";
        }
    }

    @Override
    public Action onCommand(String command) {
        String cmd = command.trim().toLowerCase();

        if (cmd.isEmpty()) {
            error = "Empty input. Type a command.";
            return actions.nop();
        }

        if (cmd.equals("back")) {
            return actions.redirect(Index.class);
        }

        if (cmd.equals("login")) {
            if (username.equals("a") && password.equals("a")) {
                loginSuccess = true;
                error = "";
            } else {
                loginTries++;
                error = "Login failed (" + loginTries + " attempts).";
            }
            return actions.nop();
        }

        if (cmd.startsWith("edit")) {
            if (cmd.contains("username")) {
                editField = "username";
                error = "";
            } else if (cmd.contains("password")) {
                editField = "password";
                error = "";
            } else {
                error = "Unknown field to edit. Use: edit username | edit password";
            }
            return actions.nop();
        }

        if (editField != null) {
            if (editField.equals("username")) {
                username = command.trim();
            } else if (editField.equals("password")) {
                password = command.trim();
            }
            editField = null;
            error = "";
            return actions.nop();
        }

        error = "Unknown command: " + command;
        return actions.nop();
    }

    @Override
    public Boolean onSelection(String userInputRaw) {
        return false;
    }
}
