using System.Collections.Generic;
using System.Drawing;
using UnityEngine;

class REPL
{
    public static Transform footCenter;

    public static void StartREPL()
    {
        //return;

        AddHeightBox();
        AddCamera();
        AddRigidbodyDebug();
        AddCubes();
        AddFootCenter();
    }

    public static void AddCamera()
	{
        GameObject camera = GameObject.Find("WanderCamera");
        if (camera != null)
        {
            GameObject.DestroyImmediate(camera);
        }

        camera = new GameObject();
        camera.name = "WanderCamera";
        camera.AddComponent<Camera>();
        camera.AddComponent<SimpleMoveCamera>();

        GameObject wanderCam = GameObject.Find("WanderCamera");

        GameObject player = GameObject.Find("Player");
        if (player != null)
        {
            wanderCam.transform.position = player.transform.position;
        }

        wanderCam.transform.position = new Vector3(1.21f, 33.92f, -10.4f);
        wanderCam.transform.rotation = Quaternion.Euler(25.95f, 75, 0);
    }

    public static void AddRigidbodyDebug()
    {
        string[] targets = {
            "Head",
            "Neck",
            "Torso",
            "Arm_Right",
            "Arm_Left",
            "Hand_Right",
            "Hand_Left",
            "Hip",
            "Leg_Left",
            "Leg_Right",
            "Foot_Left",
            "Foot_Right",
            "Knee_Left",
            "Knee_Right",
        };

        for(int i = 0; i < targets.Length; ++i)
        {
            string target = targets[i];
            GameObject go = GameObject.Find(target);
            if(go != null)
            {
                var comps = go.gameObject.GetComponents<MonoBehaviour>();
                if (comps != null)
                {
                    for (int j = 0; j < comps.Length; j++)
                    {
                        if (comps[j].GetType().Name == "DebugRigidBody" || comps[j].GetType().Name == "DebugRigidBody2")
                        {
                            UnityEngine.Object.DestroyImmediate(comps[j]);
                        }
                    }
                }
                Debug.Log(go.name);
                var drb = go.AddComponent<DebugRigidBody>();
                //drb.mode = DebugRigidBody.EMode.Cube;
                //drb.color = Color.green;
            }
        }
    }

    public struct CubeParams
    {
        public UnityEngine.Color color;
        public float size;
        public DebugRigidBody.EMode mode;
    }

    public static void AddCubes()
    {
        Dictionary<string, CubeParams> targets = new Dictionary<string, CubeParams>{
            {"CameraPos",  new CubeParams(){ color = UnityEngine.Color.red, size = 0.2f, mode = DebugRigidBody.EMode.Cube } },
            {"Main Camera",  new CubeParams(){ color = UnityEngine.Color.white, size = 0.05f, mode = DebugRigidBody.EMode.Axis_Cube } },
            {"RotationTarget",  new CubeParams(){ color = UnityEngine.Color.green, size = 0.05f, mode = DebugRigidBody.EMode.Axis_Cube } },
            //{"AvaragePosition",  new CubeParams(){ color = UnityEngine.Color.blue, size = 0.2f, mode = DebugRigidBody.EMode.Axis_Cube } },
        };

        foreach(var target in targets)
        {
            string name = target.Key;
            GameObject go = GameObject.Find(name);
            if (go != null)
            {
                var comps = go.gameObject.GetComponents<MonoBehaviour>();
                if (comps != null)
                {
                    for (int j = 0; j < comps.Length; j++)
                    {
                        if (comps[j].GetType().Name == "DebugRigidBody" || comps[j].GetType().Name == "DebugRigidBody2")
                        {
                            UnityEngine.Object.DestroyImmediate(comps[j]);
                        }
                    }
                }
                Debug.Log(go.name);
                var drb = go.AddComponent<DebugRigidBody>();
                drb.mode = target.Value.mode;
                drb.color = target.Value.color;
                drb.cubeLen = target.Value.size;
            }
        }
    }

    public static void AddHeightBox()
    {
        //float height = 2;
        //GameObject cube = new GameObject();
        //cube.transform.position = new Vector3(4.39f, 31.397f + height / 2.0f, -9.96f);
        //var drb = cube.AddComponent<DebugRigidBody>();
        //drb.mode = DebugRigidBody.EMode.Cube;
        //drb.color = UnityEngine.Color.red;
        //drb.cubeLen = height;
    }

    public static void AddFootCenter()
    {
        float height = 2;
        GameObject cube = new GameObject();
        footCenter = cube.transform;
        cube.transform.position = new Vector3(4.39f, 31.397f + height / 2.0f, -9.96f);
        var drb = cube.AddComponent<DebugRigidBody>();
        drb.mode = DebugRigidBody.EMode.Custom;
        drb.color = UnityEngine.Color.white;
        drb.cubeLen = 0.1f;
        drb.customDraw = () => {
            //Draw.Sphere(drb.transform.position, 0.05f);
        };
    }

}