text: const { PIXI, ID, Event, Debug } = require('./Utilities/Utilities');
  const GameProperties = require('./GameProperties');
  
  // class
  let Scene;
  
  // variables
  Scene = function(_id, _name = 'untitledScene', _narrative) {
    if (_id == null) _id = ID.newID; // NEVER MODIFY THIS
    this.id = _id;
    this.name = _name;
  
    this.container = null;
    this.selected = false;
    this.narrative = _narrative;
    this.bgSrc = 'src/picture.png'; // TODO replace that
  
    this.objectList = [];
  
    GameProperties.AddScene(this);
  };
  
  // static
  Scene.AddScene = function(_name) {
    const scene = new Scene(null, _name, '');
    scene.InitContainer();
    return scene;
  };
  
  Scene.LoadScene = function(_data) {
    const scene = new Scene(_data.id, _data.name, _data.narrative);
    scene.InitContainer();
    return scene;
  };
  
  Scene.addObj = function(_obj) {
    this.objectList.push(_obj);
  };
  
  Scene.removeObj = function(_obj) {
    for (const i in this.objectList) {
      if (this.objectList[i]._id == _obj.id) {
        this.objectList.splice(i, 1);
        return;
      }
    }
  };
  
  // functions
  Scene.prototype.InitContainer = function() {
    this.container = new PIXI.Container();
    this.container.visible = false;
    const tutorialSprite = PIXI.Sprite.fromImage(
      '../Pages/src/SceneBuilderTutorial.png'
    );
    this.container.addChild(tutorialSprite);
    tutorialSprite.x = 0;
    tutorialSprite.y = 0;
    tutorialSprite.visible = true;
    tutorialSprite.interactive = true;
    tutorialSprite.width = GameProperties.instance.projectData.viewWidth;
    tutorialSprite.height = GameProperties.instance.projectData.viewHeight;
    tutorialSprite.texture.baseTexture.on('loaded', () => {
      tutorialSprite.width = GameProperties.instance.projectData.viewWidth;
      tutorialSprite.height = GameProperties.instance.projectData.viewHeight;
      console.log(`${tutorialSprite.width} ${tutorialSprite.x}`);
    });
  
    this.container.on('pointerup', () => {
      console.log('Container pointup');
    });
  };
  
  Scene.prototype.SetAsStartScene = function() {
    if (!GameProperties.ProjectLoaded()) return;
    GameProperties.instance.settings.startScene = this.id;
  };
  
  Scene.prototype.DeleteThis = function() {
    if (!GameProperties.ProjectLoaded()) return;
    if (GameProperties.GetSceneLength() <= 1) {
      Debug.LogError('You have to keep at least one scene!');
      return false;
    }
    // Delete objects in scene
    const objToDelete = [];
    GameProperties.instance.objectList.forEach(obj => {
      if (obj.bindScene == this) {
        objToDelete.push(obj);
      }
    });
    objToDelete.forEach(obj => {
      obj.id = -1;
      obj.name = 'null';
      obj.DeleteThis();
    });
    // Delete scene
    if (this.container != null) {
      if (this.container.parent != null)
        this.container.parent.removeChild(this.container);
      this.container.destroy();
    }
    GameProperties.DeleteScene(this);
    Event.Broadcast('delete-scene', this.id);
  };
  
  Scene.prototype.GetFirstObject = function() {
    // TODO: replace this with BG sprite
    if (!GameProperties.ProjectLoaded()) return null;
    for (const i in GameProperties.instance.objectList) {
      if (GameProperties.instance.objectList[i].bindScene.id == this.id) {
        return GameProperties.instance.objectList[i];
      }
    }
    return null;
  };
  
  Scene.prototype.SelectOn = function() {
    this.selected = true;
    this.container.visible = true;
  };
  
  Scene.prototype.SelectOff = function() {
    this.selected = false;
    this.container.visible = false;
  };
  
  Scene.prototype.toJsonObject = function() {
    return {
      id: this.id,
      name: this.name,
      narrative: this.narrative
    };
  };
  
  module.exports = Scene;
  