using UnityEngine;
using System.Collections;

public class LoadPlayer : MonoBehaviour {

    // Attributes
    private GameObject p;
    private GameObject p2;
    
    private Rigidbody rigid;
    private PlayerMovement movement;
    private AnimationPlayer animation;
    private Animator animator;
    public PlayerStats stats;

    public Player player;
    private BoxCollider collider;
    private GameObject body;


    // Use this for initialization
    void Awake() {
        Debug.Log("AWAKE LoadPlayer.cs");

        this.p = GameObject.Find("PlayerBody");

        this.player = this.p.AddComponent<Player>();
        this.player.setBody(this.rigid);
        this.player.playerMovement = this.movement;

        if (LoadSavePlayerStats.tmp != null) {
            Debug.Log("Player is not null");

            this.stats = LoadSavePlayerStats.tmp;

            if (LoadSavePlayerStats.isLoaded) {
                Debug.Log("Player is loaded from a savedata");
                this.p.transform.position = new Vector3(this.stats.posx, this.stats.posy, this.stats.posz) ;
            } else {
                Debug.Log("Player is not loaded");
                Debug.Log(this.stats.ToString());
                this.p.transform.position = ChangingPlane.getPos(Application.loadedLevelName, this.stats.auxExit);
            }

        } else {
            Debug.Log("Player is null");
            this.stats = new PlayerStats();
            this.p.transform.position = ChangingPlane.getInitPos();



        }

        this.rigid = this.p.AddComponent<Rigidbody>();
        this.rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
        this.rigid.drag = 7.0f;

        this.movement = this.p.AddComponent<PlayerMovement>();
        this.movement.auxExit = stats.auxExit;

        this.collider = this.p.AddComponent<BoxCollider>();
        this.collider.size = new Vector3(2.4f, 6.0f, 2.4f);
        this.collider.center = new Vector3(0, -2.8f, 0);

        this.animation = this.p.AddComponent<AnimationPlayer>();

        this.body = (GameObject)Instantiate(Resources.Load("PlayerBodyParts/player_v4"), Vector3.zero, Quaternion.identity);
        this.body.name = "PlayerMesh";
        this.body.transform.parent = this.p.transform;
        this.body.transform.localPosition = new Vector3(0, 0, 0);
        this.body.tag = "Player";

        this.movement.setAnimationPlayer(this.animation);

        this.player.setPlayerStats(this.stats);

        if (LoadSavePlayerStats.tmp == null)  {
            this.player.receiveItem(1);
            this.player.equipItem(1);
        } else {
            this.equipItems();
        }
        
        Invoke("resetLoadState", 6);
    }

    public AnimationPlayer getAnimationPlayer() { return this.animation; }

    void resetLoadState() { LoadSavePlayerStats.isLoaded = false; }

    
    
    private void equipItems()   {
        if (this.stats.getInventory().weaponE != null) this.player.equipItem(this.stats.getInventory().weaponE.id);
        if (this.stats.getInventory().glovesE != null) this.player.equipItem(this.stats.getInventory().glovesE.id);
        if (this.stats.getInventory().armorE != null) this.player.equipItem(this.stats.getInventory().armorE.id);
        if (this.stats.getInventory().bootsE != null) this.player.equipItem(this.stats.getInventory().bootsE.id);
        if (this.stats.getInventory().helmetE != null) this.player.equipItem(this.stats.getInventory().helmetE.id);
    }
}
