import * as BABYLON from 'babylonjs';
import * as MATERIALS from 'babylonjs-materials';
import { scene, newColor } from '../helpers';
import { exclusionMaterials } from '../enums';

export const getUserMaterials = () => {
  const materials = scene.materials.filter(material => !exclusionMaterials.includes(material.id));
  return materials;
};

export const getUserTextures = () => {
  const userMaterials = getUserMaterials();
  const userTextures = userMaterials.map(material => material.getActiveTextures());
  return userTextures.flat();
};

export const newPBRMaterial = name => {
  const defaultName = `pbrMaterial_${getUserMaterials().length + 1}`;
  return new BABYLON.PBRMaterial(name || defaultName, scene);
};

export const newStandardMaterial = name => {
  const defaultName = `standardMaterial_${getUserMaterials().length + 1}`;
  return new BABYLON.StandardMaterial(name || defaultName, scene);
};

export const newGridMaterial = name => {
  const gridMaterialName = name || `grid_${getUserMaterials().length}`;
  return new MATERIALS.GridMaterial(gridMaterialName, scene);
};

export const setDefaultGridProperties = material => {
  material.majorUnitFrequency = 1;
  material.minorUnitVisibility = 0.5;
  material.gridRatio = 1;
  material.backFaceCulling = false;
  material.mainColor = newColor(0.25, 0.27, 0.33);
  material.lineColor = newColor(0.33, 0.35, 0.42);
  material.opacity = 0.95;
};