using ClipperLib;
using UnityEngine;

public class GameVertex {
	protected readonly Vector2 vec; // in gamecoo
	protected readonly IntPoint pt;

	public GameVertex(double x, double y) {
		vec = new Vector2((float)x, (float)y);
		pt = Converter.vectorToIntPoint(vec);
	}

	public GameVertex(Vector2 vec) {
		pt = Converter.vectorToIntPoint(vec);
		this.vec = vec;
	}

	public GameVertex(IntPoint point) {
		vec = Converter.intPointToVector(point);
		pt = point;
	}

	public float x() {
		return vec.x;
	}

	public float y() {
		return vec.y;
	}

	public Vector2 getVector() {
		return vec;
	}

	public Vector3 getVector(float z) {
		return new Vector3(vec.x, vec.y, z);
	}

	public IntPoint getIntPoint() {
		return pt;
	}

	public float distance(GameVertex other) {
		return (this.vec - other.vec).magnitude;
	}

	public static Vector operator -(GameVertex v1, GameVertex v2) {
		return new Vector(v1.getVector() - v2.getVector());
	}

	public static GameVertex operator +(GameVertex v1, GameVertex v2) {
		return new GameVertex(v1.getVector() + v2.getVector());
	}

	public static GameVertex operator +(GameVertex v1, Vector v2) {
		return new GameVertex(v1.getVector(v2.z()) + v2.getValue());
	}

	public static GameVertex operator /(GameVertex v1, float v2) {
		return new GameVertex(v1.getVector() / v2);
	}

	public static bool operator ==(GameVertex v1, GameVertex v2) {
		if (ReferenceEquals(v1, v2)) {
			return true;
		}
		else if (ReferenceEquals(v1, null)) {
			return false;
		}
		else if (ReferenceEquals(v2, null)) {
			return false;
		}

		return v1.vec == v2.vec;
	}

	public static bool operator !=(GameVertex v1, GameVertex v2) {
		return ! (v1 == v2);
	}

	public override bool Equals(object obj) {
		GameVertex other = obj as GameVertex;

		if (other == null) {
			return false;
		}

		return other.vec == vec;
	}

	public override int GetHashCode() {
		return (int)(pt.X + pt.Y);
	}
}
