using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Render;
using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
using System;
using System.Collections.Generic;

namespace RecrownedAthenaeum.UI.SkinSystem
{
    /// <summary>
    /// A skin is used to group a theme which can then be applied to the UI via the use of modules.
    /// </summary>
    public class Skin : IDisposable, ISkin
    {
        /// <summary>
        /// Whether or not this skin is completed being built and thus ready to use.
        /// </summary>
        public bool Laminated { get; private set; }
        private bool disposed;

        private TextureAtlas textureAtlas;

        Dictionary<string, Color> colors;
        readonly Dictionary<string, string> definitionOfType;
        readonly Dictionary<string, Dictionary<string, SkinDefinitionData>> definitions;

        /// <summary>
        /// The texture for the cursor.
        /// </summary>
        public virtual Texture2D CursorTexture { get; private set; }

        /// <summary>
        /// Creates a basic unfilled skin.
        /// </summary>
        /// <param name="textureAtlas">The texture atlas to use for this skin.</param>
        /// <param name="cursorTexture">The texture the cursor will be.</param>
        public Skin(TextureAtlas textureAtlas, Texture2D cursorTexture)
        {
            this.textureAtlas = textureAtlas;
            this.CursorTexture = cursorTexture;
            colors = new Dictionary<string, Color>();
            definitionOfType = new Dictionary<string, string>();
            definitions = new Dictionary<string, Dictionary<string, SkinDefinitionData>>();
        }

        /// <summary>
        /// Returns a <see cref="TextureAtlas.Region"/> with given name of region. Null values acceptable. Will return null if parameter is null.
        /// </summary>
        /// <param name="name">Name of region.</param>
        /// <param name="required">Whether or not this texture is mandatory for the module to work. If true, will throw error on failing to retrieve.</param>
        /// <returns>The region corresponding to the name or null if the requested region doesn't exist.</returns>
        public TextureAtlas.Region GetTextureAtlasRegion(string name, bool required = false)
        {
            if (!required && (name == null || !textureAtlas.ContainsRegion(name)))
            {
                return null;
            } else
            {
                return textureAtlas[name];
            }
        }

        /// <summary>
        /// Returns a <see cref="Color"/> with given name of defined color;
        /// </summary>
        /// <param name="name">Name of defined color. Will use "default" if null. Default value is null.</param>
        /// <returns>The defined color based on the name given.</returns>
        public Color GetColor(string name = null)
        {
            if (name == null) name = "default";
            return colors[name];
        }

        /// <summary>
        /// Draws a region from the texture atlas.
        /// </summary>
        /// <param name="regionName">Region to draw.</param>
        /// <param name="color">The color to tint the region.</param>
        /// <param name="batch">The batch to use.</param>
        /// <param name="destination">The destination to draw to.</param>
        /// <param name="rotation">The rotation to use in radians.</param>
        /// <param name="origin">The origin for the rotation.</param>
        public void Draw(string regionName, string color, ConsistentSpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2))
        {
            if (disposed) throw new ObjectDisposedException(GetType().Name);
            textureAtlas.Draw(regionName, batch, destination, colors[color], rotation, origin);
        }

        private SkinDefinitionData ObtainDefinition(string typeFullName, string definitionName)
        {
            if (disposed) throw new ObjectDisposedException(GetType().Name);
            if (!Laminated) throw new InvalidOperationException("Skin has yet to be laminated yet.");
            if (definitionName == null) definitionName = "default";
            if (!definitions.ContainsKey(typeFullName)) throw new KeyNotFoundException("Could not find any skin definition defining type \"" + typeFullName + "\"");
            if (!definitions[typeFullName].ContainsKey(definitionName)) throw new KeyNotFoundException("Could not find skin definition defining type  \"" + typeFullName + "\" with name \"" + definitionName + "\"");
            return definitions[typeFullName][definitionName];
        }

        /// <summary>
        /// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist.
        /// </summary>
        /// <typeparam name="T">Convenience to cast to the needed definition type.</typeparam>
        /// <param name="definitionName">The name of the definition.</param>
        /// <returns>The definition cast to T.</returns>
        public T ObtainDefinition<T>(string definitionName = null) where T : SkinDefinitionData
        {
            return (T)ObtainDefinition(definitionOfType[typeof(T).FullName], definitionName);
        }

        /// <summary>
        /// Adds the definition.
        /// </summary>
        /// <param name="definitionName">The name of the definition. Default (if left blank) name is "default".</param>
        /// <param name="skinDefinition">The definition itself.</param>
        public void AddDefinition(SkinDefinitionData skinDefinition, string definitionName = null)
        {
            if (disposed) throw new ObjectDisposedException(GetType().Name);
            if (Laminated) throw new InvalidOperationException("This skin has been laminated and cannot be edited.");
            if (definitionName == null) definitionName = "default";
            if (!definitions.ContainsKey(skinDefinition.uiModuleTypeFullName))
            {
                definitionOfType.Add(skinDefinition.GetType().FullName, skinDefinition.uiModuleTypeFullName);
                definitions.Add(skinDefinition.uiModuleTypeFullName, new Dictionary<string, SkinDefinitionData>());
            }
            else if (definitions[skinDefinition.uiModuleTypeFullName].ContainsKey(definitionName)) throw new ArgumentException("Type of definition with that name already exists!");

            definitions[skinDefinition.uiModuleTypeFullName].Add(definitionName, skinDefinition);
        }

        /// <summary>
        /// Adds color to skin.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="color"></param>
        public void AddColor(string name, Color color)
        {
            if (Laminated) throw new InvalidOperationException("This skin has been laminated and cannot be edited.");
            colors.Add(name, color);
        }

        /// <summary>
        /// Laminates the skin. Making sure no more additions are done and sets the skin to be ready for use.
        /// Needs to be called before any use of skin. Building skin needs to be done before lamination.
        /// </summary>
        public void Laminate()
        {
            Laminated = true;
        }

        /// <summary>
        /// Disposes <see cref="textureAtlas"/> and the <see cref="Texture2D"/> holding the cursor texture.
        /// </summary>
        public void Dispose()
        {
            if (disposed) throw new ObjectDisposedException(GetType().Name);
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Overridable dispose function.
        /// </summary>
        /// <param name="disposing">true when it's a user call to dispose.</param>
        public virtual void Dispose(bool disposing)
        {
            disposed = true;
            if (disposing && !disposed)
            {
                textureAtlas.Dispose();
                CursorTexture.Dispose();
            }
        }

        /// <summary>
        /// Destructor. Calls the dispose with false.
        /// </summary>
        ~Skin()
        {
            Dispose(false);
        }
    }
}
