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

namespace Onward.CustomContent.Assets
{
    [ExecuteInEditMode]
    public class GM_Base : Entity
    {
        public enum EGameModeType
        {
            Null = 0,

            Uplink = 1,
            Escort = 2,
            // Tutorial = 3,
            None = 4,
            SpecOps = 5,
            Assault = 6,
            GunGame = 7,
            OneInTheChamber = 8,

            Operations_Hunt = 10,
            Operations_Evac = 11,
            // ShootingRange = 12
        }

        public enum EAISupportedGameModeType
        {
            Null = 0,

            //Uplink = 1,
            //Escort = 2,
            // Tutorial = 3,
            //None = 4,
            //SpecOps = 5,
            //Assault = 6,
            //GunGame = 7,
            //OneInTheChamber = 8,

            Operations_Hunt = 10,
            Operations_Evac = 11,
            // ShootingRange = 12
        }

        [System.NonSerialized]
        public EGameModeType GameModeType = EGameModeType.Null;

        public GM_Base(EGameModeType gameMode)
        {
            GameModeType = gameMode;
        }

        public override void Validate()
        {
            if (GameModeType == EGameModeType.Null)
                throw new ContentExceptionWithGameObject(gameObject, "Internal error: game mode is null.");

            ValidateAmmoBoxCount();
        }

        public void ValidateObjectiveEntries<T>(ref T[] arr) where T : UnityEngine.Object
        {
#if UNITY_EDITOR
            List<T> newObjs = new List<T>(arr.Length);

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

                    continue;
                }

                newObjs.Add(arr[i]);
            }

            arr = newObjs.ToArray();

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

        void ValidateAmmoBoxCount()
        {
            if (GameModeType == EGameModeType.Operations_Evac || GameModeType == EGameModeType.Operations_Hunt || GameModeType == EGameModeType.SpecOps)
            {
                const int MinAmmoBoxCount = 4;

                if (GameObject.FindObjectsOfType<AmmoBox>().Length < MinAmmoBoxCount)
                {
                    throw new ContentExceptionWithGameObject(gameObject, $"Minimum of {MinAmmoBoxCount} ammoboxes required.");
                }
            }
        }
    }
}