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

public class Player : MonoBehaviour
{
    public List<GameObject> dummies;
    public Vegetable heldVeggie;
    public GameObject playerModel;

    protected Map map;
    private Animator playerAnim;
    private List<Animator> dummieAnims = new List<Animator>();

    public float scale = 0.1f;

    public void Start()
    {
        playerAnim = playerModel.GetComponent<Animator>();
    }

    protected virtual void Update()
    {
        UpdateDummies();
    }

    public void CreateDummies(Map map)
    {
        this.map = map;

        dummies.Add(Instantiate(playerModel, new Vector3(transform.position.x - map.TileSize * map.MapDimensions.x, transform.position.y, transform.position.z), transform.rotation, transform).gameObject);
        dummies.Add(Instantiate(playerModel, new Vector3(transform.position.x + map.TileSize * map.MapDimensions.x, transform.position.y, transform.position.z), transform.rotation, transform).gameObject);
        dummies.Add(Instantiate(playerModel, new Vector3(transform.position.x, transform.position.y, transform.position.z - map.TileSize * map.MapDimensions.y), transform.rotation, transform).gameObject);
        dummies.Add(Instantiate(playerModel, new Vector3(transform.position.x, transform.position.y, transform.position.z + map.TileSize * map.MapDimensions.y), transform.rotation, transform).gameObject);

        foreach (GameObject dummie in dummies)
        {
            
            Debug.Log(dummie.name);
            dummieAnims.Add(dummie.GetComponentInChildren<Animator>());


            foreach (Collider col in dummie.GetComponentsInChildren<Collider>())
            {
                col.enabled = false;
            }
        }
    }

    public void UpdateDummies()
    {
        dummies[0].transform.position = transform.position - (Vector3.right * (map.TileSize * map.MapDimensions.x * scale));
        dummies[1].transform.position = transform.position + (Vector3.right * (map.TileSize * map.MapDimensions.x * scale));
        dummies[2].transform.position = transform.position - (Vector3.forward * (map.TileSize * map.MapDimensions.y * scale));
        dummies[3].transform.position = transform.position + (Vector3.forward * (map.TileSize * map.MapDimensions.y * scale));

        int count = 0;
        foreach (GameObject dummie in dummies)
        {
            dummie.transform.rotation = playerModel.transform.rotation;
            Debug.Log(dummieAnims[count]);
            dummieAnims[count].SetFloat("WalkSpeed", playerAnim.GetFloat("WalkSpeed"));

            if (playerAnim.GetBool("PickUp"))
                dummieAnims[count].SetTrigger("PickUp");
        }
    }
}
