onary(variables);
        }

        private ConditionalSupport(IDictionary<string, VariableValue> variables, IEnumerable<KeyValuePair<string, VariableValue>> extended)
        {
            this._variables = new VariableDictionary(variables);
            VariableUtilities.AddDictionaryToDictionary(this._variables, extended);
        }

        private ConditionalSupport(IDictionary<string, VariableValue> variables, IEnumerable<string> variableNames)
        {
            this._variables = new VariableDictionary(variables);
            // These variables are not defined yet.  So we can't match them against their real value.
            // Give them the value of AnyValue so that at least that value can be used.
            foreach (var name in variableNames)
            {
                this._variables.Add(name, AnyValue.ToVariableValue());
            }
        }

        internal static IConditionalSupport NewEmptyInstance()
        {
            return new ConditionalSupport(new VariableDictionary());
        }

        public bool EnvironmentVariableConditionMatches(string name, string value)
        {
            string actualValue;
            if (!this.TryFindVariableValue(name, out actualValue))
            {
                if (value == Undefined)
                    return true;
                else
                    return false;
            }

            if (value == AnyValue)
                return true;

            if (string.IsNullOrWhiteSpace(actualValue) && value == Empty)
                return true;

            // Check to see if these are meant to be boolean values, if so, then ignore case and padding.
            bool result;
            if (bool.TryParse(value, out result))
                return actualValue.ToUpper().Trim() == result.ToString().ToUpper();

            return actualValue == value;
        }

        public IConditionalSupport CloneAndExtendWithYetToBeDefinedVariables(IEnumerable<string> variableNames)
        {
            if (variableNames == null)
            {
                throw new ArgumentNullException("variableNames");
            }

            return new ConditionalSupport(this._variables, variableNames);
        }

        public IConditionalSupport CloneWithExtendedVariables(IDictionary<string, VariableValue> additionalVariables)
        {
            return new ConditionalSupport(this._variables, additionalVariables);
        }

        private bool TryFindVariableValue(string name, out string value)
        {
            // This seems like the safest behavior to avoid our code misbehaving because of some env var that is set.
            // The down side is it will prevent our behavior from being overridden by setting environment variables
            VariableValue tmp;
            if (this._variables.TryGetValue(name, out tmp))
            {
                value = tmp.Value;
                return true;
            }

            value = Environment.GetEnvironmentVariable(name);

            // null means the variable was not defined
            if (value == null)
            {
                return false;
            }

            return true;
        }
    }
}
