using System;
using System.Web.Mvc;
using Service;
using Domain;

namespace WebSite.Controllers
{
    [HandleError]
    public class BaseController : Controller
    {
        public static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BaseController));
        public ISysSystemLogsManager SysSystemLogsManager { get; set; }

        protected override void HandleUnknownAction(string actionName)
        {
            this.View(actionName).ExecuteResult(this.ControllerContext);
        }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
        }

        protected Language Language
        {
            get
            {
                //语言判断
                int? isCN = Session["isCN"] as int?;
                if (!isCN.HasValue)
                {
                    isCN = 0;
                    Session["isCN"] = isCN;
                }
                switch (isCN)
                {
                    case 1:
                        return Language.EN;
                    case 2:
                        return Language.ZH_TW;
                    default:
                        return Language.CN;
                }
            }
        }
        /// <summary>
        /// 当前酒店ID
        /// </summary>
        protected int CurrentHotelID
        {
            get
            {
                if (Session["CurrentHotelID"] == null)
                {
                    return 0;
                }
                return (int)Session["CurrentHotelID"];
            }
            set
            {
                Session["CurrentHotelID"] = value;
            }
        }

        protected string CurrentHotelCode
        {
            get
            {
                if (Session["CurrentHotelCode"] == null)
                {
                    return "";
                }
                return Session["CurrentHotelCode"].ToString();
            }
            set
            {
                Session["CurrentHotelCode"] = value;
            }
        }
        /// <summary>
        /// 当前酒店名称
        /// </summary>
        protected string CurrentHotelName
        {
            get
            {
                if (Session["CurrentHotelName"] == null)
                {
                    return "";
                }
                return (string)Session["CurrentHotelName"];
            }
            set
            {
                Session["CurrentHotelName"] = value;
            }
        }
        /// <summary>
        /// 当前酒店logo路径
        /// </summary>
        protected string CurrentLogoPath
        {
            get
            {
                if (Session["CurrentLogoPath"] == null)
                {
                    return "../Uploads/logo/1001.png";
                }
                return (string)Session["CurrentLogoPath"];
            }
            set
            {
                Session["CurrentLogoPath"] = value;
            }
        }
        /// <summary>
        /// 当前酒店是否到期提醒
        /// </summary>
        protected bool CurrentHotelIsExpire
        {
            get
            {
                if (Session["CurrentHotelIsExpire"] == null)
                {
                    return true;
                }
                return Convert.ToBoolean(Session["CurrentHotelIsExpire"]);
            }
            set
            {
                Session["CurrentHotelIsExpire"] = value;
            }
        }
        /// <summary>
        /// 记录操作日志
        /// </summary>
        /// <param name="authorityID">权限ID</param>
        /// <param name="action">动作</param>
        /// <param name="detail">详细</param>
        /// <param name="result">操作结果</param>
        /// <param name="account">帐号</param>
        /// <param name="hotelID">所属酒店ID</param>
        protected void SaveSystemLog(int authorityID, string action, string detail, bool result = true, string account = "", int hotelID = 1)
        {
            try
            {
                Domain.SysSystemLogs entity = new Domain.SysSystemLogs();
                entity.ID = 0;
                entity.AuthorityID = authorityID;
                entity.Action = action;
                entity.Detail = detail;
                entity.Result = result ? HttpContext.InnerLanguage("OperationSuccess") : HttpContext.InnerLanguage("OperationFailed");
                entity.Account = string.IsNullOrEmpty(this.User.Identity.Name) ? account : this.User.Identity.Name;
                entity.IP = GetClientIP();
                entity.Time = DateTime.Now;
                entity.HotelID = CurrentHotelID == 0 ? hotelID : CurrentHotelID;

                SysSystemLogsManager.Save(entity);
            }
            catch (Exception) { }
        }

        protected string GetClientIP()
        {
            return Common.Tools.GetClientIP(Request);
        }
        /// <summary>
        /// 根据语言返回对应名称
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ename"></param>
        /// <param name="twname"></param>
        /// <returns></returns>
        protected string ReturnNameByLanguage(string name, string ename, string twname)
        {
            //语言判断
            int? isCN = Session["isCN"] as int?;
            if (!isCN.HasValue)
            {
                isCN = 0;
                Session["isCN"] = isCN;
            }
            switch ((int)Session["isCN"])
            {
                case 1:
                    return ename;
                case 2:
                    return twname;
                default:
                    return name;
            }
        }
    }
}
