/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2026 Rainbownapkin and the TTN Community

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.*/

class registerPrompt{
    constructor(){
        //Grab user prompt
        this.user = document.querySelector("#login-page-username");
        this.user.value = window.location.search.replace("?user=",'');
        //Grab pass prompts
        this.pass = document.querySelector("#login-page-password");
        //Remember me checkbox
        this.rememberMe = document.querySelector("#login-page-remember-me");
        //Grab register button
        this.button = document.querySelector("#login-page-button");
        //Grab altcha widget
        this.altcha = document.querySelector("altcha-widget");
        //Setup null property to hold verification payload from altcha widget
        this.verification = null

        //Run input setup after DOM content has completely loaded to ensure altcha event listeners work
        document.addEventListener('DOMContentLoaded', this.setupInput.bind(this));
    }

    setupInput(){
        //If we need verification
        if(this.altcha != null){
            //Add verification event listener to altcha widget
            this.altcha.addEventListener("verified", this.verify.bind(this));
        }

        //Add register event listener to register button
        this.button.addEventListener("click", this.login.bind(this));
    }

    verify(event){
        //pull verification payload from event
        this.verification = event.detail.payload;
    }

    login(){
        //If we need verification
        if(this.altcha != null){
            //If verification isn't complete
            if( this.verification == null){
                //don't bother
                return;
            }

            //login with verification
            utils.ajax.login(this.user.value , this.pass.value, this.rememberMe.checked, this.verification);
        }else{
            //login
            utils.ajax.login(this.user.value, this.pass.value, this.rememberMe.checked);
        }
    }
}

const registerForm = new registerPrompt();