﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Server.Gumps.Compendium
{
    //               Label    Image  Tiled Image Alpha Area Item HTML TextButton Button 
    //+–––––––––––––+––––––––+––––––+–––––––––––+––––––––––+––––+––––+––––––––––+––––––+
    //+ Type            x       x         x          x       x    x       x        x    
    //+ Name            x       x         x          x       x    x       x        x
    //| X Offset    |   x    |  x   |     x     |    x     | x  | x  |    x     |  x   |
    //| Y Offset    |   x    |  x   |     x     |    x     | x  | x  |    x     |  x   |
    //| Z           |   x    |  x   |     x     |    x     | x  | x  |    x     |  x   |

    //| Text        |   x    |      |           |          |    | x  |    x     |      |

    //| Color       |   x    |  x   |           |          | x  |    |    x     |      |
    //| Graphics ID |        |  x   |     x     |          | x  |    |          |  x   |
    //| Width       |        |      |     x     |    x     |    | x  |          |      |
    //| Height      |        |      |     x     |    x     |    | x  |          |      |
    //| Scrollbar   |        |      |           |          |    | x  |          |      |
    //| Background  |        |      |           |          |    | x  |          |      |
    //| Font Size   |        |      |           |          |    |    |    x     |      |
    //| GumpID      |        |      |           |          |    |    |    x     |  x   |
    //| GraphicsID2 +        +      +           +          +    +    +          +  x   +
    //                  7       7         8          7       7    10      9        8    
    //
    public class ImageElement : BaseCompendiumPageElement
    {
        public int Hue { get; set; }
        public int GumpId { get; set; }

        public override object Clone()
        {
            ImageElement image = new ImageElement();

            image.ElementType = this.ElementType.Clone() as string;
            image.Name = this.Name.Clone() as string;
            image.X = this.X;
            image.Y = this.Y;
            image.Z = this.Z;
            image.GumpId = this.GumpId;
            image.Hue = this.Hue;

            return image;
        }

        public static void Configure()
        {
            BaseCompendiumPageElement.RegisterElement("ImageElement", CreateImageElement);
            CompendiumPageEditor.RegisterElementType(typeof(ImageElement), CreateInstance, " Image      ");
        }

        //factory method
        public static BaseCompendiumPageElement CreateImageElement(XElement elementXml)
        {
            ImageElement elementToReturn = new ImageElement();

            try
            {
                elementToReturn.Deserialize(elementXml);
            }
            catch
            {
                elementToReturn = null;
            }

            return elementToReturn;
        }

        public override void Render(CompendiumPageGump gump)
        {
            gump.AddImage(X, Y, GumpId, Hue); 
        }

        public override List<ElementProperty> GetElementPropertiesSnapshot(List<ElementProperty> list = null)
        {
            list = base.GetElementPropertiesSnapshot();
            list.Add(new ElementProperty("Hue", OnHueTextEntryUpdate, Hue.ToString(), ElementProperty.InputType.TextEntry));
            list.Add(new ElementProperty("Gump ID", OnGumpIdTextEntryUpdate, GumpId.ToString(), ElementProperty.InputType.TextEntry));

            return list;
        }

        public static BaseCompendiumPageElement CreateInstance()
        {
            ImageElement image = new ImageElement();
            image.GumpId = 1;
            image.X = 0;
            image.Y = 0;
            image.Hue = 0;
            image.Name = "new Image";
            image.ElementType = "ImageElement";
            return image;
        }

        public void OnHueTextEntryUpdate(IGumpComponent gumpComponent, object param)
        {
            GumpTextEntry entry = gumpComponent as GumpTextEntry;

            if (entry != null)
            {
                try
                {
                    Hue = Convert.ToInt32(entry.InitialText);
                }
                catch
                {
                }
            }
        }

        public void OnGumpIdTextEntryUpdate(IGumpComponent gumpComponent, object param)
        {
            GumpTextEntry entry = gumpComponent as GumpTextEntry;

            if (entry != null)
            {
                try
                {
                    GumpId = Convert.ToInt32(entry.InitialText);
                }
                catch
                {
                }
            }
        }

        public override void Serialize(ref string xml, int indentLevel)
        {
            string indent = "";
            for (int indentIdx = 0; indentIdx < indentLevel; ++indentIdx)
            {
                indent += " ";
            }

            xml += string.Format("{0}{1}{2}", indent, "<Element>", Environment.NewLine);

            base.Serialize(ref xml, indentLevel + 1);

            xml += string.Format("{0}{1}{2}{3}{4}", indent, "<Hue>", Hue, "</Hue>", Environment.NewLine);
            xml += string.Format("{0}{1}{2}{3}{4}", indent, "<GumpId>", GumpId, "</GumpId>", Environment.NewLine);
            xml += string.Format("{0}{1}{2}", indent, "</Element>", Environment.NewLine);
        }

        public override void Deserialize(XElement xml)
        {
            try
            {
                base.Deserialize(xml);
                Hue = Convert.ToInt32(xml.Descendants("Hue").First().Value);
                GumpId = Convert.ToInt32(xml.Descendants("GumpId").First().Value);
            }
            catch (Exception e)
            {
                throw new Exception("Failed to parse ImageElement xml", e);
            }
        }
    }
}
