import {Container} from '@pixi/react';
import {memo} from 'react';

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

import Aabb from './aabb.jsx';
import Crosshair from './crosshair.jsx';
import Light from './light.jsx';
import SpriteComponent from './sprite.jsx';

function Entity({entity, ...rest}) {
  const [debug] = useDebug();
  const [mainEntity] = useMainEntity();
  if (!entity) {
    return false;
  }
  const {Direction, id, Sprite} = entity;
  return (
    <>
      <Container
        x={entity.Position.x}
        y={entity.Position.y}
        zIndex={entity.Position?.y || 0}
      >
        {entity.Sprite && (
          <SpriteComponent
            alpha={Sprite.alpha}
            anchor={Sprite.anchor}
            animation={Sprite.animation}
            direction={Direction?.direction}
            frame={Sprite.frame}
            id={id}
            scale={Sprite.scale}
            rotates={Sprite.rotates}
            rotation={Sprite.rotation}
            source={Sprite.source}
            tint={Sprite.tint}
            {...rest}
          />
        )}
        {entity.Light && (
          <Light
            brightness={entity.Light.brightness}
          />
        )}
        {debug && entity.Position && (
          <Crosshair />
        )}
      </Container>
      {debug && (
        <Aabb
          color={0xff00ff}
          {...entity.VisibleAabb}
        />
      )}
      {debug && entity.Collider && (
        <>
          <Aabb
            color={0xffffff}
            width={0.5}
            {...entity.Collider.aabb}
          />
          {entity.Collider.aabbs.map((aabb, i) => (
            <Aabb
              color={0xffff00}
              width={0.25}
              key={i}
              {...aabb}
            />
          ))}
        </>
      )}
      {debug && mainEntity == entity.id && (
        <Aabb
          color={0x00ff00}
          {...entity.Interacts.aabb()}
        />
      )}
    </>
  );
}

export default memo(Entity);
