import { map } from 'rxjs';
import { Socket } from 'ngx-socket-io';
import { Injectable} from '@angular/core';

@Injectable()
export class SocketService{

  constructor(private socket: Socket) {}

  newConnection(){
    return this.socket.fromEvent('new_connection').pipe(map((data: any) => data))
  }

  getConnections(){
    this.socket.emit('get_connections');
  }

  currentConnections(){
    return this.socket.fromEvent('current_connections').pipe(map((data) => data))
  }

  disconnection(){
    return this.socket.fromEvent('disconnection').pipe(map((data) => data))
  }

  closeSessions(){
    return this.socket.fromEvent('close_sessions').pipe(map((data) => data))
  }

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

  register(){
    this.socket.emit('register', localStorage.getItem('idusuario'));
  }

  maintenanceActivated(){
    this.socket.emit('maintenance_activated');
  }

  unregister(id: string){
    this.socket.emit('unregister', id);
  }

  profileUpdated(id: string){
    this.socket.emit('profile_updated', id);
  }

  refreshPermissions(){
    return this.socket.fromEvent('refresh_permissions').pipe(map((data) => data))
  }

  publishAdvice(advice: string){
    this.socket.emit('new_advice', advice);
  }

  advicePublished(){
    return this.socket.fromEvent('advice_published').pipe(map((data) => data))
  }
}
