
using PageAdmin.Common;
using PageAdmin.Utils;
using PageAdmin.Web.Filter;
using System.Web.Mvc;
using PageAdmin.BLL;
using System.Configuration;

namespace PageAdmin.Web.Areas.Admin.Controllers

{
    public class SysSetController: BaseController
    {

        public SysSetController():base("System/Configure/")
        { 

        }

        [HttpGet]
        public virtual ActionResult Index()
        {
            ViewBag.SystemConfig=JsonHelper.JsonParse(XmlHelper.AllNodeValue("configuration", "/Config/System.config"));
            ViewBag.DomainConstraint = ConfigurationManager.AppSettings["DomainConstraint"];
            ViewBag.WaterMarkConfig = JsonHelper.JsonParse(XmlHelper.AllNodeValue("configuration", "/Config/WaterMark.config"));
            ViewBag.EmailConfig = JsonHelper.JsonParse(XmlHelper.AllNodeValue("configuration", "/Config/Email.config"));
            ViewBag.SmsConfig = JsonHelper.JsonParse(XmlHelper.AllNodeValue("configuration", "/Config/Sms.config"));
            ViewBag.EditorConfig = JsonHelper.JsonParse(XmlHelper.AllNodeValue("configuration", "/Config/Editor.config"));
            ViewBag.SendCodeTemplateConfig = JsonHelper.JsonParse(XmlHelper.AllNodeValue("configuration", "/Config/SendCodeTemplate.config"));
            ViewBag.FileConfig = JsonHelper.JsonParse(XmlHelper.AllNodeValue("configuration", "/Config/File.config"));
            return View();
        }


        [ValidateInput(false)]
        [AjaxOnly]
        [HttpPost]
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public JsonResult Index(FormCollection form)
        {
            TipsInfo tipsInfo = new TipsInfo();
            string accessKeyId = form["System_AccessKeyId"].Trim();
            string accessKeySecret = form["System_AccessKeySecret"].Trim();
            if(!string.IsNullOrEmpty(accessKeyId))
            {
                if (accessKeyId.Length != 18 || accessKeySecret.Length != 32)
                {
                    tipsInfo.State = 0;
                    tipsInfo.Msg = "AccessKeyId或AccessKeySecret填写错误，如未获取授权，请务必留空！";
                    return Json(tipsInfo);
                }
            }

            form["System_Domain"] = StringHelper.DomainFormat(form["System_Domain"]);
            form["System_MemberDomain"] = StringHelper.DomainFormat(form["System_MemberDomain"]);

            Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
            config.AppSettings.Settings["DomainConstraint"].Value = form["DomainConstraint"];
            config.Save(ConfigurationSaveMode.Modified);

            XmlHelper.UpdateNodeValue("configuration", form, "/Config/System.config", "System_");
            XmlHelper.UpdateNodeValue("configuration", form, "/Config/File.config", "File_");
            XmlHelper.UpdateNodeValue("configuration", form, "/Config/WaterMark.config", "WaterMark_");
            XmlHelper.UpdateNodeValue("configuration", form, "/Config/Email.config", "Email_");
            XmlHelper.UpdateNodeValue("configuration", form, "/Config/Sms.config", "Sms_");
            XmlHelper.UpdateNodeValue("configuration", form, "/Config/Editor.config", "Editor_");
            XmlHelper.UpdateNodeValue("configuration", form, "/Config/SendCodeTemplate.config", "SendCodeTemplate_");
            SystemConfig.RefreshConfig();
            UpdateEditorJson();
            return Json(tipsInfo);
        }


        //更新百度编辑器ueditor的json文件
        private void UpdateEditorJson()
        {
            string jsonTemplate = "/Incs/UEditor/net/configTemplate.txt";
            string templateContent = IOHelper.ReadFile(jsonTemplate);

            string editorImageExt =StringHelper.Format<string>(Request.Form["Editor_ImageExt"]);
            int editorImageMaxSize = StringHelper.Format<int>(Request.Form["Editor_ImageMaxSize"]);
            editorImageExt = "\""+string.Join("\",\"", editorImageExt.Split(','))+ "\"";


            string editorMediaExt = StringHelper.Format<string>(Request.Form["Editor_MediaExt"]);
            int editorMediaMaxSize = StringHelper.Format<int>(Request.Form["Editor_MediaMaxSize"]);
            editorMediaExt = "\"" + string.Join("\",\"", editorMediaExt.Split(',')) + "\"";


            string editorFileExt = StringHelper.Format<string>(Request.Form["Editor_FileExt"]);
            int editorFileMaxSize = StringHelper.Format<int>(Request.Form["Editor_FileMaxSize"]);
            editorFileExt = "\"" + string.Join("\",\"", editorFileExt.Split(',')) + "\"";

            templateContent = templateContent.Replace("{imageMaxSize}", (editorImageMaxSize * 1024).ToString());
            templateContent = templateContent.Replace("{imageAllowFiles}",editorImageExt);

            templateContent = templateContent.Replace("{videoMaxSize}", (editorMediaMaxSize * 1024).ToString());
            templateContent = templateContent.Replace("{videoAllowFiles}", editorMediaExt);

            templateContent = templateContent.Replace("{fileMaxSize}", (editorFileMaxSize * 1024).ToString());
            templateContent = templateContent.Replace("{fileAllowFiles}", editorFileExt);

            string jsonPath = "/Incs/UEditor/net/config.json";
            IOHelper.UpdateFile(jsonPath, templateContent);
        }


        [HttpGet]
        public ActionResult EmailTest()
        {
            return View();
        }

        [AjaxOnly]
        [HttpPost]
        public JsonResult EmailTest(FormCollection form)
        {
            var tipsInfo = Common.EmailHelper.SendEmail(StringHelper.Format<string>(form["email"]), StringHelper.Format<string>(form["title"]), StringHelper.Format<string>(form["content"]));
            return Json(tipsInfo);
        }

        [HttpGet]
        public ActionResult SmsTest()
        {
            return View();
        }
        [AjaxOnly]
        [HttpPost]
        public JsonResult SmsTest(FormCollection form)
        {
            var tipsInfo = Common.SmsHelper.SendSms(StringHelper.Format<string>(form["mobile"]), StringHelper.Format<string>(form["content"]), StringHelper.Format<string>(form["paramString"]));
            return Json(tipsInfo);
        }

    }
}