﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

using SFML.System;
using SFML.Window;
using SFML.Graphics;

namespace TreasureHunt
{
    class Constants
    {
        public const uint WindowWidth = 1280;
        public const uint WindowHeight = 720;
        public const string WindowTitle = "TreasureHunt";
        public const uint AntialiasLevel = 8;
        public const float TileSize = 20.0f;
    }

    class Globals
    {
        public static Random Random = new Random((int)DateTime.Now.Ticks);
    }

    class Program
    {
        static void Main(string[] args)
        {
            VideoMode videoMode = new VideoMode(Constants.WindowWidth, Constants.WindowHeight);
            ContextSettings contextSettings = new ContextSettings(0, 0, Constants.AntialiasLevel);
            RenderWindow window = new RenderWindow(videoMode, Constants.WindowTitle, Styles.Default, contextSettings);

            World world = new World();
            world.RenderTarget = window;

            window.Closed += (sender, e) => { window.Close(); };
            window.LostFocus += (sender, e) => { world.Pause(); window.SetTitle(Constants.WindowTitle + " - PAUSED"); };
            window.GainedFocus += (sender, e) => { world.Resume(); window.SetTitle(Constants.WindowTitle); };

            world.Create<Terrain>().SetLayer(0);
            world.Create<Treasure>().SetLayer(1).AddToGroup("Treasure");
            world.Create<Vision>().SetLayer(2);
            world.Create<Explorer>().AddTag("Explorer").SetLayer(3);

            while (window.IsOpen)
            {
                window.DispatchEvents();

                if (Keyboard.IsKeyPressed(Keyboard.Key.Space))
                {
                    world.Update();

                    while (Keyboard.IsKeyPressed(Keyboard.Key.Space) && !Keyboard.IsKeyPressed(Keyboard.Key.RAlt)) Thread.Yield();
                }

                window.Clear();

                world.Draw();

                window.Display();

                Thread.Yield();
            }
        }
    }
}
