using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace TFSWrappers
{
    public class BuildTypeProperty
    {
        public BuildTypeProperty()
        {
        }

        public BuildTypeProperty(XmlDocument xmlDocument, XmlNamespaceManager xmlNamespaceManager, string xPath, string defaultValue, string title, string helpString)
        {
            this.xmlDocument = xmlDocument;
            this.xmlNamespaceManager = xmlNamespaceManager;
            xmlNode = xmlDocument.SelectSingleNode(xPath, xmlNamespaceManager);
            exists = xmlNode != null;
            if (exists)
            {
                propertyValue = xmlNode.InnerText;
            }
            else
            {
                propertyValue = String.Empty;
            }
            xmlNodeName = xPath.Substring(xPath.LastIndexOf('/') + 4);
            xmlNodeParentXPath = xPath.Substring(0, xPath.LastIndexOf('/'));
            this.defaultValue = defaultValue;
            this.helpString = helpString;
            this.title = title;
        }

        private XmlDocument xmlDocument;
        private XmlNamespaceManager xmlNamespaceManager;
        private XmlNode xmlNode;
        private string xmlNodeName;
        private string xmlNodeParentXPath;

        private string title;

        public string Title
        {
            get { return title; }
        }

        private bool exists;

        public bool Exists
        {
            get { return exists; }
        }

        private string propertyValue;

        public string PropertyValue
        {
            get 
            { 
                return propertyValue; 
            }
            set 
            { 
                if (exists)
                {
                    if (propertyValue != value)
                    {
                        xmlNode.InnerText = value;
                    }
                }
                else
                {
                    if (defaultValue != value)
                    {
                        propertyValue = value;
                        xmlNode = xmlDocument.CreateNode(XmlNodeType.Element, xmlNodeName, Constants.NamespaceUri);
                        xmlNode.InnerText = value;
                        XmlNode parentXmlNode = xmlDocument.SelectSingleNode(xmlNodeParentXPath, xmlNamespaceManager);
                        parentXmlNode.AppendChild(xmlNode);
                        exists = true;
                    }
                }
            }
        }

        private string defaultValue;

        public string DefaultValue
        {
            get { return defaultValue; }
            set { defaultValue = value; }
        }

        /*public string Value
        {
            get 
            {
                if (exists)
                {
                    return xmlNode.InnerText;
                }
                else
                {
                    return String.Empty;
                }
            }
            set 
            {
                if (exists)
                {
                    xmlNode.InnerText = value;
                }
                else
                {
                    xmlNode = xmlDocument.CreateNode(XmlNodeType.Element, xmlNodeName, Constants.NamespaceUri);
                    XmlNode parentXmlNode = xmlDocument.SelectSingleNode(xmlNodeParentXPath, xmlNamespaceManager);
                    parentXmlNode.AppendChild(xmlNode);
                    exists = true;
                }
            }
        }*/

        public void Remove()
        {
            if (exists)
            {
                XmlNode parentNode = xmlNode.ParentNode;
                parentNode.RemoveChild(xmlNode);
                exists = false;
            }
        }

        private string helpString;

        public string HelpString
        {
            get { return helpString; }
            set { helpString = value; }
        }
    }
}
