import { Deserialize } from "../../app/models/util/deserialize.model";
import { DiscoveryVolumeNamesModel } from "./DiscoveryVolumeNames.model";

export class IslandDataModel implements Deserialize {
  name: string;
  x: number;
  y: number;
  imagePath: string;
  landscapeMaterialOverride: number;
  sublevelNames: string[];
  discoveryVolumeNames: DiscoveryVolumeNamesModel[] = [];
  spawnerOverrides: object;
  extraSublevels: string[];
  treasureMapSpawnPoints: string[];
  wildPirateCampSpawnPoints: string[];
  minTreasureQuality: number;
  maxTreasureQuality: number;
  useNpcVolumesForTreasures: boolean;
  useLevelBoundsForTreasures: boolean;
  prioritizeVolumesForTreasures: boolean;
  islandTreasureBottleSupplyCrateOverrides: string;
  islandPoints: number;
  singleSpawnPointX: number;
  singleSpawnPointY: number;
  singleSpawnPointZ: number;

  // De-parsed from the name
  islandType: string;
  islandShape: string;
  climate: string;
  variant: string;

  private _climateLookup = {
    CH: "High Desert",
    CL: "Low Desert",
    CP: "Polar",
    EE: "Equatorial",
    EQ: "Equatorial Tropical",
    ET: "Eastern Temperate",
    PO: "Polar",
    TR: "Tropical",
    WF: "West Temperate",
    WR: "West Tropical",
    WT: "West Temperate",
    WU: "West Tundra",
    ER: "East Tropical",
    EU: "East Tundra",
    BO: "Tropical",
    CU: "Central Tundra"
  };

  private _variantLookup = {
    E: 'Freeport',
    PVE: 'Event'
  };

  deserialize(input: any): this {
    Object.assign(this, input);
    console.log(input);

    const parsedName = this.name.split('_');

    this.islandType = parsedName[0];
    this.islandShape = parsedName[1];
    this.climate = this._climateLookup[parsedName[2]];
    this.variant = parsedName[3] === undefined
      ? 'Original'
      : this._variantLookup[parsedName[3]] === undefined
        ? parsedName[3]
        : this._variantLookup[parsedName[3]];

    // Override image paths from server grid editor to included code
    if (this.imagePath.substr(2, 6) === 'Island') {
      this.imagePath = './assets/server-grid-editor/' + this.imagePath.substr(2);
    }

    this.discoveryVolumeNames = this.discoveryVolumeNames.map(
      i => new DiscoveryVolumeNamesModel().deserialize(i)
    );

    return this;
  }

  customDisplayName(): string {
    return `${this.islandType} (${this.islandShape}) - ${this.climate} (${this.variant})`
  }
}