/*
    Project Name: Real-Time Whiteboard
    Founded by: Molla Samser
    Designer & Tester: Rima Khatun
    Website: https://rskworld.in
    Email: hello@rskworld.in, info@rskworld.com
    Phone: +91 93305 39277
    Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147
    Year: 2026
    Copyright: © 2026 RSK World. All rights reserved.
*/
import { io } from 'socket.io-client';

export class SocketClient {
    constructor(url = 'http://localhost:3001', roomId = 'default-whiteboard', userName = '') {
        this.socket = io(url);
        this.roomId = roomId;
        this.userName = userName || 'User_' + Math.floor(Math.random() * 1000);
        this.users = [];
        this.socket.emit('join-room', { roomId: this.roomId, userName: this.userName });

        this.onUserListUpdate((users) => {
            this.users = users;
        });
    }

    emitDrawStart(data) {
        this.socket.emit('draw-start', { ...data, roomId: this.roomId });
    }

    emitDraw(data) {
        this.socket.emit('draw', { ...data, roomId: this.roomId });
    }

    emitDrawEnd() {
        this.socket.emit('draw-end', { roomId: this.roomId });
    }

    emitClear() {
        this.socket.emit('clear-board', { roomId: this.roomId });
    }

    emitChangeBg(bg) {
        this.socket.emit('change-bg', { bg, roomId: this.roomId });
    }

    emitAddSticky(data) {
        this.socket.emit('add-sticky', { ...data, roomId: this.roomId });
    }

    emitUpdateSticky(data) {
        this.socket.emit('update-sticky', { ...data, roomId: this.roomId });
    }

    emitDeleteSticky(id) {
        this.socket.emit('delete-sticky', { id, roomId: this.roomId });
    }

    emitCursorMove(x, y) {
        this.socket.emit('cursor-move', { x, y, roomId: this.roomId, userName: this.userName });
    }

    emitMessage(text) {
        this.socket.emit('send-message', { text, roomId: this.roomId, userName: this.userName });
    }

    emitTyping(isTyping) {
        this.socket.emit('typing', { isTyping, roomId: this.roomId, userName: this.userName });
    }

    emitReaction(emoji) {
        this.socket.emit('reaction', { emoji, roomId: this.roomId });
    }

    onRemoteDrawStart(callback) {
        this.socket.on('remote-draw-start', callback);
    }

    onRemoteDraw(callback) {
        this.socket.on('remote-draw', callback);
    }

    onRemoteClear(callback) {
        this.socket.on('remote-clear', callback);
    }

    onRemoteChangeBg(callback) {
        this.socket.on('remote-change-bg', callback);
    }

    onRemoteAddSticky(callback) {
        this.socket.on('remote-add-sticky', callback);
    }

    onRemoteUpdateSticky(callback) {
        this.socket.on('remote-update-sticky', callback);
    }

    onRemoteDeleteSticky(callback) {
        this.socket.on('remote-delete-sticky', callback);
    }

    onUserListUpdate(callback) {
        this.socket.on('user-list-update', callback);
    }

    onRemoteCursorMove(callback) {
        this.socket.on('remote-cursor-move', callback);
    }

    onNewMessage(callback) {
        this.socket.on('new-message', callback);
    }

    onTyping(callback) {
        this.socket.on('remote-typing', callback);
    }

    onReaction(callback) {
        this.socket.on('remote-reaction', callback);
    }

    onInitState(callback) {
        this.socket.on('init-state', callback);
    }

    onUserDisconnect(callback) {
        this.socket.on('remote-user-disconnect', callback);
    }
}
