using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;

using SpaceEngineers.Game.ModAPI.Ingame;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;

using VRage;
using VRage.Collections;
using VRage.Game;
using VRage.Game.Components;
using VRage.Game.GUI.TextPanel;
using VRage.Game.ModAPI.Ingame;
using VRage.Game.ModAPI.Ingame.Utilities;
using VRage.Game.ObjectBuilders.Definitions;

using VRageMath;

namespace IngameScript
{
    class Output
    {
        IList<IMyTextSurface> outputs;
        int maxNbLines;

        public Output(IList<IMyTextSurface> surfaces, int maxNbLines = 10)
        {
            foreach (var s in surfaces)
            {
                s.ContentType = ContentType.TEXT_AND_IMAGE;
                s.WriteText("");
            }

            this.outputs = surfaces;
            this.maxNbLines = maxNbLines;
        }

        public Output(IMyCockpit cockpit, int maxNbLines = 10)
        {
            this.outputs = new List<IMyTextSurface>();
            for (int n = 0; n < cockpit.SurfaceCount; n++)
            {
                var surface = cockpit.GetSurface(n);
                surface.ContentType = ContentType.TEXT_AND_IMAGE;
                surface.WriteText("");
                this.outputs.Add(surface);
            }
            this.maxNbLines = maxNbLines;
        }

        public Output(IMyTextSurface surface, int maxNbLines = 10)
            : this(new List<IMyTextSurface> { surface }, maxNbLines)
        { }

        public void Print(string text, int outputNumber = 0)
        {
            if (this.outputs.Count() <= outputNumber)
            {
                throw new Exception($"Output number {outputNumber} doesn't exist (number of output: {this.outputs.Count()}");
            }
            else
            {
                var output = this.outputs[outputNumber];
                var currentText = output.GetText();
                var lines = currentText.Split('\n');
                if (lines.Count() >= this.maxNbLines)
                {
                    output.WriteText(lines.Skip(lines.Count() - this.maxNbLines + 1).Append(text).Aggregate((a, b) => a + Environment.NewLine + b));
                }
                else if (lines.Count() == 0)
                {
                    output.WriteText(text);
                }
                else
                {
                    output.WriteText(Environment.NewLine + text, true);
                }
            }
        }

        public void PrintError(string text, int outputNumber = 0)
        {
            this.Print($"Error: {text}", outputNumber);
        }


        public void Display(string text, int outputNumber = 0)
        {
            if (this.outputs.Count() <= outputNumber)
            {
                throw new Exception($"Output number {outputNumber} doesn't exist (number of output: {this.outputs.Count()}");
            }
            else
            {
                this.outputs[outputNumber].WriteText(text);
            }
        }
    }
}