using GraphicsManager.Interfaces;
using GraphicsManager.Objects;
using OpenTK.Graphics.OpenGL4;

namespace Luski.GUI.MainScreen.UI.Generic;

public class MessageFlow : LuskiFlow
{
    public override void ResizeDraw(int x, int y, int sx, int sy, int sw, int sh)
    {
        if (LuskiExperiments.GUI.LiveLabelSize.IsEnabled())
        {
            if (OldHeight == Size.Y)
            {
                if (!UpdateScissorBox(ref x, ref y, ref sx, ref sy, ref sw, ref sh)) return;
                Window!.CopyFromFrontToBack(x, y, Size.X, Size.Y, x, y);
            }
            else if (OldHeight < Size.Y && OldWidth < Size.X)
            {
                if (!UpdateScissorBox(ref x, ref y, ref sx, ref sy, ref sw, ref sh)) return;
                Window!.CopyFromFrontToBack(x, y, OldWidth, OldHeight, x, y);
                int nsh = sh - OldHeight+1;
                if (nsh <= 0) return;
                int nsw = sw - OldWidth+1;
                if (nsw <= 0) return;
                GL.Scissor(sx, Window!.CS.Y - sy - sh, sw, nsh);
                DrawBase();
                GL.Scissor(sx, Window!.CS.Y - sy - sh, nsw, sh);
                DrawBase();
                GL.Scissor(sx, Window!.CS.Y - sy - sh, sw, sh);
            	
                for (int i = 0; i < Controls.Length; i++)
                {
                    if (!Controls[i].Loaded)
                    {
                        BlockDraw = true;
                        Controls[i].LoadToParent(this, Window!);
                        BlockDraw = false;
                    }
                    if (Controls[i].Location.X >= Size.X ||
                        Controls[i].Location.Y >= Size.Y ||
                        Controls[i].Location.X + Controls[i].Size.X < OldWidth ||
                        Controls[i].Location.Y + Controls[i].Size.Y < OldHeight) continue;
            				
            				
                    if (Controls[i] is IParent pp)
                    {
                        pp.ResizeDraw(x,y,sx,sy,sw,sh);
                        GL.Scissor(sx, Window!.CS.Y - sy - sh, sw, sh);
                    }
                    else
                    {
                        Controls[i].Draw(x,y,sx,sy,sw,sh);
                    }
                }
            }
            else
            {
                if (!UpdateScissorBox(ref x, ref y, ref sx, ref sy, ref sw, ref sh)) return;
                Window!.CopyFromFrontToBack(x, y, Size.X, Size.Y, x, y);
            }
        }
        else base.ResizeDraw(x,y,sx,sy,sw,sh);
    }
}