using UnityEngine;
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;

public enum ECompassDirection
{
    north = 0,
    northeast,
    east,
    southeast,
    south,
    southwest,
    west,
    northwest,
    max,
}

public class TM_Utilities : MonoBehaviour
{
    private static TM_Utilities _instance;

    private static TM_Utilities Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType<TM_Utilities>();

                if (_instance != null)
                {
                    return _instance;
                }

                GameObject go = new GameObject("TM_Utilities");
                _instance = go.AddComponent<TM_Utilities>();
            }

            return _instance;
        }
    }



    public static Vector2 Rotate2DVector(Vector2 rotateMe, float fAngleInDegrees)
    {
        Vector2 retVec = new Vector2();
        float rads = (fAngleInDegrees / 360.0f) * 6.28f;

        retVec.x = rotateMe.x * Mathf.Cos(rads) - rotateMe.y * Mathf.Sin(rads);
        retVec.y = rotateMe.x * Mathf.Sin(rads) + rotateMe.y * Mathf.Cos(rads);
        return retVec;
    }

    public static void PointSpriteInDirection(Vector2 vDirection, GameObject go)
    {
        Vector3 rotation = go.transform.rotation.eulerAngles;
        rotation.z = Vector2.Angle(Vector2.up, vDirection);
        if (vDirection.x > 0)
            rotation.z *= -1.0f;

        go.transform.rotation = Quaternion.Euler(rotation);
    }

}

    //Below: Code specific to Lich Defense

/*
    public static void CallSpellScript(string strScriptName, LDChanneledSpell spell, GameObject caster)
    {
        Type myType = Instance.GetType();
        MethodInfo script = myType.GetMethod(strScriptName);

        object[] scriptParams = new object[2];
        scriptParams[0] = spell;
        scriptParams[1] = caster;
        script.Invoke(Instance, scriptParams);
    }

    public static Vector3 KeepTransformInPlayfield(Vector3 vDesiredPosition)
    {
        Rect playfield = Camera.main.GetComponent<LDCamera>().rectPlayField;

        if (vDesiredPosition.x < playfield.xMin)
        {
            vDesiredPosition.x = playfield.xMin + 0.05f;
        } 
        if (vDesiredPosition.x > playfield.xMax)
        {
            vDesiredPosition.x = playfield.xMax - 0.05f;
        }
        if (vDesiredPosition.y < playfield.yMin)
        {
            vDesiredPosition.y = playfield.yMin + 0.05f;
        }
        if (vDesiredPosition.y > playfield.yMax)
        {
            vDesiredPosition.y = playfield.yMax - 0.05f;
        }

        return vDesiredPosition;
    }
    
    public static void Script_TestChanneledSpell(LDChanneledSpell spell, GameObject caster)
    {
        
    }

    public static void Script_LaunchShockOrbBarrage(LDChanneledSpell spell, GameObject caster)
    {
        Debug.Log("Caster: " + caster.name + " is launching Shock Orbs!");

        //create shock orbs and send them at the player
        Vector3 vGoalPosition = GameManager.playerObject.transform.position;

        int iNumOrbs = 4; //perhaps change based on rank?

        for (int t = 0; t < iNumOrbs; t++)
        {
            GameObject orb = Instantiate(Resources.Load<GameObject>("Prefabs/projectile_enemy_shock_orb"));
            TM_SproggiwoodCoinMotionComponent sproggiMotion = orb.GetComponent<TM_SproggiwoodCoinMotionComponent>();
            float fLifeTime = UnityEngine.Random.Range(1.5f, 3f);
            sproggiMotion.Launch( caster.transform.position, vGoalPosition, UnityEngine.Random.insideUnitCircle, UnityEngine.Random.Range(10f,14f), fLifeTime);
            TM_Projectile proj = orb.GetComponent<TM_Projectile>();

            //breathing room to allow for an impact particle when the orb hits the end of the trail
            proj.fDestroyAfter = fLifeTime + 0.1f;

            orb.transform.position = caster.transform.position;
        }
    }

    public static void Script_DragonBreath(LDChanneledSpell spell, GameObject caster)
    {
        LDCreatureAIDragon dragon = caster.GetComponent<LDCreatureAIDragon>();
        dragon.StartBreathWeapon();
    }

    public static string HighlightThreatText(string str)
    {
        return "<color=#ff7800>" + str + "</color>";
    }

    public static string HighlightHypeText(string str)
    {
        return "<color=#00ffff>" + str + "</color>";
    }

    public static string HighlightSoulsText(string str)
    {
        return "<color=#77D593>" + str + "</color>";
    }

    public static string GetWaveIntroTextForWave(int iWaveNum)
    {
        switch( iWaveNum )
        {
            case 1:
                return "Dispatch these simple minded ale-swilling Fighter types.";
            case 2:
                return "Keep an eye on bow-wielding Rangers who hang in the back.";
            case 3:
                return "Mages command the elements, don't let them finish casting their Shock Orb swarms.";
            case 4:
                return "Filthy sneaking Rogues will attempt to dash in, steal the goods, and leave! Don't let them.";
        }

        if (iWaveNum%5 == 0)
        {
            return "Your dark legend grows! More heroes march forth to be slaughtered.";
        }

        return "";
    }

    public static string GetWaveOutroTextForWave(int iWaveNum)
    {
        switch( iWaveNum )
        {
            case 1:
                return "Now to use the souls you drained...";
            case 2:
                return "Cowards hiding behind archery. Feh!";
            case 3:
                return "Hedge wizards, all of them. None control the arcane as you do!";
            case 4:
                return "Death is too good for those mewling shadow-hugging slimes.";
        }

        return "";
    }
    
}
*/
