using Sandbox.ModAPI.Ingame;
using System.Collections.Generic;
using VRage.Game.ModAPI.Ingame.Utilities;

namespace IngameScript
{
    public partial class Program : MyGridProgram, IConsoleProgram
    {
        public IConsole Console { get; private set; }
        public MyIni Ini { get; private set; }

        private OreReport _oreReport = null;

        public Program()
        {
            Ini = new MyIni();
            Console = new MainConsole(this, "Info Displays");

            // find displays that are configured to show info, create the necessary objects.
            // TODO: currently we only support one display per report, and we just use the first one
            List<IMyTerminalBlock> displays = new List<IMyTerminalBlock>();
            GridTerminalSystem.GetBlocksOfType(displays, blockFilter);
            foreach (IMyTerminalBlock display in displays)
            {
                Ini.TryParse(display.CustomData);
                List<MyIniKey> keys = new List<MyIniKey>();
                Ini.GetKeys("infoDisplay", keys);
                foreach (MyIniKey key in keys)
                {
                    switch (key.Name)
                    {
                        case "ingotReport":
                            if (_oreReport != null) break;
                            int index = Ini.Get(key).ToInt32();
                            // extract text surface
                            if (display is IMyTextSurface)
                            {
                                _oreReport = new OreReport(display as IMyTextSurface, this);
                                Console.Print($"Showing Ore Report on '{display.CustomName}'");
                            }
                            else if (display is IMyTextSurfaceProvider)
                            {
                                _oreReport = new OreReport(((IMyTextSurfaceProvider)display).GetSurface(index), this);
                                Console.Print($"Showing Ore Report on '{display.CustomName}'");
                            }
                            else
                            {
                                Console.Print("Tried to configure object without display surface.");
                            }
                            break;
                        default:
                            Console.Print($"Display type '{key.Name}' not defined.");
                            break;
                    }
                }
            }

            Console.UpdateTickCount();
            if (_oreReport == null)
            {
                Console.Print("No configured displays found. Shutting down.");
                return;
            }
            Runtime.UpdateFrequency = UpdateFrequency.Update100;
        }

        public void Main(string argument, UpdateType updateSource)
        {
            Console.UpdateTickCount();

            if (_oreReport != null)
            {
                _oreReport.Update();
                _oreReport.Draw();
            }
        }

        private bool blockFilter(IMyTerminalBlock block)
        {
            return block.IsSameConstructAs(this.Me) && MyIni.HasSection(block.CustomData, "infoDisplay");
        }
    }
}