import { Deserialize } from "../util/deserialize.model";
import { IslandInstanceModel } from "./island-instance.model";
import { ServerModel } from "./server.model";
import { ServerGridModel } from "../serverGrid.model";
import { IslandDataModel } from "../../../data/models/island-data.model";

export class ServerSublevelModel implements Deserialize {
  public name: string;
  public additionalTranslationX: number;
  public additionalTranslationY: number;
  public additionalTranslationZ: number = 0;
  public additionalRotationPitch: number = 0;
  public additionalRotationYaw: number = 0;
  public additionalRotationRoll: number = 0;
  public id: number;
  public landscapeMaterialOverride: number;

  deserialize(input: any): this {
    return Object.assign(this, input);
  }

  setupFromIsland(island: IslandInstanceModel, islandData: IslandDataModel, cell: ServerModel, grid: ServerGridModel) {
    this.id = island.id;
    this.landscapeMaterialOverride = islandData.landscapeMaterialOverride;
    this.additionalRotationYaw = island.rotation;

    // XY are based on centre of the cell
    // Use integers to reduce precision issues.
    // 10 Million chosen as is larger than gridSize recommended setting
    const multiplier = 10_000_000;
    const worldX = island.worldX * multiplier;
    const worldY = island.worldY * multiplier;
    const gridSize = grid.gridSize * multiplier;

    const newX = worldX - (gridSize * cell.gridX) - (gridSize / 2);
    this.additionalTranslationX = newX / multiplier;
    const newY = worldY - (gridSize * cell.gridY) - (gridSize / 2);
    this.additionalTranslationY = newY / multiplier;
  }
}