import { Injectable } from '@angular/core';
import { ServerGridModel } from "./models/serverGrid.model";
import { Islands } from "../data/models/islands.model";

@Injectable()
export class Server {
  private rawData: string = "";
  private jsonData: object = {};
  public serverGrid: ServerGridModel = new ServerGridModel();
  public islands: Islands;

  constructor() {
    this.islands = new Islands();
  }

  public load(raw) {
    this.rawData = raw;
    try {
      this.jsonData = JSON.parse(this.rawData);
      this.serverGrid = new ServerGridModel().deserialize(this.jsonData);
    } catch (e) {
      console.log(e);
    }
  }

  public save() {
    this.rawData = JSON.stringify(this.serverGrid, null, 2);
    return this.rawData;
  }
}