using UnityEngine;
using System.Collections.Generic;

/*
 * A type of GamePolygon representing a region in the map
*/
public class MapPolygon : GamePolygon {

    private DTerrain terrain;

	public MapPolygon(SlicedPolygon slicedPolygon, DTerrain terrain) : base(slicedPolygon) {
        this.terrain = terrain;
    }

	public MapPolygon(Contour contour, DTerrain terrain) : this(new SlicedPolygon(contour), terrain) { }

	public MapPolygon(GameVertex origin, float size, DTerrain terrain) : this(createSliced(origin, size), terrain) { }

    public override void addToGame() {
        base.addToGame();
        renderer.material = terrain.getMaterial();
    }

	public DTerrain getTerrain() {
        return terrain;
    }

    public void triangulate(Set<TerrainVertex> terrainVertices, Set<TerrainSegment> terrainSegments, Set<TerrainTriangle> terrainTriangles) {
        Triangulation.triangulateForTerrain(this, terrainVertices, terrainSegments, terrainTriangles);
    }

	private static SlicedPolygon createSliced(GameVertex origin, float size) {
		ContourBuilder builder = new ContourBuilder();
		builder.addVertex(new GameVertex(origin.x() - size / 2, origin.y() - size / 2));
		builder.addVertex(new GameVertex(origin.x() - size / 2, origin.y() + size / 2));
		builder.addVertex(new GameVertex(origin.x() + size / 2, origin.y() + size / 2));
		builder.addVertex(new GameVertex(origin.x() + size / 2, origin.y() - size / 2));

		return new SlicedPolygon(builder.build());
	}
}
