import PlaceableTab from "./placeable-tab.mjs";
import AmbientLightFilter from "../filters/ambient-light-filter.mjs";

/**
 * The AmbientLight-specific placeables tab.
 * @extends {PlaceableTab}
 */
export default class AmbientLightTab extends PlaceableTab {
  constructor(...args) {
    super(...args);
    Object.assign(this._filterState, {
      animationType: null,
      color: null,
      negative: false,
      walls: false,
      vision: false
    });
  }

  /* -------------------------------------------- */

  /** @override */
  static FILTER_CLASS = AmbientLightFilter;

  /* -------------------------------------------- */
  /*  Filtering                                   */
  /* -------------------------------------------- */

  /** @inheritDoc */
  _applyFilters() {
    super._applyFilters();
    if ( !canvas.activeLayer || !canvas.scene ) return;
    this.layer.placeables.forEach(p => p.initializeLightSource());
  }

  /* -------------------------------------------- */

  /** @inheritDoc */
  _clearFilters() {
    super._clearFilters();
    Object.assign(this._filterState, {
      animationType: null, color: null, negative: false, walls: false, vision: false
    });
  }

  /* -------------------------------------------- */

  /** @inheritDoc */
  _matchesFilter(entry) {
    const { animationType, color, negative, walls, vision } = this._filterState;
    if ( animationType !== null ) {
      const type = entry.config.animation.type;
      const matchesAnimation = animationType === "none" ? type === null : type === animationType;
      if ( !matchesAnimation ) return false;
    }
    if ( (color !== null) && (entry.config.color?.css !== color) ) return false;
    if ( negative && !entry.config.negative ) return false;
    if ( walls && !entry.walls ) return false;
    if ( vision && !entry.vision ) return false;
    return true;
  }

  /* -------------------------------------------- */

  /** @inheritDoc */
  _hasAdvancedFilters() {
    const { animationType, color, negative, walls, vision } = this._filterState;
    return super._hasAdvancedFilters() || (animationType !== null) || (color !== null) || negative || walls || vision;
  }
}
