// The miscellaneous assortment of random methods that every lazy programmer
// inevitably creates.
//
// Do not rely on any of these functions hanging around long-term!
// See https://www.jeromethibaud.com/en/blog/utils-considered-harmful/ for why;
// when we're feeling less lazy these will probably get refactored, but this is
// just a fun personal project so, for now, YOLO.

using Sandbox.ModAPI.Ingame;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace IngameScript
{
    partial class Program
    {
        public class Utils
        {
            public static List<IMyTerminalBlock> GetBlocksNameContains(
                IMyGridTerminalSystem gridSystem,
                string name)
            {
                List<IMyTerminalBlock> allBlocks = new List<IMyTerminalBlock>();
                List<IMyTerminalBlock> result = new List<IMyTerminalBlock>();
                gridSystem.GetBlocks(allBlocks);

                foreach (IMyTerminalBlock block in allBlocks)
                {
                    if (block.CustomName.Contains(name)) result.Add(block);
                }
                return result;
            }

            public static void GetBlocksOfTypeNameContains<T>(
                IMyGridTerminalSystem gridSystem,
                List<T> result,
                string name)
                where T : class
            {
                List<T> allBlocks = new List<T>();
                gridSystem.GetBlocksOfType(allBlocks);

                foreach (T block in allBlocks)
                {
                    if (((IMyTerminalBlock)block).CustomName.Contains(name)) result.Add(block);
                }
            }

            public static string ExtractTag(IMyTerminalBlock block, string tag)
            {
                foreach (string token in block.CustomName.Split(' ')) {
                    if (token.StartsWith(tag)) return token;
                }
                return "";
            }
        }
    }
}