using System;
using Whir.Domain;
using Whir.ezEIP.Web;
using Whir.Framework;
using Whir.Service;
using Whir.Config;
using Whir.Config.Models;
using Whir.Language;
using System.Text.RegularExpressions;


public partial class Whir_System_Handler_Config_SeoSetting : SysHandlerPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var action = RequestUtil.Instance.GetFormString("_action");
        Exec(this, action);
    }


    /// <summary>
    /// 获取数据
    /// </summary>
    /// <returns></returns>
    public HandlerResult GetSeoSeting()
    {
        int siteId = RequestUtil.Instance.GetFormString("siteId").ToInt(0);
        SiteInfo siteInfo = ServiceFactory.SiteInfoService.SingleOrDefault<SiteInfo>(siteId);
        if (siteInfo == null)
        {
            siteInfo = ServiceFactory.SiteInfoService.SingleOrDefault<SiteInfo>(CurrentSiteID);
        }
        return new HandlerResult { Status = true, Message = siteInfo.ToJson() };

    }
    /// <summary>
    /// 保存数据
    /// </summary>
    /// <returns></returns>
    public HandlerResult SaveSeoSetting()
    {
        int siteId = RequestUtil.Instance.GetFormString("Site").ToInt(0);
        try
        {
            SiteInfo siteInfo = ServiceFactory.SiteInfoService.SingleOrDefault<SiteInfo>(siteId) ?? ModelFactory<SiteInfo>.Insten();
            var type = typeof(SiteInfo);
            siteInfo = GetPostObject(type, siteInfo) as SiteInfo;
            ServiceFactory.SiteInfoService.Save(siteInfo);

            //记录操作日志
            ServiceFactory.SiteInfoService.SaveLog(siteInfo, "update");
            return new HandlerResult { Status = true, Message = "操作成功".ToLang() };
        }
        catch (Exception ex)
        {
            return new HandlerResult { Status = false, Message = "操作失败：".ToLang() + ex.Message };
        }

    }

    /// <summary>
    /// 保存数据
    /// </summary>
    /// <returns></returns>
    public HandlerResult SaveSysSetting()
    {
        try
        {
            string editorCode = RequestUtil.Instance.GetFormString("EditorCode");

            SystemConfig sysSetting = ConfigHelper.GetSystemConfig();
            var type = typeof(SystemConfig);
            sysSetting = GetPostObject(type, sysSetting) as SystemConfig;
            XmlUtil.SerializerObject(ConfigHelper.GetAppConfigPath("SystemConfig.config"), type, sysSetting);

            //设置Ewebeditor的授权码
            string newLicense = editorCode.Trim();
            string sFileName = AppName + "Editor/EWebEditor/ASPX/config.aspx";
            string configText = EWebEditorHelper.ReadFile(sFileName);
            string mapPath = Server.MapPath(sFileName);
            EWebEditorHelper.ModifyLicense(newLicense, configText, mapPath);

            if (sysSetting != null)
                ServiceFactory.OperateLogService.Save(
                    string.Format("修改系统配置，访问DNS【{0}】，是否打开日志【{1}】", sysSetting.DNS,
                        sysSetting.OpenLog));
        }
        catch (Exception ex)
        {
            return new HandlerResult { Status = false, Message = "操作失败：".ToLang() + ex.Message };
        }
        return new HandlerResult { Status = true, Message = "操作成功".ToLang() };
    }

    /// <summary>
    /// 保存数据 快捷菜单
    /// </summary>
    /// <returns></returns>
    public HandlerResult SaveQuickMenuSetting()
    {
        try
        {
            string quickMenuName = RequestUtil.Instance.GetFormString("QuickMenuName").TrimEnd(',');
            string quickMenuIcon = RequestUtil.Instance.GetFormString("QuickMenuIcon").TrimEnd(',');
            string quickMenuUrl = RequestUtil.Instance.GetFormString("QuickMenuUrl").TrimEnd(',');
             
            SystemConfig sysSetting = ConfigHelper.GetSystemConfig();
            var type = typeof(SystemConfig);
            sysSetting = GetPostObject(type, sysSetting) as SystemConfig;

            XmlUtil.SerializerObject(ConfigHelper.GetAppConfigPath("SystemConfig.config"), type, sysSetting);
            
            if (sysSetting != null)
                ServiceFactory.OperateLogService.Save(
                    string.Format("修改快捷菜单", sysSetting.DNS, sysSetting.OpenLog));
        }
        catch (Exception ex)
        {
            return new HandlerResult { Status = false, Message = "操作失败：".ToLang() + ex.Message };
        }
        return new HandlerResult { Status = true, Message = "操作成功".ToLang() };
    }

    public HandlerResult SaveSubjectSeo()
    {
        try
        {
            int subjectId = RequestUtil.Instance.GetFormString("SubjectId").ToInt();
            Subject subject = ServiceFactory.SubjectService.SingleOrDefault<Subject>(subjectId) ??
                              ModelFactory<Subject>.Insten();
            var type = typeof(Subject);
            subject = GetPostObject(type, subject) as Subject;
            ServiceFactory.SubjectService.Save(subject);
        }
        catch (Exception ex)
        {
            return new HandlerResult { Status = false, Message = "操作失败：".ToLang() + ex.Message };
        }
        return new HandlerResult { Status = true, Message = "操作成功".ToLang() };
    }
}