import { ShredderStorage } from './storage';
import { Scenario } from './models/scenario';

export class Scenarios {

  public scenarios = [];
  public currentScenario;

  constructor() {
    this.loadFromStorage();
  }

  loadFromStorage() {
    const fromStorage = ShredderStorage.getSerialized();

    fromStorage.scenarios.forEach((scenario) => {
      this.scenarios.push(new Scenario(scenario));

      this.setCurrentScenario(fromStorage.currentScenario);
    });
  }

  setCurrentScenario(scenarioId) {
    this.currentScenario = scenarioId ? this.getById(scenarioId) : null;
  }

  saveToStorage() {
    const currentScenarioId = this.currentScenario.id;

    ShredderStorage.save(this.scenarios, currentScenarioId);
    return true;
  }

  getById(scenarioId) {
    return this.scenarios.filter((scenario) => scenario.id === scenarioId)[0];
  }

  addRuleToScenario(scenarioId, capturedRequest) {
    this.getById(scenarioId).addRule(capturedRequest);

    this.saveToStorage();
  }

  addScenario(name) {
    this.scenarios.push(new Scenario({ name }));
    this.saveToStorage();
  }

  duplicateScenario(scenarioId) {
    const selectedScenario = this.getById(scenarioId);

    const newScenario = {
      name: `${ selectedScenario.name } copy`,
      rules: selectedScenario.rules,
    };

    this.scenarios.push(new Scenario(newScenario));

    this.saveToStorage();
  }

  removeScenario(scenarioId) {
    this.scenarios = this.scenarios
      .filter((scenario) => scenario.id !== scenarioId);

    this.saveToStorage();
  }

  updateRuleInScenario(scenarioId, ruleId, request, response) {
    this.getById(scenarioId).updateRule(ruleId, request, response);

    this.saveToStorage();
  }
}

export default new Scenarios();

@@ -117,75 +117,48 @@
 %0A%0A  
-public scenarios = %5B%5D;%0A  public currentScenario;%0A%0A  constructor() %7B
+constructor() %7B%0A    this.scenarios = %5B%5D;
 %0A   

remove typescript leftovers from Scenarios