﻿using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Web;
using System.Xml.Linq;

namespace Ovitur.Core
{/// <summary>
    /// Summary description for Utils
    /// </summary>
    public static class Modules
    {
        public static List<ModuleInfo> GetProductModules()
        {
            List<ModuleInfo> moduleList = new List<ModuleInfo>();
            foreach (string subDir in Directory.GetDirectories(HttpContext.Current.Request.MapPath("~/Modules")))
                if (File.Exists(Path.Combine(subDir, "moduleInfo.xml")))
                {

                    XDocument doc = XDocument.Load(Path.Combine(subDir, "moduleInfo.xml"));
                    string enabled = doc.Root.Attribute("enabled").Value;

                    if (bool.Parse(enabled))
                    {
                        //ResourceManager rm = new ResourceManager(subDir, Assembly.GetExecutingAssembly());

                        string menuItemText = (string)HttpContext.GetGlobalResourceObject("Hotel", "MenuItemName");
                        string description = (string)HttpContext.GetGlobalResourceObject("Hotel", "Description");

                        moduleList.Add(new ModuleInfo
                        {
                            MenuItemText = menuItemText,
                            Directory = Path.GetFileName(subDir),
                            Description = description
                        });
                    }
                }
            return moduleList;
        }

        public static string SerializeJSON(object json)
        {
            // Serializar la lista de hoteles a JSON
            MemoryStream memoryStream = new MemoryStream();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(json.GetType());
            serializer.WriteObject(memoryStream, json);
            memoryStream.Position = 0;
            StreamReader sr = new StreamReader(memoryStream);

            return sr.ReadToEnd();
        }
    }

    public class ModuleInfo
    {
        public string MenuItemText { get; set; }
        public string Description { get; set; }
        public string Directory { get; set; }
    }
}