import openfl.display.Sprite;
import openfl.display.Bitmap;
import openfl.events.MouseEvent;
import MapDesc.TileDesc;

class TileEdit extends Sprite {
	public static final WIDTH:Int = 30;
	public static final HEIGHT:Int = 30;

	public var tileDesc:TileDesc = {
		wallTextureID: null,
		ceilTextureID: null,
		floorTextureID: null,
		collision: false
	};

	var editor:Main;

	var floorBitmap:Bitmap = null;
	var wallBitmap:Bitmap = null;
	var ceilBitmap:Bitmap = null;

	var floorBitmapLayer:Sprite = new Sprite();
	var wallBitmapLayer:Sprite = new Sprite();
	var ceilBitmapLayer:Sprite = new Sprite();

	var cursorLayer:Sprite = new Sprite();

	var cellX:Int;
	var cellY:Int;

	public function new(editor:Main, cellX:Int, cellY:Int) {
		super();

		this.editor = editor;
		this.cellX = cellX;
		this.cellY = cellY;

		graphics.beginFill(0x0);
		graphics.drawRect(0, 0, WIDTH, HEIGHT);
		graphics.endFill();

		buttonMode = true;

		addChild(floorBitmapLayer);
		addChild(wallBitmapLayer);
		addChild(ceilBitmapLayer);

		ceilBitmapLayer.alpha = 0.5;

		addChild(cursorLayer);

		cursorLayer.graphics.beginFill(0xff0000, 0.2);
		cursorLayer.graphics.drawRect(0, 0, WIDTH, HEIGHT);
		cursorLayer.graphics.endFill();

		cursorLayer.visible = false;

		addEventListener(MouseEvent.MOUSE_OVER, (event:MouseEvent) -> {
			cursorLayer.visible = true;
		});

		addEventListener(MouseEvent.MOUSE_OUT, (event:MouseEvent) -> {
			cursorLayer.visible = false;
		});

		addEventListener(MouseEvent.CLICK, (event:MouseEvent) -> {
			if (editor.mode == Tiles) {
				if (editor.wallTexture.selected != null) {
					tileDesc.wallTextureID = editor.textures.indexOf(editor.wallTexture.selected);
				} else {
					tileDesc.wallTextureID = null;
				}

				if (editor.floorTexture.selected != null) {
					tileDesc.floorTextureID = editor.textures.indexOf(editor.floorTexture.selected);
				} else {
					tileDesc.floorTextureID = null;
				}

				if (editor.ceilTexture.selected != null) {
					tileDesc.ceilTextureID = editor.textures.indexOf(editor.ceilTexture.selected);
				} else {
					tileDesc.ceilTextureID = null;
				}

				setTileData(tileDesc);
			} else {
				editor.mobsLayer.addMob({type: editor.selectedMob, x: cellX + 0.5, y: cellY + 0.5});
			}
		});
	}

	public function setTileData(tileData:TileDesc) {
		tileDesc = tileData;

		if (floorBitmap != null) {
			floorBitmapLayer.removeChild(floorBitmap);
		}

		if (tileDesc.floorTextureID != null) {
			floorBitmap = new Bitmap(editor.bitmaps[editor.textures[tileData.floorTextureID]]);
			floorBitmap.width = WIDTH;
			floorBitmap.height = HEIGHT;
			floorBitmapLayer.addChild(floorBitmap);
		}

		if (wallBitmap != null) {
			wallBitmapLayer.removeChild(wallBitmap);
		}

		if (tileDesc.wallTextureID != null) {
			wallBitmap = new Bitmap(editor.bitmaps[editor.textures[tileData.wallTextureID]]);
			wallBitmap.width = WIDTH;
			wallBitmap.height = HEIGHT;
			wallBitmapLayer.addChild(wallBitmap);

			tileData.collision = true;
		} else {
			tileData.collision = false;
		}

		if (ceilBitmap != null) {
			ceilBitmapLayer.removeChild(ceilBitmap);
		}

		if (tileDesc.ceilTextureID != null) {
			ceilBitmap = new Bitmap(editor.bitmaps[editor.textures[tileData.ceilTextureID]]);
			ceilBitmap.width = WIDTH;
			ceilBitmap.height = HEIGHT;
			ceilBitmapLayer.addChild(ceilBitmap);
		}
	}
}
