using UnityEngine;
using System;
using System.Collections;

public class Anchor : MonoBehaviour 
{
    public enum eAnchorTypes { Top, Bottom, Left, Right }

    [Serializable]
    public class ScreenAnchor
    {
        public eAnchorTypes AnchorType;
        public float Distance;
    }

    [Serializable]
    public class ObjectAnchor
    {
        public GameObject Object;
        public eAnchorTypes AnchorType;
        public float Distance;
    }

    public ScreenAnchor[] ScreenAnchors = new ScreenAnchor[0];
    //public ObjectAnchor[] ObjectAnchors = new ObjectAnchor[0];

    public void ApplyAnchors(Vector3 cameraPosition)
    {
        Util.AssertTrue(ScreenAnchors.Length > 0, "The gameobject '" + gameObject.name + "' has an 'Anchor' component but no anchors set in its configurations");
        
        float objWidth = gameObject.GetSize(true, true).x;
        float objHeight = gameObject.GetSize(true, true).y;
        ScreenAnchors.ForEach(screenAnchor =>
        {
            switch (screenAnchor.AnchorType)
            {
                case eAnchorTypes.Left:
                    gameObject.transform.position = new Vector3(cameraPosition.x - LayerManager.NormalizedScreenWidth/2 + objWidth / 2 + screenAnchor.Distance, transform.position.y, transform.position.z);
                    break;
                case eAnchorTypes.Right:
                    gameObject.transform.position = new Vector3(cameraPosition.x + LayerManager.NormalizedScreenWidth/2 - objWidth / 2 - screenAnchor.Distance, transform.position.y, transform.position.z);
                    break;
                case eAnchorTypes.Top:
                    gameObject.transform.position = new Vector3(transform.position.x, cameraPosition.y + LayerManager.NormalizedScreenHeight/2 - objHeight / 2 - screenAnchor.Distance, transform.position.z);
                    break;
                case eAnchorTypes.Bottom:
                    gameObject.transform.position = new Vector3(transform.position.x, cameraPosition.y - LayerManager.NormalizedScreenHeight/2 + objHeight / 2 + screenAnchor.Distance, transform.position.z);
                    break;
            }
        });

        /*ObjectAnchors.ForEach(objectAncor =>
        {
            Util.AssertTrue(objectAncor.Object != null, "The 'Object' field must be set for every Object Anchor");
            Vector3 targetObjPos = objectAncor.Object.transform.position;
            Vector3 targetObjSize = objectAncor.Object.GetSize(true, true);
            switch (objectAncor.AnchorType)
            {
                case eAnchorTypes.Left:
                    gameObject.transform.position = new Vector3(targetObjPos.x - targetObjSize.x / 2 - objWidth / 2 - objectAncor.Distance, transform.position.y, transform.position.z);
                    break;
                case eAnchorTypes.Right:
                    gameObject.transform.position = new Vector3(targetObjPos.x + targetObjSize.x / 2 + objWidth / 2 + objectAncor.Distance, transform.position.y, transform.position.z);
                    break;
                case eAnchorTypes.Top:
                    gameObject.transform.position = new Vector3(targetObjPos.x, targetObjPos.y + targetObjSize.y / 2 + objHeight / 2 + objectAncor.Distance, transform.position.z);
                    break;
                case eAnchorTypes.Bottom:
                    gameObject.transform.position = new Vector3(targetObjPos.x, targetObjPos.y - targetObjSize.y / 2 - objHeight / 2 - objectAncor.Distance, transform.position.z);
                    break;
            }
        });*/
    }
}
