//Created by Neodrop
//neodrop@unity3d.ru
using UnityEngine;
//#pragma warning disable

public class AI_RangeMarker : MonoBehaviour
{

    public enum Axis
    {
        X, Y, Z
    }

    public AI_Range holder;
    public Color color, colorBack;
    public float angleView = 360f, radius;
    public Transform parentTransform;
    public Axis parentTransformLookAtAxis = Axis.Z;
    public string zoneName;

    public float runtimeDistance;
    public Transform anchorLeft, anchorRight, anchorTarget;
    private Transform _target;

    void Start()
    {
        colorBack = color;
        CreateAnchor();
    }

    void CreateAnchor()
    {
        anchorLeft = new GameObject("Anchore Left_" + zoneName).transform;
        anchorRight = new GameObject("Anchore Right_" + zoneName).transform;
        anchorLeft.hideFlags = anchorRight.hideFlags = HideFlags.HideInHierarchy;

        anchorTarget = new GameObject("Anchor Target").transform;
        anchorTarget.gameObject.hideFlags = HideFlags.HideInHierarchy;
        anchorLeft.position = anchorRight.position = anchorTarget.position = holder.transform.position;

        if (parentTransform)
        {
            _target = new GameObject("_parent").transform;
            _target.position = parentTransform.position;
            runtimeDistance = Vector3.Distance(transform.position, parentTransform.position);
            switch (parentTransformLookAtAxis)
            {
                case Axis.X:
                    _target.Translate(new Vector3(runtimeDistance, 0, 0), parentTransform);
                    break;
                case Axis.Y:
                    _target.Translate(new Vector3(0, runtimeDistance, 0), parentTransform);
                    break;
                case Axis.Z:
                    _target.Translate(new Vector3(0, 0, runtimeDistance), parentTransform);
                    break;
            }
            _target.hideFlags = HideFlags.HideAndDontSave;
            _target.parent = parentTransform;
        }
        else runtimeDistance = Vector3.Distance(transform.position, transform.parent.position);
    }

    private bool _lastUpdate;
    void LateUpdate()
    {
        if (_lastUpdate) UpdatePosition();
        _lastUpdate = !_lastUpdate;
    }

    void UpdatePosition()
    {
        anchorTarget.position = anchorLeft.position = anchorRight.position = holder.transform.position;
        if (parentTransform == null)
        {
            anchorLeft.rotation = anchorRight.rotation = holder.transform.rotation;
            anchorLeft.Rotate(0, -angleView * .5f, 0);
            anchorRight.Rotate(0, angleView * .5f, 0);
            return;
        }

        transform.position = holder.transform.position;
        transform.LookAt(new Vector3(_target.position.x, transform.position.y, _target.position.z));
        anchorLeft.rotation = anchorRight.rotation = transform.rotation;

        transform.parent = null;
        transform.Translate(new Vector3(0, 0, runtimeDistance), Space.Self);
        transform.parent = holder.transform;

        anchorLeft.Rotate(0, -angleView * .5f, 0);
        anchorRight.Rotate(0, angleView * .5f, 0);
    }

    #region
    //void OnDrawGizmos()
    //{
    //    if (holder == null)
    //    {
    //        if (!Application.isEditor) Destroy(gameObject);
    //        else DestroyImmediate(gameObject);
    //        return;
    //    }
    //    if (!holder.showRings) return;
    //    if (Application.isPlaying) UpdatePosition();
    //    Gizmos.color = color;
    //    Gizmos.DrawSphere(transform.position, .075f);
    //    Gizmos.DrawLine(holder.transform.position, transform.position);
    //    DrawRing();
    //    Gizmos.color = Color.white;
    //}
    #endregion

    void DrawRing()
    {
        if (Application.isPlaying) UpdatePosition();
        float oneSide = angleView * .1f;
        Transform conductor = new GameObject("cond").transform;
        Transform conductorAnchor = new GameObject("condAnchor").transform;
        conductor.position = transform.position;
        conductorAnchor.position = holder.transform.position;
        conductorAnchor.LookAt(conductor);
        conductor.parent = conductorAnchor;
        Quaternion startRotation = conductorAnchor.rotation;

        for (int i = 0; i < oneSide; i++)
        {
            Vector3 lastPos = conductor.position;
            conductorAnchor.Rotate(Vector3.up, 5f);
            Gizmos.DrawLine(lastPos, conductor.position);
        }
        Gizmos.DrawLine(conductorAnchor.position, conductor.position);
        conductorAnchor.rotation = startRotation;

        for (int i = 0; i < oneSide; i++)
        {
            Vector3 lastPos = conductor.position;
            conductorAnchor.Rotate(Vector3.up, -5f);
            Gizmos.DrawLine(lastPos, conductor.position);
        }
        Gizmos.DrawLine(conductorAnchor.position, conductor.position);
        DestroyImmediate(conductorAnchor);
    }

    void OnDrawGizmosSelected()
    {
        if (holder == null)
        {
            if (!Application.isEditor) Destroy(gameObject);
            else DestroyImmediate(gameObject);
            return;
        }
        if (!holder.showRings) return;
        Gizmos.color = color;
        Gizmos.DrawSphere(transform.position, .075f);
        Gizmos.DrawWireSphere(transform.position, .1f);
        Gizmos.DrawLine(holder.transform.position, transform.position);
        DrawRing();
        Gizmos.color = Color.white;
    }

}
