package ecs.systems;

import ecs.components.Orbiter;
import ecs.core.Entity;
import ecs.core.System;
import res.display.FrameBuffer;

using Std;
using res.graphics.Painter;

class DrawOrbitsSystem extends System {
	var uniqueOrbits:Array<{
		rX:Float,
		rY:Float
	}> = [];

	public function new() {
		super(Orbiter);
	}

	function updateOrbits() {
		uniqueOrbits = [];

		for (entity in entities) {
			final orbiter = entity.get(Orbiter);

			var exists = false;

			for (orbit in uniqueOrbits) {
				if (orbit.rX == orbiter.rX && orbit.rY == orbiter.rY) {
					exists = true;
					break;
				}
			}

			if (!exists)
				uniqueOrbits.push({rX: orbiter.rX, rY: orbiter.rY});
		}
	}

	override function add(entity:Entity) {
		super.add(entity);
		updateOrbits();
	}

	override function remove(entity:Entity) {
		super.remove(entity);
		updateOrbits();
	}

	override function render(frameBuffer:FrameBuffer) {
		final orbitSystem = engine.getSystem(OrbitSystem);

		for (orbit in uniqueOrbits) {
			frameBuffer.ellipse(orbitSystem.centerX.int(), orbitSystem.centerY.int(), orbit.rX.int(), orbit.rY.int(), 9);
		}
	}
}
