using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using NaughtyAttributes;
using UnityEngine;

namespace EscapeRoomEngine.Engine.Runtime.UI
{
    /// <summary>
    /// The puzzle plan UI component that displays the current puzzle plan in the game master window.
    /// </summary>
    public class PuzzlePlan : MonoBehaviour
    {
        /// <summary>
        /// The distance from one entry to the next.
        /// </summary>
        [SerializeField]
        private Vector2 entryOffset;
        [BoxGroup("Internal")] [SerializeField]
        private RectTransform plan;
        [BoxGroup("Internal")] [SerializeField]
        private PuzzlePlanEntry currentPuzzle, entryPrefab;
        [BoxGroup("Internal")] [SerializeField]
        private GameObject currentPuzzleTitle, planTitle;

        public PuzzleModuleDescription CurrentPuzzle
        {
            set
            {
                currentPuzzleTitle.SetActive(true);
                currentPuzzle.gameObject.SetActive(true);
                
                // set the current puzzle
                currentPuzzle.Puzzle = value;
            }
        }
        public List<PuzzleModuleDescription> Puzzles
        {
            set
            {
                planTitle.SetActive(true);
                plan.gameObject.SetActive(true);
                
                // remove the old children
                foreach (RectTransform child in plan)
                {
                    Destroy(child.gameObject);
                }
                
                // add the new children
                var offset = Vector2.zero;
                value.ForEach(puzzle =>
                {
                    var entry = Instantiate(entryPrefab, plan, false);
                    entry.Position = offset;
                    entry.Puzzle = puzzle;
                    offset += entryOffset;
                });
            }
        }
    }
}