using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using Newtonsoft.Json;
using Wizdom365.BLL.IoC;
using Wizdom365.BLL.Managers;
using Wizdom365.BLL.Managers.Implementation;

namespace GUI.Web.Base.Bundles
{
    public partial class Translations : Page
    {
        private readonly IWebResourceManager _webResourceManager;

        public Translations()
            : this(Ioc.Resolve<IWebResourceManager>())
        {
        }

        public Translations(IWebResourceManager webResourceManager)
        {
            _webResourceManager = webResourceManager;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //var result = new Dictionary<string, Dictionary<string, string>>();
            //translationManager.GetTranslations().ToList().ForEach(translation =>
            //{
            //    try
            //    {
            //        foreach (var languageValue in translation.LanguageValues)
            //        {
            //            if (!result.ContainsKey(languageValue.Key))
            //            {
            //                var translations = new Dictionary<string, string>() { { translation.Key, languageValue.Value.CustomizedValue ?? languageValue.Value.Value } };
            //                result.Add(languageValue.Key, translations);
            //            }
            //            else {
            //                var languageTranslations = result.FirstOrDefault(x => x.Key == languageValue.Key);
            //                languageTranslations.Value.Add(translation.Key, languageValue.Value.CustomizedValue ?? languageValue.Value.Value);
            //            }
            //        }


            //        //foreach (var language in t.LanguageValues.Keys.ToList())
            //        //{
            //        //    if (!translations.ContainsKey(language))
            //        //        translations.Add(language, new Dictionary<string, string>());

            //        //    translations[language].Add(t.Key, t.LanguageValues[language]);
            //        //}                    
            //    }
            //    catch (Exception)
            //    {
            //        Ioc.Resolve<ILogger>().Warn("translation key already exists", translation);
            //    }
            //});
            Response.ContentType = "application/javascript";

            string translationsObj = _webResourceManager.GetTranslations();

			Response.Write(translationsObj);

			//Response.Write("angular.module('Wizdom365.Translations',[]).value('translations'," +
			//               JsonConvert.SerializeObject(result) + ");");
		}
    }
}