/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024  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 channel{
    constructor(){
        //Establish connetion to the server via socket.io
        this.connect();
        //Define socket listeners
        this.defineListeners();

        //Scrape channel name off URL
        this.channelName = window.location.pathname.split('/c/')[`1`];

        //Create the Video Player Object
        this.player = new player(this);
        //Create the Chat Box Object
        this.chatBox = new chatBox(this);
        //Create the User List Object
        this.userList = new userList(this);
        //Create the Canopy Panel Object
        this.cPanel = new cPanel(this);
    }

    connect(){
        this.socket = io();
    }

    defineListeners(){
        this.socket.on("connect", () => {
            document.title = `${this.channelName} - Connected`
        });

        this.socket.on("kick", (data) => {
            new canopyUXUtils.popup(`You have been ${data.type} from the channel for the following reason:<br>${data.reason}`);
        });

        this.socket.on("clientMetadata", this.handleClientInfo.bind(this));

        this.socket.on("error", console.log);
    }

    handleClientInfo(data){
        this.user = {
            id: data.user.id,
            name: data.user.name,
            rank: data.user.rank
        }

        this.chatBox.handleClientInfo(data);
    }
}

const client = new channel();