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

namespace Onward.CustomContent.Assets
{
    public class Objective : Entity
    {
        [Header("Defending Team")]
        public SpawnSystem[] DefendingTeamSpawns;

        [Header("Attacking Team")]
        public SpawnSystem[] AttackingTeamSpawns;

        public override void Validate()
        {
            base.Validate();

            ValidateSpawnSystemEntries(ref DefendingTeamSpawns, gameObject);
            ValidateSpawnSystemEntries(ref AttackingTeamSpawns, gameObject);

            if (DefendingTeamSpawns.Length <= 0)
                throw new ContentExceptionWithGameObject(gameObject, "No spawns for defending team.");

            if (AttackingTeamSpawns.Length <= 0)
                throw new ContentExceptionWithGameObject(gameObject, "No spawns for attacking team.");
        }

        public static void ValidateSpawnSystemEntries(ref SpawnSystem[] arr, GameObject go)
        {
#if UNITY_EDITOR
            List<SpawnSystem> newSpawnSystem = new List<SpawnSystem>(arr.Length);

            for (int i = arr.Length - 1; i >= 0; --i)
            {
                if (arr[i] == null)
                {
                    Debug.LogError("Invalid SpawnSystem entry detected and removed. This error is not fatal.", go);

                    continue;
                }

                newSpawnSystem.Add(arr[i]);
            }

            arr = newSpawnSystem.ToArray();

            UnityEditor.EditorUtility.SetDirty(go);
#endif
        }

        protected static void DrawUplinkGizmo(Transform transform)
        {
            Util.RenderDownwardsLine(transform.position);

            // Draw spheres
            Gizmos.color = Color.blue;

            Gizmos.DrawWireSphere(transform.position, 1f);

            Gizmos.color = Color.green;

            Gizmos.DrawWireSphere(transform.position, 5f);

            // Draw "mesh"
            Gizmos.color = Color.green;

            Matrix4x4 oldMatrix = Gizmos.matrix;
            {
                Gizmos.matrix = transform.localToWorldMatrix;

                // Base
                Gizmos.DrawCube(
                    new Vector3(0f, 0.152f, -0.0008335114f),
                    new Vector3(1.227543f, 0.2629822f, 1.228493f));

                // Base extend 1
                Gizmos.DrawCube(
                    new Vector3(0f, 0.4182f, 0f),
                    new Vector3(0.5872279f, 0.7289984f, 0.5897084f));

                // Base extend 2
                Gizmos.DrawCube(
                    new Vector3(0f, 0.8593f, 0.026f),
                    new Vector3(0.35f, 0.3326135f, 0.35f));

                // Dish
                Gizmos.matrix = transform.localToWorldMatrix * Matrix4x4.TRS(new Vector3(0.0126f, 1.5318f, -0.1233f), Quaternion.Euler(-20.041f, 0f, 0f), new Vector3(1.181746f, 1.138005f, 0.2916182f));
                Gizmos.DrawCube(Vector3.zero, Vector3.one);

                // Dish arm
                Gizmos.matrix = transform.localToWorldMatrix * Matrix4x4.TRS(new Vector3(0f, 1.183f, -0.417f), Quaternion.Euler(36.434f, 0f, 0f), new Vector3(0.166395f, 0.06171389f, 1.465596f));
                Gizmos.DrawCube(Vector3.zero, Vector3.one);

                // Dish
                Gizmos.matrix = transform.localToWorldMatrix * Matrix4x4.TRS(new Vector3(0f, 1.7518f, -0.8859f), Quaternion.Euler(-16.729f, 0f, 0f), new Vector3(0.3293305f, 0.3199438f, 0.2190256f));
                Gizmos.DrawCube(Vector3.zero, Vector3.one);
            }
            Gizmos.matrix = oldMatrix;
        }

#if UNITY_EDITOR
        private void OnDrawGizmosSelected()
        {
            Gizmos.color = new Color(0, 1, 0, 0.25f);

            if (DefendingTeamSpawns != null)
            {
                foreach (var spawnSys in DefendingTeamSpawns)
                {
                    spawnSys.DrawLinesTo(transform.position);
                }
            }

            if (AttackingTeamSpawns != null)
            {
                foreach (var spawnSys in AttackingTeamSpawns)
                {
                    spawnSys.DrawLinesTo(transform.position);
                }
            }
        }
#endif
    }
}