import { TribeLogConfigModel } from "./tribeLogConfig.model";
import { SharedLogConfigModel } from "./sharedLogConfig.model";
import { TravelDataConfigModel } from "./travelDataConfig.model";
import { GlobalGameplaySetupModel } from "./GlobalGameplaySetupModel";
import { Deserialize } from "./util/deserialize.model";
import { DatabaseConnectionModel } from "./databaseConnection.model";
import { ServerModel } from "./atlasData/server.model";
import { ServerTemplateModel } from "./atlasData/serverTemplate.model";

export class ServerGridModel implements Deserialize {
  // Server Argument Section
  // Used internally by Grapeshot
  //public BaseServerArgs: string = "";
  // Probably also internal to Grapeshot
  //public AdditionalCmdLineParams: string = "";

  // World Options
  public WorldFriendlyName: string = "New Server";
  public WorldAtlasId: string = "";
  public WorldAtlasPassword: string = "";
  public ModIDs: string = "";

  // Grid Options
  public gridSize: number = 1000;
  public totalGridsX: number = 1;
  public totalGridsY: number = 1;
  public globalTransitionMinZ: number = 0.0;

  // Used by ServerGridEditor for Zoom Level, so ignored
  // Will be added if present in config when loading.
  //public coordsScaling: number = 0.000000000001;

  // Image Paths
  // Used for generating tile output
  //public backgroundImgPath: string = "image.png";
  // Used for displaying discovery zones in ServerGridEditor
  //public discoZonesImagePath: string = "image.png";

  // URL Options
  public MetaWorldURL: string = "";
  public AuthListURL: string = "";
  public MapImageURL: string = "";

  // Time Options
  public Day0: string = "2019-01-09 20:28:56";
  public bUseUTCTime: boolean = false;
  public columnUTCOffset: number = 0.0;
  public lastImageOverride: string = "0001-01-01T00:00:00";

  // Info Options for ServerGridEditor
  // public showServerInfo: boolean = false;
  // public showDiscoZoneInfo: boolean = false;
  // public showShipPathsInfo: boolean = false;
  // public showIslandNames: boolean = false;
  // public showLines: boolean = false;
  // public showBackground: boolean = false;

  // S3 Options
  public LocalS3URL: string = "";
  public LocalS3AccessKeyId: string = "";
  public LocalS3SecretKey: string = "";
  public LocalS3BucketName: string = "";
  public LocalS3Region: string = "";

  // ID Generator current values
  // Used for individual ship path
  public shipPathsIdGenerator: number = 0;
  // Used for Discovery Zone & Island IDs
  public idGenerator: number = 0;
  // Unused as far as can see in ServerGridEditor
  public regionsIdGenerator: number = 0;

  // Log Configs
  public TribeLogConfig: TribeLogConfigModel = new TribeLogConfigModel();
  public SharedLogConfig: SharedLogConfigModel = new SharedLogConfigModel();
  public TravelDataConfig: TravelDataConfigModel = new TravelDataConfigModel();

  // Quest Config
  public globalGameplaySetup: GlobalGameplaySetupModel = new GlobalGameplaySetupModel();

  public DatabaseConnections: DatabaseConnectionModel[];
  public servers: ServerModel[];
  public serverTemplates: ServerTemplateModel[];

  public deserialize(input: any): this {
    Object.assign(this, input);
    this.SharedLogConfig = new SharedLogConfigModel().deserialize(input.SharedLogConfig);
    this.TribeLogConfig = new TribeLogConfigModel().deserialize(input.TribeLogConfig);
    this.TravelDataConfig = new TravelDataConfigModel().deserialize(input.TravelDataConfig);
    this.globalGameplaySetup = new GlobalGameplaySetupModel().deserialize(input.globalGameplaySetup);
    this.DatabaseConnections = this.DatabaseConnections.map(i => new DatabaseConnectionModel().deserialize(i));
    this.servers = this.servers.map(i => new ServerModel().deserialize(i));
    this.serverTemplates = this.serverTemplates.map(i => new ServerTemplateModel().deserialize(i));
    return this;
  }

  getNextId() {
    return ++this.idGenerator;
  }
}
