using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisimBox.Resources;
using VisiMath;

namespace VisimBox.Renderer
{
    /// <summary>
    /// Abstract base for vertex buffer manager
    /// </summary>
    public abstract class VertexBufferManager : AbstractResMan<IVertexBuffer>
    {
        /// <summary>
        /// Adds new already created vertex buffer to manager (use CreateBuffer to get correct vertex buffer!)
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">Valid vertex buffer</param>
        public override void Add(string key, IVertexBuffer value)
        {
            base.Add(key, value);
        }

        /// <summary>
        /// Creates new vertex buffer from vertex array
        /// </summary>
        /// <param name="key"></param>
        /// <param name="vertexarray">Vertex array containing data for vertex buffer</param>
        public virtual void Add(string key, IVertexArray vertexarray, ResourceUsage usage = ResourceUsage.Static)
        {
            IVertexBuffer ivb = CreateBuffer(vertexarray, usage);
            Add(key, ivb);
        }

        /// <summary>
        /// Creates new vertext buffer
        /// </summary>
        /// <param name="varray">Source VertexArray</param>
        /// <param name="usage">Requested usage</param>
        /// <returns>New VertexBuffer</returns>
        public abstract IVertexBuffer CreateBuffer(IVertexArray varray, ResourceUsage usage = ResourceUsage.Static);
        
        /// <summary>
        /// Binds vertex buffer for using in renderer
        /// </summary>
        /// <param name="name">Name of the buffer</param>
        public virtual void BindBuffer(string name)
        {
            if (!resources.ContainsKey(name))
            {
                Console.DebugLine(string.Format("VertexBuffer {0} not found", name));
                return;
            }
            BindBuffer(this[name]);
        }

        /// <summary>
        /// Binds vertex buffer for using in renderer
        /// </summary>
        /// <param name="vb">VertexBuffer object</param>
        public abstract void BindBuffer(IVertexBuffer vb);

        /// <summary>
        /// Unbinds buffer - sets current vertex buffer to null
        /// </summary>
        public abstract void UnBindBuffer();
    }
}
