using System;
using System.Xml;

namespace Creditcall.ChipDna.Client
{
    internal class ExtraParameter
    {
        public ExtraParameter(XmlNode paramNode) : 
            this(paramNode.InnerText,
                 ParseAttributeValue(paramNode, "description"),
                 ParseAttributeValue(paramNode, "default"),
                 ParseTypeValue(paramNode))
        {
        }

        private static string ParseAttributeValue(XmlNode param, string attributeName)
        {
            if (param.Attributes == null || param.Attributes[attributeName] == null)
            {
                return null;
            }
            return param.Attributes[attributeName].Value;
        }

        private static bool ParseTypeValue(XmlNode param)
        {
            string type = ParseAttributeValue(param, "type");
            if(type == null) 
            {
                throw new InvalidOperationException("'type' attribute is not optional");
            }
            switch (type)
            {
                case "preset": return false;
                case "input": return true;
                default: throw new InvalidOperationException("'type' attribute must be either 'preset' or 'input'");
            }
        }

        public ExtraParameter(string key, string description, string defaultValue, bool getInput)
        {
            this.key = key;
            this.description = description;
            this.defaultValue = defaultValue;
            this.getInput = getInput;
        }

        public string Key
        {
            get { return key; }
        }

        public string Description
        {
            get { return description; }
        }

        public string DefaultValue
        {
            get { return defaultValue; }
        }

        public bool GetInput
        {
            get { return getInput; }
        }

        private readonly string description;
        private readonly string key;
        private readonly string defaultValue;
        private readonly bool getInput;
    }
}