﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;

namespace CommanderSharp.DefaultProvider
{
    public class PropertyFileDefaultProvider : IDefaultProvider
    {
        public static readonly string DEFAULT_FILE_NAME = "Resources";
        private ResourceManager m_properties;

        public PropertyFileDefaultProvider()
        {
            init(DEFAULT_FILE_NAME);
        }

        public PropertyFileDefaultProvider(String fileName)
        {
            init(fileName);
        }

        private void init(String fileName)
        {
            try
            {
                m_properties = ResourceManager.CreateFileBasedResourceManager(fileName, (new FileInfo(fileName)).Directory.FullName, null);
            }
            catch (Exception)
            {
                throw new ParameterException("Could not open property file: " + fileName);
            }
        }


        public string GetDefaultValueFor(string optionName)
        {
            int index = 0;
            while (index < optionName.Length && !(Char.IsLetter(optionName[index]) || Char.IsDigit(optionName[index])))
            {
                index++;
            }
            String key = optionName.Substring(index);
            return m_properties.GetString(optionName);
        }
    }
}
