'use strict';

const EventEmitter = require('events');

class State extends EventEmitter {
  constructor() {
    super();
    this.activeBots = new Map();
    this.proxyLease = null;
    this.testRunning = false;
    this.currentTarget = '';
    this.currentBotPassword = '';
    this.currentChatMessages = [];
    this.currentStayConnected = false;
    this.currentProxyStrategy = null;
    this.logs = [];
    this.proxyValidationJob = null;
    this.counters = {
      totalBotsSpawned: 0,
      totalConnectionsFailed: 0,
      totalKeepAlivesSent: 0,
      totalHandshakesOk: 0,
    };
    this.maxLogs = 2000;
  }

  addBot(bot) {
    this.activeBots.set(bot.id, bot);
  }

  removeBot(id) {
    this.activeBots.delete(id);
  }

  snapshot() {
    const bots = [];
    for (const b of this.activeBots.values()) bots.push(b.toJSON());
    return bots;
  }

  pushLog(entry) {
    this.logs.push(entry);
    if (this.logs.length > this.maxLogs) this.logs.shift();
  }

  clearLogs() {
    this.logs = [];
  }

  resetCounters() {
    this.counters = {
      totalBotsSpawned: 0,
      totalConnectionsFailed: 0,
      totalKeepAlivesSent: 0,
      totalHandshakesOk: 0,
    };
  }
}

module.exports = { State };
