
using System;
using Quik.CommandMachine;

namespace Quik.Controls
{
    /// <summary>
    /// Bases for all UI elements.
    /// </summary>
    public abstract class UIBase
    {
        public UIBase Parent { get; protected set; }
        public string Id { get; set; }
        public QRectangle Bounds { get; set; }

        public QVec2 Position
        {
            get => Bounds.Min;
            set => Bounds = new QRectangle(Bounds.Max - Bounds.Min + value, value);
        }

        public QVec2 Size
        {
            get => Bounds.Max - Bounds.Min;
            set => Bounds = new QRectangle(value + Bounds.Min, Bounds.Min);
        }

        public QRectangle AbsoluteBounds
        {
            get
            {
                if (Parent == null)
                {
                    return Bounds;
                }
                else
                {
                    return new QRectangle(Bounds.Max + Parent.Position, Bounds.Min + Parent.Position);
                }
            }
        }

        public void NotifyEvent(object sender, EventArgs args)
        {
        }

        protected virtual void PaintBegin(CommandQueue cmd)
        {
            cmd.PushViewport();
            cmd.StoreViewport(AbsoluteBounds);
            cmd.PushZ();
        }

        protected virtual void PaintEnd(CommandQueue cmd)
        {
            cmd.PopZ();
            cmd.PopViewport();
        }

        public void Paint(CommandQueue cmd)
        {
            PaintBegin(cmd);
            PaintEnd(cmd);
        }
    }
}