﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DiamondGrid : MonoBehaviour {

    public OctagonGrid octagonGrid;

    public Texture texture;

    public float floorLevel;

    private float diamondRadius;

    public void Initialize() {
    }

    public GameObject SpawnDiamond(int x, int z) {

        diamondRadius = this.octagonGrid.GetRadius() * (3.0f / 5.0f);

        GameObject diamond = new GameObject("Diamond , " + x + " , " + z);

        Vector3[] newVertices;
        Vector2[] newUV;
        int[] newTriangles;

        //positions vertices of the hexagon to make a normal hexagon
        newVertices = new Vector3[] {
			/*0*/new Vector3(0.0f * this.diamondRadius, this.floorLevel, 0.5f * this.diamondRadius),
			/*1*/new Vector3(0.5f * this.diamondRadius, this.floorLevel, 1.0f * this.diamondRadius),
			/*2*/new Vector3(1.0f * this.diamondRadius, this.floorLevel, 0.5f * this.diamondRadius),
			/*3*/new Vector3(0.5f * this.diamondRadius, this.floorLevel, 0.0f * this.diamondRadius),
		};

        // Triangles connecting the verts.
        newTriangles = new int[] {
			0, 1, 2,
            3, 0, 2,
		};

        //uv mappping
        newUV = new Vector2[] {
			/*0*/new Vector3(0.0f, 0.5f),
			/*1*/new Vector3(0.5f, 1.0f),
			/*2*/new Vector3(1.0f, 0.5f),
			/*3*/new Vector3(0.5f, 0.0f),
		};

        // Create the mesh, make it render, provide collisions.
        diamond.AddComponent<MeshFilter>();
        diamond.AddComponent<MeshRenderer>();
        Mesh octagonMesh = new Mesh();
        octagonMesh.vertices = newVertices;
        octagonMesh.triangles = newTriangles;
        octagonMesh.uv = newUV;
        octagonMesh.RecalculateNormals();
        diamond.GetComponent<MeshFilter>().mesh = octagonMesh;
        MeshCollider colliderMesh = diamond.AddComponent(typeof(MeshCollider)) as MeshCollider;
        colliderMesh.sharedMesh = octagonMesh;
        colliderMesh.isTrigger = true;
        diamond.renderer.enabled = true;
        diamond.renderer.material.mainTexture = texture;

        // Shift the octagon from default position to where it should be.
        Vector3 shift = new Vector3(0, 0, 0);
        diamond.transform.position = shift;
        shift.x = this.octagonGrid.GetRadius() * (float)(x) - this.diamondRadius / 2;
        shift.z = this.octagonGrid.GetRadius() * (float)(z) - this.diamondRadius / 2;
        diamond.transform.position += shift;
        diamond.transform.parent = GameObject.Find("Diamond Grid").transform;

        return diamond;
    }

    public float GetXMiddleOfGridPosition(int x, int y) {
        return x * this.diamondRadius + this.diamondRadius / 2.0f;
    }

    public float GetYMiddleOfGridPosition(int x, int y) {
        return this.floorLevel;
    }

    public float GetZMiddleOfGridPosition(int x, int y) {
        return y * this.diamondRadius + this.diamondRadius / 2.0f;
    }

    public void CameraIsHitting(string s) {
        // Debug.Log(s);
    }

    // Update is called once per frame
    void Update() {
    }

}
