using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

[System.Serializable]
public class BuildEvent : UnityEvent<int>
{

}

public class GameMenu : MonoBehaviour
{
    public int money = 2000000;
    public int population = 1;

    public GameObject[] home;
    public GameObject[] shop;
    public GameObject[] serve;

    public GameObject popUpMenu;
    public GameObject buildMenu;
    public GameObject openMenu;
    public GameObject missionBin;

    public GameObject missionPrefab;

    // building inputs
    public Dropdown buildingTypeSelector;
    public InputField buildingNameInput;

    public Text cityName;

    public BuildEvent buildEvent;

    private GameController gc;

    public AudioSource buttonPress;
    public AudioSource purchase;
    public AudioSource error;

    void Start()
    {
        buildEvent = new BuildEvent();
        gc = GameObject.Find("GameController").GetComponent<GameController>();
        popUpMenu.SetActive(false);
        buildMenu.SetActive(false);
        cityName.text = gc.cityName;

        Mission m1 = PostMission(
            "Build your first 5 houses",
            "Click \"menu\", and hit construct to build a house. Then click to place the house."
        );

        m1.BeBuildMission(0, 5, 10000);

        Mission m2 = PostMission(
            "Make REAL money",
            "Build an office for people to work at. Pick OFFICE on the build menu to build an office."
        );

        m2.BeBuildMission(1, 1, 10000);

        Mission m3 = PostMission(
            "We need to eat!",
            "Create a super market."
        );

        m3.BeBuildMission(1, 1, 10000);

        Mission m4 = PostMission(
            "Crime rates are high... probably",
            "Buil a police station to protect the peace."
        );

        m4.BeBuildMission(2, 1, 15000);
    }

    public void ShowPopUp()
    {
        buttonPress.Play();
        popUpMenu.SetActive(true);
        openMenu.SetActive(false);
    }

    public void HidePopUp()
    {
        buttonPress.Play();
        popUpMenu.SetActive(false);
        openMenu.SetActive(true);
    }

    public void ShowBuild()
    {
        buttonPress.Play();
        popUpMenu.SetActive(false);
        buildMenu.SetActive(true);
    }

    public void HideBuild()
    {
        buttonPress.Play();
        buildMenu.SetActive(false);
        popUpMenu.SetActive(true);
    }

	public void MakeBuilding()
	{
        // get values
        int bType = buildingTypeSelector.value;
        string name = buildingNameInput.text;

        // make sure they can afford!
        int cost = 0;
        if (bType == 0) cost = 200000;
        else if (bType == 1) cost = 150000;
        else if (bType == 2) cost = 150000;

        if (money < cost)
        {
            error.Play();
            return;
        }

        money -= cost;

        buildMenu.SetActive(false);
        purchase.Play();

        // reset inputs
        buildingTypeSelector.value = 0;
        buildingNameInput.text = "";

        GameObject obj = null;
        if (bType == 0)
        {
            obj = Instantiate(home[0], GameObject.Find("Environment").transform);
            population += 4;
        }
        else if (bType == 1)
            obj = Instantiate(shop[0], GameObject.Find("Environment").transform);

        else if (bType == 2)
            obj = Instantiate(serve[0], GameObject.Find("Environment").transform);

        Building b = obj.GetComponent<Building>();
        b.bType = bType;
        b.title.text = name;
    }

    public Mission PostMission(string title, string msg)
    {
        GameObject obj = Instantiate(missionPrefab, missionBin.transform);
        obj.transform.GetChild(0).gameObject.GetComponent<Text>().text = title;
        obj.transform.GetChild(1).gameObject.GetComponent<Text>().text = msg;
        return obj.GetComponent<Mission>();
    }
}
