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

public class Crafting : MonoBehaviour {

    List<GameObject> craftingItems = new List<GameObject>();
    public GameObject[] craftables;
    public string[] itemValues;

    float time;
    float step = 2;

    GameObject thing;

    int craftedItems = 0;

    private void Start() {
        itemValues = new string[2];
    }

    // Update is called once per frame
    void Update() {
        if (craftingItems.Count > 1) {
            foreach (GameObject g in craftingItems) {
                g.transform.parent = transform;
                g.transform.localPosition = Vector3.Lerp(g.transform.localPosition, Vector3.zero, 0.1f);
            }
            time += Time.deltaTime;
            if (time > step) {
                Craft();
                foreach (GameObject g in craftingItems) {
                    Destroy(g);
                }
                craftingItems.Clear();
                time = 0;
            }
        }

    }

    void Craft() {

        for (int i = 0; i < 2; i++) {
            switch (craftingItems[i].GetComponent<Item>().itemName) {
                case Item.ItemName.COCONUT:
                    itemValues[i] = "coconut"; ;
                    break;
                case Item.ItemName.WOOD:
                    itemValues[i] = "wood";
                    break;
                case Item.ItemName.LEAVES:
                    itemValues[i] = "leaves";
                    break;
                case Item.ItemName.SAND:
                    itemValues[i] = "sand";
                    break;
                case Item.ItemName.SHELL:
                    itemValues[i] = "shell";
                    break;
                case Item.ItemName.ROCKS:
                    itemValues[i] = "rocks";
                    break;
            }
        }

        //Toilet
        if ((itemValues[0] == "sand" && itemValues[1] == "shell") || (itemValues[1] == "sand" && itemValues[0] == "shell")) {
            MakeThing(0);
            craftedItems++;
        }

        //TP
        if ((itemValues[0] == "coconut" && itemValues[1] == "wood") || (itemValues[1] == "coconut" && itemValues[0] == "wood")) {
            MakeThing(1);
            craftedItems++;
        }

        //Rug
        if ((itemValues[0] == "coconut" && itemValues[1] == "leaves") || (itemValues[1] == "coconut" && itemValues[0] == "leaves")) {
            MakeThing(2);
            craftedItems++;
        }

        //Outhouse
        if ((itemValues[0] == "wood" && itemValues[1] == "shell") || (itemValues[1] == "wood" && itemValues[0] == "shell")) {
            if (craftedItems > 2) {
                MakeThing(3);
                GameObject.Find("obj_Player").GetComponent<PlayerController>().gameOver = true;
                GameObject.Find("obj_Player").GetComponent<AudioSource>().PlayOneShot(GameObject.Find("obj_Player").GetComponent<Sounds>().sounds[1]);
            }
        }
    }

    void MakeThing(int t) {
        thing = Instantiate(craftables[t]);
    }

    private void OnTriggerEnter(Collider other) {
        if (other.CompareTag("Item")) {
            craftingItems.Add(other.gameObject);
            GameObject.Find("obj_Player").GetComponent<AudioSource>().PlayOneShot(GameObject.Find("obj_Player").GetComponent<Sounds>().sounds[3]);
        }
    }

    private void OnTriggerExit(Collider other) {
        if (other.CompareTag("Item")) {
            craftingItems.RemoveAt(craftingItems.Count-1);
        }
    }
}
