' Login dialog with username/password validation via Win32 DECLARE
#COMPILE EXE
#DIM ALL

DECLARE FUNCTION MessageBox LIB "USER32.DLL" ALIAS "MessageBoxA" (BYVAL hWnd AS LONG, BYVAL lpText AS ASCIIZ, BYVAL lpCaption AS ASCIIZ, BYVAL uType AS LONG) AS LONG
DECLARE FUNCTION DialogBoxParam LIB "USER32.DLL" ALIAS "DialogBoxParamA" (BYVAL hInstance AS LONG, BYVAL lpTemplateName AS ASCIIZ, BYVAL hWndParent AS LONG, BYVAL lpDialogFunc AS LONG, BYVAL dwInitParam AS LONG) AS LONG
DECLARE FUNCTION GetDlgItemText LIB "USER32.DLL" ALIAS "GetDlgItemTextA" (BYVAL hDlg AS LONG, BYVAL nIDDlgItem AS LONG, BYVAL lpString AS ASCIIZ, BYVAL nMaxCount AS LONG) AS LONG
DECLARE FUNCTION SetDlgItemText LIB "USER32.DLL" ALIAS "SetDlgItemTextA" (BYVAL hDlg AS LONG, BYVAL nIDDlgItem AS LONG, BYVAL lpString AS ASCIIZ) AS LONG
DECLARE FUNCTION EndDialog LIB "USER32.DLL" (BYVAL hDlg AS LONG, BYVAL nResult AS LONG) AS LONG

%ID_USERNAME = 101
%ID_PASSWORD = 102
%ID_LOGIN    = 103
%ID_CANCEL   = 104

FUNCTION ValidateUsername(BYVAL name AS STRING) AS LONG
    LOCAL n AS LONG
    n = LEN(name)
    IF n < 3 THEN
        MessageBox(0, "Username must be at least 3 characters", "Validation Error", 0)
        FUNCTION = 0
    END IF
    IF n > 32 THEN
        MessageBox(0, "Username must be at most 32 characters", "Validation Error", 0)
        FUNCTION = 0
    END IF
    LOCAL i AS LONG
    FOR i = 1 TO n
        LOCAL ch AS STRING
        ch = MID$(name, i, 1)
        IF ch = " " OR ch = CHR$(9) THEN
            MessageBox(0, "Username cannot contain spaces", "Validation Error", 0)
            FUNCTION = 0
        END IF
    NEXT i
    FUNCTION = 1
END FUNCTION

FUNCTION ValidatePassword(BYVAL pwd AS STRING) AS LONG
    LOCAL n AS LONG
    n = LEN(pwd)
    IF n < 6 THEN
        MessageBox(0, "Password must be at least 6 characters", "Validation Error", 0)
        FUNCTION = 0
    END IF
    IF n > 64 THEN
        MessageBox(0, "Password too long (max 64)", "Validation Error", 0)
        FUNCTION = 0
    END IF
    LOCAL hasUpper  AS LONG
    LOCAL hasLower  AS LONG
    LOCAL hasDigit  AS LONG
    hasUpper = 0
    hasLower = 0
    hasDigit = 0
    LOCAL i AS LONG
    FOR i = 1 TO n
        LOCAL ch AS STRING
        ch = MID$(pwd, i, 1)
        IF ch >= "A" AND ch <= "Z" THEN hasUpper = 1
        IF ch >= "a" AND ch <= "z" THEN hasLower = 1
        IF ch >= "0" AND ch <= "9" THEN hasDigit = 1
    NEXT i
    IF hasUpper = 0 THEN
        MessageBox(0, "Password needs an uppercase letter", "Validation Error", 0)
        FUNCTION = 0
    END IF
    IF hasLower = 0 THEN
        MessageBox(0, "Password needs a lowercase letter", "Validation Error", 0)
        FUNCTION = 0
    END IF
    IF hasDigit = 0 THEN
        MessageBox(0, "Password needs a digit", "Validation Error", 0)
        FUNCTION = 0
    END IF
    FUNCTION = 1
END FUNCTION

FUNCTION CheckCredentials(BYVAL user AS STRING, BYVAL pass AS STRING) AS LONG
    IF user = "admin" AND pass = "Secret1" THEN
        FUNCTION = 1
    ELSEIF user = "user" AND pass = "Hello2" THEN
        FUNCTION = 1
    ELSEIF user = "guest" AND pass = "Guest3" THEN
        FUNCTION = 1
    END IF
    FUNCTION = 0
END FUNCTION

FUNCTION HashPassword(BYVAL pwd AS STRING) AS STRING
    LOCAL i  AS LONG
    LOCAL h  AS LONG
    LOCAL r AS STRING
    r = ""
    FOR i = 1 TO LEN(pwd)
        h = ASC(MID$(pwd, i, 1))
        h = (h * 7 + 13) MOD 256
        r = r + CHR$(h)
    NEXT i
    FUNCTION = r
END FUNCTION

SUB SimulateLogin()
    PRINT "Login dialog simulation:"
    PRINT "  Dialog with USERNAME, PASSWORD, LOGIN, CANCEL"
    PRINT ""
    PRINT "  Test user 'admin' / pass 'Secret1':"
    LOCAL u  AS STRING
    LOCAL p  AS STRING
    u = "admin"
    p = "Secret1"
    PRINT "    Username: "; u
    PRINT "    Password: "; p
    PRINT "    Valid user: "; ValidateUsername(u)
    PRINT "    Valid pass: "; ValidatePassword(p)
    PRINT "    Credentials: "; CheckCredentials(u, p)
    PRINT ""
    PRINT "  Test user 'bad' / pass 'short':"
    u = "bad"
    p = "short"
    PRINT "    Valid user: "; ValidateUsername(u)
    PRINT "    Valid pass: "; ValidatePassword(p)
    PRINT ""
    PRINT "  Password hash: "; HashPassword("Secret1")
END SUB

SUB LoginAttempts()
    LOCAL users(0:2) AS STRING
    LOCAL passes(0:2) AS STRING
    users(0) = "admin"
    passes(0) = "Secret1"
    users(1) = "user"
    passes(1) = "wrong"
    users(2) = "unknown"
    passes(2) = "Test123"
    LOCAL i AS LONG
    FOR i = 0 TO 2
        PRINT "Attempt "; i + 1; ": user='"; users(i); "' pass='"; passes(i); "'"
        LOCAL validU  AS LONG
        LOCAL validP  AS LONG
        LOCAL creds  AS LONG
        validU = ValidateUsername(users(i))
        validP = ValidatePassword(passes(i))
        IF validU AND validP THEN
            creds = CheckCredentials(users(i), passes(i))
            PRINT "  Result: "; creds
        ELSE
            PRINT "  Validation failed"
        END IF
    NEXT i
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=== Login Dialog Demo ==="
    PRINT "Win32: DialogBoxParam, GetDlgItemText via DECLARE"
    PRINT ""
    SimulateLogin()
    PRINT ""
    LoginAttempts()
    PRINT ""
    PRINT "=== End Login Dialog ==="
    FUNCTION = 0
END FUNCTION
