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

public enum EncounterTypes
{
    Combat, Adventure, Treasure, Storm, Port
}

public class EncounterMessage : MonoBehaviour {

    public Text text;
    public RectTransform panelTransform;
    public Button baseButton;
    public XmlNode options = new XmlDocument();
    public LoadScene sceneLoader;
    private EncounterDetails details;
    public GameObject sceneManager;

    public struct buttonData
    {
        public string name;
        public string text;
        public string outcomeText;
        public int outcomeCode;
    }

    public Dictionary<string, buttonData> dataOfButtons = new Dictionary<string, buttonData>();
    private bool killForm;
    private float curTime;

    // Use this for initialization
    void Start()
    {
        GameObject sceneLder = GameObject.FindWithTag("SceneManager");
        details = sceneLder.GetComponent<LoadScene>().details;

        var xmlDoc = new XmlDocument();
        xmlDoc.Load(@"Assets/Script Resources/Encounters.xml");
	
        var encounterNode = GetEncounterMessage(xmlDoc);

        AddButtons(encounterNode);
        
        
    }

	// Update is called once per frame
	void Update () 
    {
		if (killForm)
        {
            if (Time.time - curTime > 3)
            {
                //Destroy(this.gameObject);
                this.gameObject.SetActive(false);
                sceneManager.GetComponent<BaseSceneManager>().encounterBegun = true;
            }
        }
	}

    public void ShowSceneWithText(string passedMessage)
    {
        this.gameObject.SetActive(true);
        text.text = passedMessage;
    }

    // Even more child nodes, why god why.
    void AddButtons(XmlNode encounterNode)
    {
        options = encounterNode.ChildNodes[1];
        var noOfButtons = int.Parse(options.Attributes["amnt"].Value);


        for (int i = 0; i < noOfButtons; ++i)
        {
            Button b = Instantiate(baseButton, panelTransform);
            buttonData bd = new buttonData();
            bd.name = "Button" + i.ToString();
            b.name = bd.name;

            bd.text = options.ChildNodes[i].ChildNodes[0].InnerText;
            b.GetComponentInChildren<Text>().text = bd.text;
            bd.outcomeText = options.ChildNodes[i].ChildNodes[1].InnerText;
            bd.outcomeCode = int.Parse(options.ChildNodes[i].Attributes["opId"].Value);

            dataOfButtons.Add(bd.name, bd);

            b.onClick.AddListener(delegate { TaskOnClick(b.name); });
        }
    }

    // Sorry for the amount of child nodes, this has gotten out of hand.
    XmlNode GetEncounterMessage(XmlDocument xmlDoc)
    {
        //var encounterType = (EncounterTypes)UnityEngine.Random.Range(0, 4);
        var type = xmlDoc.SelectSingleNode("//encounters/encounterType[@type=\"" + details.type + "\"]");
        var noOfOptions = int.Parse(type.Attributes["count"].Value);

        //var encounter = UnityEngine.Random.Range(0, noOfOptions - 1);
		var encounterNode = type.ChildNodes[details.enID - 1];
		
        text.text += encounterNode.ChildNodes[0].InnerText;

        return encounterNode;
    }

    void TaskOnClick(string name)
    {
        buttonData bd = new buttonData();
        dataOfButtons.TryGetValue(name, out bd);

        text.text = bd.outcomeText;
        var buttons = GetComponentsInChildren<Button>();

        for (int i = buttons.Length - 1; i >= 0; i--)
        {
            Destroy(buttons[i].gameObject);
        }

        killForm = true;
        curTime = Time.time;
    }


}
