﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnumsAnswer1
{
    public enum Numbers
    {
        Ten = 10,
        Twenty = 20,
        Thirty = 30,
        Fourty = 40,
        Fifty = 50,
    }

    public class StringToEnumMapping
    {
        //This implementation also allows to programatically access the mappings e.g. for logging purposes.
        static readonly Dictionary<string, Numbers> NumbersDictionary = new Dictionary<string, Numbers>(StringComparer.InvariantCultureIgnoreCase)
        {
            { "Ten", Numbers.Ten },
            { "Eleven", Numbers.Ten },
            { "Twenty", Numbers.Twenty },
            { "Thirty", Numbers.Thirty },
            { "ThirtyOne", Numbers.Thirty },
            { "ThirtyTwo", Numbers.Thirty },
            { "Fourty", Numbers.Fourty },
            { "Fifty", Numbers.Fifty }
        };

        static Numbers UsingTryGetValue(string key)
        {
            Numbers numbers;
            if (key != null && NumbersDictionary.TryGetValue(key, out numbers))
            {
                //TryGetValue returns '0' when the key was not found, even when there is no corresponding enum.
                return numbers;
            }
            return Numbers.Fourty;
        }

        static Numbers UsingIndexWithCatch(string key)
        {
            Numbers numbers = Numbers.Fourty;
            try
            {
                //Handles KeyNotFoundException and ArgumentNullException at the same time.
                numbers = NumbersDictionary[key];
            }
            catch
            {
                //Intentionally left blank as we already handle the exception by setting a fail safe value.
            }
            return numbers;
        }
    }
}
