using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Configuration;
using QMAPP.ServicesAgent;

namespace QMAPP.MD.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    /// <summary>
    /// 应用程序对象
    /// </summary>
    public class MvcApplication : System.Web.HttpApplication
    {
        /// <summary>
        /// 主应用路径
        /// </summary>
        public static string ManageCenterRoot { get; set; }

        /// <summary>
        /// 数据分页尺寸
        /// </summary>
        public static int PageSize { get; set; }

        /// <summary>
        /// 应用程序物理路径
        /// </summary>
        public static string PhysicsRootPath { get; set; }

        /// <summary>
        /// 临时文件路径
        /// </summary>
        public static string TempPath { get; set; }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Login", action = "Login", id = UrlParameter.Optional, }, // Parameter defaults
                new string[] { "QMAPP.MD.Web.Controllers" }
            );

        }

        /// <summary>
        /// 应用启动
        /// </summary>
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();            

            //配置日志处理组件
            QMFrameWork.Log.LogManager.Configure("log4net");
            try
            {
                //设置主应用路径
                ManageCenterRoot = ConfigurationManager.AppSettings["ManageCenterRoot"].ToString();
                QMAPP.Common.Web.Controllers.QController.ManageCenterRoot = ManageCenterRoot;                

                //应用程序物理路径
                PhysicsRootPath = Server.MapPath(HttpRuntime.AppDomainAppVirtualPath);
                QMAPP.Common.Web.Controllers.QController.PhysicsRootPath = PhysicsRootPath;

                //临时文件路径
                TempPath = PhysicsRootPath + "Temp\\";

                if (System.IO.Directory.Exists(PhysicsRootPath + "Temp") == false)
                {
                    System.IO.Directory.CreateDirectory(PhysicsRootPath + "Temp");
                }

                //设置文件操作路径
                QMFrameWork.WebUI.Util.IEFileTool.TempPath = TempPath;
                QMFrameWork.Common.ExcelOperation.IEExcelHelper.FilePath = Server.MapPath("~/App_Data/Excel/");
                QMAPP.Common.Web.Controllers.PrintController.TemplatePath = Server.MapPath("~/App_Data/Print/");               

                //多语言
                QMFrameWork.Common.MLanguage.LanguageHelper.Config();

                //设置数据分页尺寸
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["PageSize"]) == false)
                    PageSize = int.Parse(ConfigurationManager.AppSettings["PageSize"]);
                else
                    PageSize = 15;

                //加载静态基础数据
                QMFrameWork.Common.Util.ModelDictionaryHandler.Configure();

                //设置菜单文件路径
                QMFrameWork.WebUI.Menu.MenuHelper.MenuFilePath = Server.MapPath("~/App_Data/Menu.xml");

                //初始化服务
                ServiceAgent.Init();                

                //记录日志
                QMFrameWork.Log.LogManager.LogHelper.Info(
                    new QMFrameWork.Log.LogInfo { ClientIP = "localhost", UserName = "admin", Info = "应用启动"});

            }
            catch (Exception ex)
            {
                if (QMFrameWork.Log.LogManager.LogHelper != null)
                {
                    QMFrameWork.Log.LogManager.LogHelper.Error(
                        new QMFrameWork.Log.LogInfo { ClientIP = "localhost", UserName = "admin", Info = "应用启动",ErrorInfo=ex });
                }
                throw ex;
            }
            RegisterRoutes(RouteTable.Routes);
        }

        
        /// <summary>
        /// 应用停止
        /// </summary>
        protected void Application_End()
        {
        }
    }
}