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

namespace VisimBox.Renderer
{
    /// <summary>
    /// Base texture manager
    /// </summary>
    public abstract class TextureManager : AbstractResMan<Texture>
    {

        /// <summary>
        /// Creates texture from the System.Drawing.Bitmap, adds it to the database (changes name if needeed) and returns created texture.
        /// </summary>
        /// <param name="name">Required name for the texture (will be changed if manager already contains texture of this name)</param>
        /// <param name="bmp">Bitmap to be added</param>
        /// <returns>Texture created from the bitmap</returns>
        public virtual Texture Add(ref string name, System.Drawing.Bitmap bmp, bool alpha = false)
        {
            name = GetUniqueName(name);
            Texture t = CreateTexture(bmp, name);
            Add(name, t);
            return t;
        }
        
        /// <summary>
        /// Sets texture to specified channel
        /// </summary>
        /// <param name="name">Name of the texutre</param>
        /// <param name="channel">channel to use, default value is 0</param>
        public virtual void SetTexture(string name, IRenderContext rc, int channel = 0)
        {
            if (!resources.ContainsKey(name))
            {
                Console.DebugLine(string.Format("Texture {0} not found", name));
                return;
            }
            SetTexture(this[name], rc, channel);
        }

        /// <summary>
        /// Sets texture to speficied channel
        /// </summary>
        /// <param name="tex">Texture instance</param>
        /// <param name="channel">channel to use, default value is 0</param>
        public virtual void SetTexture(Texture tex, IRenderContext rc, int channel = 0)
        {
            if (channel != 0)
            {
                throw new ArgumentException("SetTexture different channel is NOT supported");
            }
            tex.Activate(rc);
        }

        /// <summary>
        /// Creates new texture from the System.Drawing.Bitmap. Note, doesn't add this to resources.
        /// Use Add(bitmap)
        /// </summary>
        /// <param name="bmp">Bitmap for the texture</param>
        /// <param name="alpha">if true, texture will be RGBA, otherwise RGB</param>
        /// <returns>New texture with RGB or RGBA texture format</returns>
        public abstract Texture CreateTexture(System.Drawing.Bitmap bmp, string name, bool alpha = false);
    }
}
