import {Container} from '@pixi/react';
import {useCallback, useState} from 'react';

import {useEcsTick} from '@/react/context/ecs.js';
import {useMainEntity} from '@/react/context/main-entity.js';

import Entities from './entities.jsx';
import TargetingGhost from './targeting-ghost.jsx';
import TargetingGrid from './targeting-grid.jsx';
import TileLayer from './tile-layer.jsx';
import Water from './water.jsx';

export default function Ecs({camera, monopolizers, particleWorker, scale}) {
  const [mainEntity] = useMainEntity();
  const [layers, setLayers] = useState([]);
  const [hour, setHour] = useState(10);
  const [projected, setProjected] = useState([]);
  const [position, setPosition] = useState({x: 0, y: 0});
  const [water, setWater] = useState();
  const onEcsTick = useCallback((payload, ecs) => {
    const entity = ecs.get(mainEntity);
    for (const id in payload) {
      const update = payload[id];
      switch (id) {
        case '1': {
          const master = ecs.get(1);
          if (update.TileLayers) {
            setLayers(Object.values(master.TileLayers.$$layersProxies));
          }
          if (update.Time) {
            setHour(Math.round(ecs.get(1).Time.hour * 60) / 60);
          }
          if (update.Water) {
            setWater(master.Water.water);
          }
          break;
        }
      }
    }
    if (entity) {
      const {Direction, Position, Wielder} = entity;
      setPosition(Position.toJSON());
      setProjected(Wielder.activeItem()?.project(Position.tile, Direction.quantize(4)));
    }
  }, [mainEntity]);
  useEcsTick(onEcsTick);
  return (
    <Container
      scale={scale}
      x={-camera.x}
      y={-camera.y}
    >
      <Container>
        {layers.map((layer, i) => (
          <TileLayer
            key={i}
            tileLayer={layer}
          />
        ))}
      </Container>
      {water && layers[0] && (
        <Water
          tileLayer={layers[0]}
          water={water}
        />
      )}
      {projected && layers[0] && (
        <TargetingGrid
          tileLayer={layers[0]}
          x={position.x}
          y={position.y}
        />
      )}
      {projected?.length > 0 && layers[0] && (
        <TargetingGhost
          projected={projected}
          tileLayer={layers[0]}
        />
      )}
      <Entities
        monopolizers={monopolizers}
        particleWorker={particleWorker}
      />
    </Container>
  )
}
