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

@Injectable({
  providedIn: 'root'
})
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))
  }

  getNewProvider(){
    return this._socket.fromEvent('get_new_provider').pipe(map((data) => data));
  }

  registerProvider(id: string){
    this._socket.emit('new_provider', id);
  }

  sendNotice(msg:string) {
    this._socket.emit('send_notice', msg);
  }

  getNotice(){
    return this._socket.fromEvent('get_notice').pipe(map((data) => data));
  }

  getNewSubcontract(){
    return this._socket.fromEvent('get_new_subcontract').pipe(map((data) => data));
  }

  registerSubcontract(id: string){
    this._socket.emit('new_subcontract', id);
  }

  equipmentAlert(){
    return this._socket.fromEvent('equipment_alert').pipe(map((data: any) => data))
  }

  refreshCounterMeasures(){
    return this._socket.fromEvent('refresh_counter_measures').pipe(map((data: any) => data))
  }

  notificationPublished(){
    return this._socket.fromEvent('notification_published').pipe(map((data: any) => data))
  }

  controlPanelUpdated(id: string){
    this._socket.emit('control_panel_updated', id);
  }

  updatePanelElements(){
    return this._socket.fromEvent('update_panel_elements').pipe(map((data: any) => data))
  }

  changePanelAssociations(msg: string){
    this._socket.emit('change_panel_associations', msg);
  }

  panelAssociationsChanged(){
    return this._socket.fromEvent('panel_associations_changed').pipe(map((data: any) => data));
  }

  saveSession(msg: string){
    this._socket.emit('save_session', msg);
  }

  newSessionSaved(){
    return this._socket.fromEvent('new_session_saved').pipe(map((data: any) => data));
  }

  deleteSession(msg: string){
    this._socket.emit('delete_session', msg);
  }

  getSessions(){
    this._socket.emit('get_sessions');
  }

  enabledSessionsUpdated(msg: string){
    this._socket.emit('enabled_sessions_updated', msg);
  }

  updateEnabledSessions(){
    return this._socket.fromEvent('update_enabled_sessions').pipe(map((data: any) => data))
  }

  timerUpdated(){
    return this._socket.fromEvent('timer_updated').pipe(map((data: any) => data))
  }

  showTimerAlert(){
    return this._socket.fromEvent('show_timer_alert').pipe(map((data: any) => data))
  }

  getTimerEnabled(){
    this._socket.emit('get_timer_enabled');
  }

  timerEnabled(){
    return this._socket.fromEvent('timer_enabled').pipe(map((status: any) => status));
  }

  prtgInfoUpdated(){
    return this._socket.fromEvent('prtg_info_updated').pipe(map((data: any) => data));
  }

  modulesUpdated(){
    this._socket.emit('modules_updated');
  }

  refreshModules(){
    return this._socket.fromEvent('refresh_modules').pipe(map((data: any) => data));
  }
}
