using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using System;

public class MainCharacterManager : MonoBehaviour
{
    [SerializeField]
    public GameObject avatar;
    [SerializeField]
    public GameObject AvatarClone;

    public Vector3 startPosition;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void Awake()
    {
        try
        {
            avatar = GetMainCharacterObject();
            GameObject globalGUI = GameObject.FindGameObjectWithTag("GUI");

            if (AvatarClone == null)
            {
                startPosition = InitCharacterPosition();
                AvatarClone = GameObject.Instantiate(avatar, startPosition, Quaternion.identity);
            }
        }
        catch (UnityException e)
        {
            Debug.Log(e);
        }

        //DynamicPanel.transform.localPosition = uiPanelTemplate.transform.position;   // prevent overwritten position by... environment???
    }

    // Later here we will decide whether the player is loading a saved game, thus, a saved position
    // In general we have 3 cases -> saved game and save position, new position on the map, and position after using teleport/door

    private Vector3 InitCharacterPosition()
    {
        switch (OnMapAppearanceMethod.Gateway)
        {
            case OnMapAppearanceMethodEnum.Gateway:
            {
                if (SceneManager.GetActiveScene().name != PlayerPrefs.GetString("gateway.nextMapName") && PlayerPrefs.GetString("gateway.nextMapName") != "")
                {
                    throw new Exception("Gateway says we should be somewhere else: " + PlayerPrefs.GetString("gateway.nextMapName"));
                }

                var x = PlayerPrefs.GetFloat("gateway.respawnCoords.x");
                var y = PlayerPrefs.GetFloat("gateway.respawnCoords.y");
                var z = PlayerPrefs.GetFloat("gateway.respawnCoords.z");

                return new Vector3(x, y, z);
            }
            case OnMapAppearanceMethodEnum.LoadGame:
            {
                // TODO change for loading from a file instead of Prefabs

                //Debug.Log("Load Game");
                return PlayerPosition.LoadPosition();
            }
            default:
            {
                return DefaultPlayerMapPosition();
            }
        }
    }

    private Vector3 DefaultPlayerMapPosition()
    {
        switch (SceneManager.GetActiveScene().name)
        {
            case "CaveEntrance":
            {
                return new Vector3(0.5f, -37, 10);
            }
            case "WizardHouse":
            {
                return new Vector3(-5, 1.5f, 10);
            }
            default:
            {
                return avatar.transform.position;
            }
        }
    }

    private GameObject GetMainCharacterObject()
    {
        /*        Debug.Log("XDDDDD");
                string[] obects = AssetDatabase.FindAssets("t:Object", new[] { "Assets/Resources/" });

                foreach (string localObject in obects)
                {
                    Debug.Log(AssetDatabase.GUIDToAssetPath(localObject));
                }
                return (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Resources/ThePlayer.prefab", typeof(GameObject));*/

        return Resources.Load("ThePlayer") as GameObject;
    }
}