using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

public class DialogController : MonoBehaviour
{
	public AudioClip buttonClickSound;
	public Text titleText;
	public Text messageText;
	public Button leftButton;
	public Text leftButtonText;
	public Button rightButton;
	public Text rightButtonText;

	public void Initialize (string title, string message, string button1Text, string button2Text, Action<int> clickAction, AudioClip button1Sound = null, AudioClip button2Sound = null)
	{
		if (title != null) {
			this.titleText.text = title;
		} else {
			this.titleText.gameObject.SetActive (false);
		}
		this.messageText.text = message;

		if (button1Text != null && button1Text.Length > 9 || button2Text != null && button2Text.Length > 9) {
			((RectTransform)this.leftButton.transform).sizeDelta = new Vector2 (580, 180);
			((RectTransform)this.rightButton.transform).sizeDelta = new Vector2 (580, 180);
		}

		if (button1Text != null) {
			this.leftButtonText.text = button1Text;
			this.leftButton.onClick.AddListener (delegate {
				this.PlaySound (button1Sound);
				clickAction (1);
				GameObject.Destroy (this.gameObject);
			});
		} else {
			this.leftButton.gameObject.SetActive (false);
		}
		
		if (button2Text != null) {
			this.rightButtonText.text = button2Text;
			this.rightButton.onClick.AddListener (delegate {
				this.PlaySound (button2Sound);
				clickAction (2);
				GameObject.Destroy (this.gameObject);
			});
		} else {
			this.rightButton.gameObject.SetActive (false);
		}
	}

	private void PlaySound (AudioClip sound)
	{
		if (sound == null) {
			sound = this.buttonClickSound;
		}

		AudioManager2D.instance.PlaySound (sound);
	}
}
