using ClipperLib;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class TerrainTriangle : Triangle {

    new TerrainVertex v1;
    new TerrainVertex v2;
    new TerrainVertex v3;

    private TerrainSegment s1;
    private TerrainSegment s2;
    private TerrainSegment s3;

    private DTerrain terrain;

    List<TerrainTriangle> neighborgs = null;

    public TerrainTriangle(TerrainVertex v1, TerrainVertex v2, TerrainVertex v3, DTerrain terrain) : base(v1, v2, v3){
        this.v1 = v1;
        this.v2 = v2;
        this.v3 = v3;
        this.terrain = terrain;
    }

    public void link(TerrainSegment s) {
        if (s.getFirstPoint() == v1 && s.getSecondPoint() == v2) {
            s1 = s;
        }
        else if (s.getFirstPoint() == v2 && s.getSecondPoint() == v1) {
            s1 = s;
        }
        else if (s.getFirstPoint() == v2 && s.getSecondPoint() == v3) {
            s2 = s;
        }
        else if (s.getFirstPoint() == v3 && s.getSecondPoint() == v2) {
            s2 = s;
        }
        else if (s.getFirstPoint() == v3 && s.getSecondPoint() == v1) {
            s3 = s;
        }
        else if (s.getFirstPoint() == v1 && s.getSecondPoint() == v3) {
            s3 = s;
        }
        else {
            Debug.Log("Bad link");
        }
    }

    public bool hasThreeNeighbors() {
        return s1.hasTwoTriangles() && s2.hasTwoTriangles() && s3.hasTwoTriangles();
    }

    public DTerrain getTerrain() {
        return terrain;
    }

    public List<TerrainTriangle> getNeighborgs() {
        if (neighborgs != null) {
            return neighborgs;
        }

        neighborgs = new List<TerrainTriangle>();
        addNeighborgs(s1);
        addNeighborgs(s2);
        addNeighborgs(s3);

        return neighborgs;
    }

    private void addNeighborgs(TerrainSegment segment) {
        TerrainTriangle n = segment.getOther(this);
        if (n != null) {
            neighborgs.Add(n);
        }
    }

	//These properties are used only for path finding
	public TerrainTriangle previous { get; set; }
	public double weight { get; set; }
	public bool processed { get; set; }
}
