import {
  WorldManifest,
  WorldManifestRegion,
} from '../../common/world/world-manifest';
import { join } from 'path';
import { readFile } from 'fs/promises';
import { ASSETS } from '../constants';

const manifestAsset = join(ASSETS, 'terrain', 'manifest.json');

export class ServerWorldManifest implements WorldManifest {
  constructor(
    public worldWidth: number,
    public worldHeight: number,
    public worldChunkSize: number,
    public worldHeightScale: number,
    public textureBombingNoise: string,
    public regionMap: WorldManifestRegion[],
  ) {}

  public static async loadManifest(): Promise<ServerWorldManifest> {
    console.log(`<WORLD> Loading manifest..`);
    const file = await readFile(manifestAsset, { encoding: 'utf-8' });
    const json = JSON.parse(file);
    const manifest = new ServerWorldManifest(
      json.worldWidth,
      json.worldHeight,
      json.worldChunkSize,
      json.worldHeightScale,
      json.textureBombingNoise,
      json.regionMap,
    );

    console.log(
      `<WORLD> Loaded manifest: ${manifest.worldWidth} x ${manifest.worldHeight} ^ ${manifest.worldHeightScale}, ${manifest.regionMap.length} regions`,
    );
    return manifest;
  }
}
