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

using Microsoft.Xna.Framework.Graphics;

namespace EVE.EVEGpgpu
{
    public class RTData
    {
        private bool m_lock;

        public bool Lock
        {
            get { return m_lock; }
            set { m_lock = value; }
        }

        public bool isGoodRT(SurfaceFormat sf, int w, int h)
        {
            if (m_RT.Width == w && m_RT.Height == h && m_RT.Format == sf && !m_lock)
                return true;
            return false;
        }

        public RenderTarget2D RT
        {
            get { return m_RT; }
        }

        private RenderTarget2D m_RT;

        public RTData(GraphicsDevice gd, int w, int h, int lvl, SurfaceFormat sF, bool isLocked)
        {
            m_RT = new RenderTarget2D(gd, w, h, lvl, sF);
            m_lock = isLocked;
        }
    }

    public sealed class RTManager
    {
        private GraphicsDevice currentProcessor;

        private static readonly RTManager instance = new RTManager();

             public static RTManager Instance
        {
            get { return instance; }
        }

        private static List<RTData> RTPool = new List<RTData>();

        public void Initialize(GraphicsDevice gd)
        {
            currentProcessor = gd;
        }

        public RTData requestRT(SurfaceFormat sf, int w, int h)
        {
            foreach (RTData element in RTPool)
            {
                if (element.isGoodRT(sf, w, h))
                {
                    element.Lock = true;
                    return element;
                }
            }

            RTData tempRTData = new RTData(currentProcessor, w, h, 1, sf, true);
            RTPool.Add(tempRTData);

            return tempRTData;
        }
    }
}
