import {describe, test, assert} from 'vitest';
import {
  LayerGroup,
  getLayerDivId,
  getLayerDetailsFromLayerDivId
} from '../../src/gui/layerGroup.js';

/**
 * Tests for the 'gui/layerGroup.js' file.
 */

describe('gui', () => {

  /**
   * Tests for {@link LayerGroup} string id.
   *
   * @function module:tests/gui~layergroup-string-id
   */
  test('LayerGroup string id', () => {
    // test #00
    const theoId00 = 'layerGroupA-layer-0';
    const theoDetails00 = {groupDivId: 'layerGroupA', layerIndex: 0};
    const id00 = getLayerDivId(
      theoDetails00.groupDivId, theoDetails00.layerIndex);
    const details00 = getLayerDetailsFromLayerDivId(theoId00);
    assert.equal(id00, theoId00, 'getLayerDivId #00');
    assert.equal(details00.groupDivId, theoDetails00.groupDivId,
      'getLayerDetailsFromLayerDivId groupId #00');
    assert.equal(details00.layerIndex, theoDetails00.layerIndex,
      'getLayerDetailsFromLayerDivId layerId #00');

    // test #01
    const theoId01 = 'layerGroupB-layer-1';
    const theoDetails01 = {groupDivId: 'layerGroupB', layerIndex: 1};
    const id01 = getLayerDivId(
      theoDetails01.groupDivId, theoDetails01.layerIndex);
    const details01 = getLayerDetailsFromLayerDivId(theoId01);
    assert.equal(id01, theoId01, 'getLayerDivId #01');
    assert.equal(details01.groupDivId, theoDetails01.groupDivId,
      'getLayerDetailsFromLayerDivId groupId #01');
    assert.equal(details01.layerIndex, theoDetails01.layerIndex,
      'getLayerDetailsFromLayerDivId layerId #01');
  });

  /**
   * Tests for {@link LayerGroup} creation.
   *
   * @function module:tests/gui~layergroup-creation
   */
  test('LayerGroup creation', () => {
    const element00 = document.createElement('div');
    element00.id = 'layerGroup00';
    const layerGroup00 = new LayerGroup(element00);
    assert.equal(layerGroup00.getNumberOfLayers(), 0,
      'new layerGroup has no layers');
    assert.equal(layerGroup00.getDivId(), element00.id,
      'new layerGroup div id');
  });

  /**
   * Tests for {@link LayerGroup} add/remove view layer.
   *
   * @function module:tests/gui~layergroup-add-remove-view-layer
   */
  test('LayerGroup add remove view layer', () => {
    const element00 = document.createElement('div');
    element00.id = 'layerGroup00';
    const layerGroup00 = new LayerGroup(element00);
    assert.equal(layerGroup00.getNumberOfLayers(), 0,
      'new layerGroup has no layers');

    const vl00 = layerGroup00.addViewLayer();
    assert.equal(layerGroup00.getNumberOfLayers(), 1,
      'layerGroup has one view layers after add');
    let activeType = typeof layerGroup00.getActiveViewLayer();
    assert.ok(activeType !== 'undefined',
      'layerGroup active view layer is defined after add');

    layerGroup00.removeLayer(vl00);
    assert.equal(layerGroup00.getNumberOfLayers(), 0,
      'layerGroup has no view layers after remove');
    activeType = typeof layerGroup00.getActiveViewLayer();
    assert.ok(activeType === 'undefined',
      'layerGroup active view layer is undefined after remove');
  });

  /**
   * Tests for {@link LayerGroup} add/remove draw layer.
   *
   * @function module:tests/gui~layergroup-add-remove-draw-layer
   */
  test('LayerGroup add remove draw layer', () => {
    const element00 = document.createElement('div');
    element00.id = 'layerGroup00';
    const layerGroup00 = new LayerGroup(element00);
    assert.equal(layerGroup00.getNumberOfLayers(), 0,
      'new layerGroup has no layers');

    const dl00 = layerGroup00.addDrawLayer();
    assert.equal(layerGroup00.getNumberOfLayers(), 1,
      'layerGroup has one draw layers after add');
    let activeType = typeof layerGroup00.getActiveDrawLayer();
    assert.ok(activeType !== 'undefined',
      'layerGroup active draw layer is defined after add');

    layerGroup00.removeLayer(dl00);
    assert.equal(layerGroup00.getNumberOfLayers(), 0,
      'layerGroup has no draw layers after remove');
    activeType = typeof layerGroup00.getActiveDrawLayer();
    assert.ok(activeType === 'undefined',
      'layerGroup active draw layer is undefined after remove');
  });

});
