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

public class LoadScene : MonoBehaviour {

    public EncounterDetails details;
    public EventSystem system;
    public AudioSource audio;
    public bool killMusic;

	// Use this for initialization
	void Start () 
    {
        DontDestroyOnLoad(gameObject);
	}
	
    public void OnClick(EncounterDetails sender)
    {
        

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

        audio = gameObject.GetComponent<AudioSource>();

        if (audio.isPlaying)
        {
            audio.Stop();
        }

        var audioNode = xmlDoc.SelectSingleNode("//encounters/encounterType/encounter[@enID=\"" + details.enID + "\"]");
//        var clip = (AudioClip)Resources.Load("Audio/MPTMFiles/" + audioNode.Attributes["audio"].Value);
//        audio.loop = true;
//        audio.clip = clip;
//        audio.Play();

        LoadTheScene();
        //do something with the sender
    }

	// Update is called once per frame
	void Update () {
        if (killMusic)
        {
            audio.Stop();
            killMusic = false;
        }
	}

    public void LoadTheScene()
    {
        

        switch (details.type)
        {
            case EncounterTypes.Adventure:
                SceneManager.LoadScene("BaseLevel");
                break;
            case EncounterTypes.Combat:
                SceneManager.LoadScene("CombatScene");
                break;
            case EncounterTypes.Port:
                SceneManager.LoadScene("BaseLevel");
                break;
            case EncounterTypes.Storm:
				SceneManager.LoadScene("WaveScene");
                break;
            case EncounterTypes.Treasure:
                SceneManager.LoadScene("BaseLevel");
                break;
        }
    }
}
