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

public class Tile : MonoBehaviour
{
    [SerializeField] private Color colorOne, colorTwo, colorThree;
    [SerializeField] private SpriteRenderer spriteRenderer;
    [SerializeField] GameObject highlight;
    [SerializeField] GameObject UIManager;
    [SerializeField] GameObject LevelManager;
    [SerializeField] GameObject audioManager;

    [SerializeField] GameObject resourceManager;
    private int scrapAmount;
    private int waterAmount;
    private int soapAmount;

    [Header("Scrap")]
    [SerializeField] GameObject scrap;
    [SerializeField] public int scrapScrapCost;

    [Header("Water")]
    [SerializeField] GameObject water;
    [SerializeField] public int waterScrapCost;

    [Header("Soap")]
    [SerializeField] GameObject soap;
    [SerializeField] public int soapScrapCost;

    [Header("Machine")]
    [SerializeField] GameObject machine;
    [SerializeField] public int machineScrapCost;

    [Header("Barrage")]
    [SerializeField] GameObject barrage;
    [SerializeField] public int barrageScrapCost;
    [SerializeField] public int barrageWaterCost;

    [Header("Blast")]
    [SerializeField] GameObject blast;
    [SerializeField] public int blastScrapCost;
    [SerializeField] public int blastWaterCost;
    [SerializeField] public int blastSoapCost;

    [Header("Bath")]
    [SerializeField] GameObject bath;
    [SerializeField] public int bathWaterCost;
    [SerializeField] public int bathSoapCost;


    [SerializeField] float yOffset;

    public bool isOccupied = false;

    void Update()
    {
        GetResourceAmounts();
    }
    public void GetResourceAmounts()
    {
        scrapAmount = resourceManager.GetComponent<ResourceManager>().scrap;
        waterAmount = resourceManager.GetComponent<ResourceManager>().water;
        soapAmount = resourceManager.GetComponent<ResourceManager>().soap;
    }
    public void TileColor(bool tileColor)
    {
        spriteRenderer.color = tileColor ? colorOne : colorTwo;
    }

    private void OnMouseEnter()
    {
        highlight.SetActive(true);
    }
    private void OnMouseExit()
    {
        highlight.SetActive(false);
    }

    private void OnMouseDown()
    {
        if (isOccupied == false && LevelManager.GetComponent<LevelManager>().isPaused == false)
        {
            string selection = UIManager.GetComponent<UIManager>().GetSelection();

            if (selection == "scrap" && scrapAmount >= scrapScrapCost)
            {
                isOccupied = true;
                resourceManager.GetComponent<ResourceManager>().scrap -= scrapScrapCost;
                GameObject p = Instantiate(scrap, new Vector2(transform.position.x, transform.position.y + yOffset), Quaternion.identity);
                spriteRenderer.color = colorThree;
                audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.MACHINEPLACE);
            }
            if (selection == "water" && scrapAmount >= waterScrapCost)
            {
                isOccupied = true;
                resourceManager.GetComponent<ResourceManager>().scrap -= waterScrapCost;
                GameObject p = Instantiate(water, new Vector2(transform.position.x, transform.position.y + yOffset), Quaternion.identity);
                spriteRenderer.color = colorThree;
                audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.MACHINEPLACE);
            }
            if (selection == "soap" && scrapAmount >= soapScrapCost)
            {
                isOccupied = true;
                resourceManager.GetComponent<ResourceManager>().scrap -= soapScrapCost;
                GameObject p = Instantiate(soap, new Vector2(transform.position.x, transform.position.y + yOffset), Quaternion.identity);
                spriteRenderer.color = colorThree;
                audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.MACHINEPLACE);
            }
            if (selection == "machine" && scrapAmount >= machineScrapCost)
            {
                isOccupied = true;
                resourceManager.GetComponent<ResourceManager>().scrap -= machineScrapCost;
                GameObject p = Instantiate(machine, new Vector2(transform.position.x, transform.position.y + yOffset), Quaternion.identity);
                spriteRenderer.color = colorThree;
                audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.MACHINEPLACE);
            }
            if (selection == "barrage" && scrapAmount >= barrageScrapCost && waterAmount >= barrageWaterCost)
            {
                isOccupied = true;
                resourceManager.GetComponent<ResourceManager>().scrap -= barrageScrapCost;
                resourceManager.GetComponent<ResourceManager>().water -= barrageWaterCost;
                GameObject p = Instantiate(barrage, new Vector2(transform.position.x, transform.position.y + yOffset), Quaternion.identity);
                spriteRenderer.color = colorThree;
                audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.MACHINEPLACE);
            }
            if (selection == "blast" && scrapAmount >= blastScrapCost && waterAmount >= blastWaterCost && soapAmount >= blastSoapCost)
            {
                isOccupied = true;
                resourceManager.GetComponent<ResourceManager>().scrap -= blastScrapCost;
                resourceManager.GetComponent<ResourceManager>().water -= blastWaterCost;
                resourceManager.GetComponent<ResourceManager>().soap -= blastSoapCost;
                GameObject p = Instantiate(blast, new Vector2(transform.position.x, transform.position.y + yOffset), Quaternion.identity);
                spriteRenderer.color = colorThree;
                audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.MACHINEPLACE);
            }
            if (selection == "bath" && waterAmount >= bathWaterCost && soapAmount >= bathSoapCost)
            {
                isOccupied = true;
                resourceManager.GetComponent<ResourceManager>().water -= bathWaterCost;
                resourceManager.GetComponent<ResourceManager>().soap -= bathSoapCost;
                GameObject p = Instantiate(bath, new Vector2(transform.position.x, transform.position.y), Quaternion.identity);
                spriteRenderer.color = colorThree;
                audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.MACHINEPLACE);
            }
        }
    }
}
