using UnityEngine;
using System.Collections;
//using Mono.Security.Cryptography;
using TMPro;

public class TreasureLocationComponent : MonoBehaviour 
{

    SpriteRenderer myPointingArrow;
    public GameObject Prefab_BreakableHex;
    public TextMeshPro tmpDistance;

    private Vector3 vBestArrowLocation;
    private GameManager theGame;

    private int iNumHexesUnbroken = 0;
    void Awake()
    {
        myPointingArrow = GetComponentInChildren<SpriteRenderer>();
        tmpDistance = GetComponentInChildren<TextMeshPro>();
        theGame = GameObject.Find("GameManager").GetComponent<GameManager>();
    }

	// Use this for initialization
	void Start ()
    {
	    PlaceTreasureHexes();
        Debug.Log(tmpDistance.ToString());
	    tmpDistance.GetComponent<MeshRenderer>().sortingLayerName = "UI";
    }

    void PlaceTreasureHexes()
    {
        Vector3 vPlacePos = transform.position;
        int iHexesToPlace = 5;
        int iPlaced = 0;
        
        vBestArrowLocation = vPlacePos;
        vBestArrowLocation.y += 1.0f;

        while (iPlaced < iHexesToPlace)
        {
            GameObject newHex = Instantiate(Prefab_BreakableHex);
            newHex.transform.position = vPlacePos;

            if (Random.value < 0.3f)
            {
                vPlacePos.x -= 0.75f;
                vPlacePos.y -= 1.25f;
                vBestArrowLocation.x -= 0.375f;
            }
            else
            {
                vPlacePos.x += 1.5f;
                vBestArrowLocation.x += 0.75f;

            }

            BreakableTreasureHex bth = newHex.GetComponent<BreakableTreasureHex>();
            bth.myTLC = this;

            iPlaced++;
        }

        iNumHexesUnbroken = iPlaced;

        //now place enemies!
        theGame.SpawnBaddiesNearTreasure(vBestArrowLocation);
    }

	// Update is called once per frame
	void Update ()
    {
	    //if i'm near the camera, draw the sprite over myself.
        //if not, draw it on screen, pointing at me.

        Vector3 vCameraToMe = transform.position - Camera.main.transform.position;
        vCameraToMe.z = 0;

        float fScreenWidthNotReally = Camera.main.orthographicSize;

        //out of range
        if( vCameraToMe.sqrMagnitude > fScreenWidthNotReally * fScreenWidthNotReally )
        {
            Vector3 vNewPos = Camera.main.transform.position + (vCameraToMe.normalized) * (fScreenWidthNotReally) * 0.8f;
            Vector3 vTextPos = Camera.main.transform.position + (vCameraToMe.normalized) * (fScreenWidthNotReally) * 0.68f;
            vNewPos.z = -5;
            vTextPos.z = -5;

            myPointingArrow.transform.position = vNewPos;

            float fDesiredZ = Vector2.Angle(Vector2.up,(Vector2)vCameraToMe);

            if (vCameraToMe.x > 0)
                fDesiredZ = 360.0f - fDesiredZ;

            myPointingArrow.transform.rotation = Quaternion.Euler(0, 0, fDesiredZ);

            tmpDistance.transform.position = vTextPos;
            tmpDistance.color = Color.white;
            tmpDistance.text = vCameraToMe.magnitude.ToString("0.00");
        }
        else
        {
            //right above me, pointing down
            Vector3 vNewPos = vBestArrowLocation;
            vNewPos.z = -5f;
            //vNewPos.y -= 1.5f;
            vNewPos.y += Mathf.Sin(Time.time * 6.28f) * 0.7f;

            myPointingArrow.transform.position = vNewPos;
            myPointingArrow.transform.rotation = Quaternion.Euler(0, 0, 180);
            tmpDistance.transform.position = vNewPos + new Vector3(0, 1, 0);
            tmpDistance.color = new Color(0.75f,0.2f,0.2f);

            if (theGame.thePlayer.currentForm == EShipForm.square)
            {
                tmpDistance.text = "SHOOT";
            }
            else
            {
                tmpDistance.text = "TANK";
            }
        }

	}

    public void ReportBrokenHex()
    {
        iNumHexesUnbroken--;
        if (iNumHexesUnbroken <= 0)
        {
            theGame.RemoveTreasureLocation(this);
            Destroy(gameObject);
        }
    }
}
