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

public class ForceButtonScript : MonoBehaviour {
    [SerializeField] private Image image = null;
    [SerializeField] private Button button = null;

    private System.Action buttonAction;
    private bool pressed = false;
    private string spriteName;

    public void Initialize(string spriteName, System.Action buttonAction) {
        this.spriteName = spriteName;
        this.buttonAction = buttonAction;

        image.sprite = Resources.Load<Sprite>("Art/UI/" + spriteName);
        if (image.sprite == null) {
            Debug.LogError("Couldn't find sprite: Art/UI/" + spriteName);
        }
        button.onClick.RemoveAllListeners();
        button.onClick.AddListener(ButtonClicked);
    }

    private void ButtonClicked() {
        if (pressed) {
            return;
        }

        pressed = true;
        image.sprite = Resources.Load<Sprite>("Art/UI/" + spriteName + "Click");
        if (image.sprite == null) {
            Debug.LogError("Couldn't find sprite: Art/UI/" + spriteName + "Click");
        }
        buttonAction();
    }

    public void Unclick() {
        image.sprite = Resources.Load<Sprite>("Art/UI/" + spriteName);
        if (image.sprite == null) {
            Debug.LogError("Couldn't find sprite: Art/UI/" + spriteName);
        }
        pressed = false;
    }
}
