using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using MercatorApi;
using MercatorExtensions;
using MercatorUi;
using MercatorDatabase;
using System.Xml;

namespace SigStock
{
    public class Customizer : MercatorUi.ICustomizers.IXmlDocumentUpdaterWithContextInfo, MercatorUi.ICustomizers.IStringUpdaterWithContextInfo
    {

        public void XmlDocumentUpdateWithContextInfo(XmlDocument xmlDocument, object contextInfo)
        {
            if (Globals.IsMercatorPenguinServer)
            {
                // De standaard stackLayout zoeken
                XmlNode parentStackLayout = xmlDocument.FindNodesRecursive(n => (n.Name == "StackLayout")).First();

                //Een horizontale stacklayout definiëren
                XmlNode newStackLayout = xmlDocument.CreateNode(XmlNodeType.Element, "StackLayout", null);
                parentStackLayout.AppendChild(newStackLayout);
                XmlAttribute attrHorizontal = xmlDocument.CreateAttribute("Orientation");
                attrHorizontal.Value = "Horizontal";
                newStackLayout.Attributes.SetNamedItem(attrHorizontal);

                // een nieuw Label definiëren en toevoegen aan de StackLayout
                XmlNode label = xmlDocument.CreateNode(XmlNodeType.Element, "Label", null);
                XmlAttribute attrText = xmlDocument.CreateAttribute("Text");
                attrText.Value = "NL :";
                label.Attributes.SetNamedItem(attrText);
                XmlAttribute attrFont = xmlDocument.CreateAttribute("Font");
                attrFont.Value = "Medium";
                label.Attributes.SetNamedItem(attrFont);
                XmlAttribute attrMargin = xmlDocument.CreateAttribute("Margin");
                attrMargin.Value = "10,5,25,0";
                label.Attributes.SetNamedItem(attrMargin);
                XmlAttribute attrVerticalOptions = xmlDocument.CreateAttribute("VerticalOptions");
                attrVerticalOptions.Value = "Center";
                label.Attributes.SetNamedItem(attrVerticalOptions);
                newStackLayout.AppendChild(label);

                // een nieuwe EditEntry definiëren en toevoegen aan de StackLayout
                XmlNode entry = xmlDocument.CreateNode(XmlNodeType.Element, "m", "EditEntry", "clr-namespace:MercatorPenguin.Controls.Edit;assembly=MercatorPenguin.dll");
                XmlAttribute attrSource = xmlDocument.CreateAttribute("Source");
                attrSource.Value = "s_modelen";
                entry.Attributes.SetNamedItem(attrSource);
                XmlAttribute attrHorizontalOptions = xmlDocument.CreateAttribute("HorizontalOptions");
                attrHorizontalOptions.Value = "FillAndExpand";
                entry.Attributes.SetNamedItem(attrHorizontalOptions);
                MercatorUi.Sig.Sig sigStock = MercatorUi.Sig._SigsStatic.SigByModule(MercatorUi.Sig._SigEnum.STOCK);
                XmlAttribute attrMaxLength = xmlDocument.CreateAttribute("MaxLength");
                attrMaxLength.Value = sigStock.FieldList["S_MODELEN"].Length.ToString();
                entry.Attributes.SetNamedItem(attrMaxLength);
                newStackLayout.AppendChild(entry);
            }
        }

        public string StringUpdate(string s, object contextInfo)
        {
            if (Globals.IsMercatorPenguinServer)
            {
                MercatorUi.Sig.Tools.SigRecordDescriptor sigRecordDescriptor = (MercatorUi.Sig.Tools.SigRecordDescriptor)contextInfo;
                s = s.Replace(" from ", ",s_modelen from ");
            }
            return s;
        }
    }
}