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

/// <summary>
/// отмена создания стены, маяка, зоны
/// </summary>
public class HistoryController : MonoBehaviour
{
    public static HistoryController instance;

    public Button redo;
    public Button undo; // отмена
    public List<History> histories = new List<History>();

    private void Awake()
    {
        instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        undo.onClick.AddListener(Undo);
        redo.onClick.AddListener(Redo);
    }

    // Update is called once per frame
    void Update()
    {

    }
 
    public void Undo() {
        var last = histories.Where(l => l.status).LastOrDefault();
        if (last != null)
        {
            last.obj.SetActive(false);
            if (last.ButtonPanel != null) last.ButtonPanel.SetActive(false);
            last.status = false;
        }
    }

    public void Redo()
    {
        var last = histories.Where(l => !l.status).FirstOrDefault();
        if (last != null)
        {
            last.obj.SetActive(true);
            if(last.ButtonPanel != null) last.ButtonPanel.SetActive(true);
            last.status = true;
        }
        histories.Remove(last);
    }

    public void EventHistory(uint id, GameObject gameObject, GameObject panel = null)
    {
        histories.Add(new History { id = id, obj = gameObject, ButtonPanel = panel, status = true });
    }
}
