using System;
using System.Collections.Generic;
using Quik.CommandMachine;
using Quik.Controls;
using Quik.Media;
using Quik.PAL;

namespace Quik
{
    /// <summary>
    /// Main class for Quik applications.
    /// </summary>
    public class QuikApplication
    {
        /// <summary>
        /// The application platform driver.
        /// </summary>
        public IQuikPlatform Platform { get; }

        /// <summary>
        /// Title of the application.
        /// </summary>
        public string Title
        {
            get => Platform.Title;
            set => Platform.Title = value;
        }

        /// <summary>
        /// Application icon.
        /// </summary>
        public QImage Icon
        {
            get => Platform.Icon;
            set => Platform.Icon = value;
        }

        public View MainView { get; private set; } = null;

        /// <summary>
        /// List of media loaders, drivers that load media such as images and fonts.
        /// </summary>
        public List<MediaLoader> MediaLoaders { get; } = new List<MediaLoader>();

        public QuikApplication(IQuikPlatform platform)
        {
            Platform = platform;
        }

        public void Run(View mainView)
        {
            IQuikPort port = Platform.CreatePort();
            CommandQueue cmd = new CommandQueue();

            MainView = mainView;

            port.EventRaised += (sender, ea) => mainView.NotifyEvent(sender, ea);

            while (port.IsValid)
            {
                Platform.ProcessEvents(false);
                if (port.IsValid)
                {
                    MainView.Paint(cmd);
                    port.Paint(cmd);
                }
            }
        }
    }
}