using DbCommon.Enties.DbModels;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Common.Function
{
    public class ColorSetting
    {
        private static ColorSetting m_instance;

        private Dictionary<LocationStatus, Color> m_dict = new Dictionary<LocationStatus, Color>();

        public static ColorSetting Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new ColorSetting();
                    if (!m_instance.Load())
                    {
                        return null;
                    }
                }
                return m_instance;
            }
        }

        public Color GetColor(LocationStatus status)
        {
            if (m_dict.ContainsKey(status))
            {
                return m_dict[status];
            }
            return Color.FromKnownColor(KnownColor.Control);
        }

        private bool Load()
        {
            try
            {
                var arrayValues = Enum.GetValues(typeof(LocationStatus));
                foreach (var item in arrayValues)
                {
                    LocationStatus status = (LocationStatus)item;
                    string name = status.ToString();

                    string colorStr = AppSetting.TryGetValue<string>(name);
                    Color color = ColorTranslator.FromHtml(colorStr);

                    if (!m_dict.ContainsKey(status))
                    {
                        m_dict.Add(status, color);
                    }
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}
