﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;

public class ButtonHandler : MonoBehaviour
{
    [SerializeField] GameObject housePrefab;
    [SerializeField] GameObject buildingPrefab;
    [SerializeField] GameObject man;
    [SerializeField] TMP_Text text;
    [SerializeField] AudioClip plantSound;

    AudioSource audio;
    private void Start()
    {
        audio = GetComponent<AudioSource>();

        if (SceneManager.GetActiveScene().buildIndex == 3)
        {
            for (int i = 0; i < Game.I.homesPlaced; i++)
            {
                Vector3 pos = new Vector3(Random.Range(-8f, 8f), Random.Range(-3f, 0f), 0);
                Instantiate(housePrefab, pos, Quaternion.identity);
            }
            for (int i = 0; i < Game.I.menPlaced; i++)
            {
                Vector3 pos = new Vector3(Random.Range(-8f, 8f), Random.Range(-3f, -.5f), 0);
                Instantiate(man, pos, Quaternion.identity);
            }
            for (int i = 0; i < Game.I.buildingsPlaced; i++)
            {
                Vector3 pos = new Vector3(Random.Range(-8f, 8f), Random.Range(-14f, -11f), 0);
                Instantiate(buildingPrefab, pos, Quaternion.identity);
            }
            text.text = $"Plants: {Game.I.plantNum}";
            text.text = $"Plants: {Game.I.plantNum}";
        }
        else if (SceneManager.GetActiveScene().buildIndex == 4)
        {
            for (int i = 0; i < Game.I.homesPlaced; i++)
            {
                Vector3 pos = new Vector3(Random.Range(-8f, 8f), Random.Range(-14f, -10f), 0);
                Instantiate(housePrefab, pos, Quaternion.identity);
            }
            for (int i = 0; i < Game.I.menPlaced; i++)
            {
                Vector3 pos = new Vector3(Random.Range(-8f, 8f), Random.Range(-14f, -11f), 0);
                Instantiate(man, pos, Quaternion.identity);
            }
            for (int i = 0; i < Game.I.buildingsPlaced; i++)
            {
                Vector3 pos = new Vector3(Random.Range(-8f, 8f), Random.Range(-14f, -10f), 0);
                Instantiate(buildingPrefab, pos, Quaternion.identity);
            }
        }
    }

    public void ChangeScene(int index)
    {
        SceneManager.LoadScene(index);
    }

    public void AddPlants()
    {
        var script = FindObjectOfType<GroundScript>();

        if(Game.I.plantNum <= 0)
        {
            return;
        }

        if (Game.I.plantsUsed < 40)
        {
            script.AddPlant();
            Game.I.plantNum--;
            Game.I.plantsUsed++;
            text.text = $"Plants: {Game.I.plantNum}";
        }

        else if ((Game.I.plantsUsed >= 40 && Game.I.plantsUsed < 60))
        {
            Game.I.plantNum--;
            Game.I.menPlaced++;
            Game.I.plantsUsed++;
            Vector3 pos = new Vector3(Random.Range(-8f, 8f), Random.Range(-3f, 0f), 0);
            Instantiate(man, pos, Quaternion.identity);
            text.text = $"Plants: {Game.I.plantNum}";
        }

        else if ((Game.I.plantsUsed >= 60 && Game.I.plantsUsed < 80))
        {
            Game.I.plantNum--;
            Game.I.homesPlaced++;
            Game.I.plantsUsed++;
            Vector3 pos = new Vector3(Random.Range(-8f, 8f), Random.Range(-3f, 0f), 0);
            Instantiate(housePrefab, pos, Quaternion.identity);
            text.text = $"Plants: {Game.I.plantNum}";
        }

        else if (Game.I.plantsUsed >= 80 && Game.I.plantsUsed < 100)
        {
            Game.I.plantNum--;
            Game.I.buildingsPlaced++;
            Game.I.plantsUsed++;
            Vector3 pos = new Vector3(Random.Range(-8f, 8f), Random.Range(-3f, 0f), 0);
            Instantiate(buildingPrefab, pos, Quaternion.identity);
            text.text = $"Plants: {Game.I.plantNum}";
        }
        else
        {
            Game.I.plantNum--;
            Game.I.menPlaced++;
            Game.I.plantsUsed++;
            Vector3 pos = new Vector3(Random.Range(-8f, 8f), Random.Range(-3f, 0f), 0);
            Instantiate(man, pos, Quaternion.identity);
            text.text = $"Plants: {Game.I.plantNum}";
        }

        audio.PlayOneShot(plantSound);
    }

}
