using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain;
using Service;

namespace WebSite.Controllers
{
    public class SysSettingController : BaseController
	{
        private const int AUTHORITY_SysSetting = 52;

		public ISysSettingManager SysSettingManager { get; set; }

		#region Action

		public ActionResult Index()
        {
			return View("SimonIndex");
		}

		[Authorize]
		public ActionResult Save(string jsonData)
		{
            try
            {
                IDictionary<string, string> settings = Newtonsoft.Json.JsonConvert.DeserializeObject<IDictionary<string, string>>(jsonData);

                foreach (var kv in settings)
                {
                    SysSettingManager.SetValue(kv.Key, kv.Value);
                }

                SaveSystemLog(AUTHORITY_SysSetting, HttpContext.InnerLanguage("EditSystemSetting"), "");

                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("SaveSuccess") });
            }
            catch (Exception ex)
            {
                SaveSystemLog(AUTHORITY_SysSetting, HttpContext.InnerLanguage("EditSystemSetting"), ex.Message);

                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("SaveFailed") });
            }
		}

        [Authorize]
        public ActionResult LoadAll()
        {
            var list = SysSettingManager.LoadAll().Where(r => r.AllowEdit);

            IList<object> result = new List<object>();

            foreach (var setting in list)
            {
                switch ((int)Session["isCN"])
                {
                    case 1:
                        {
                            if (setting.ERemark == "Address")
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.ERemark, value = setting.Value, group = setting.EType, editor = new { type = "validatebox", options = new { required = true, validType = "ip" } } });
                            }
                            else if (setting.ERemark == "Port")
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.ERemark, value = setting.Value, group = setting.EType, editor = new { type = "numberbox", options = new { required = true, min = 1, max = 65535 } } });
                            }
                            else if (setting.ERemark == "Synchronization Time")
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.ERemark, value = setting.Value, group = setting.EType, editor = new { type = "timespinner", options = new { required = true } } });
                            }
                            else
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.ERemark, value = setting.Value, group = setting.EType, editor = "text" });
                            }
                        }
                        break;
                    case 2:
                        {
                            if (setting.Remark == "地址")
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.Remark, value = setting.Value, group = setting.Type, editor = new { type = "validatebox", options = new { required = true, validType = "ip" } } });
                            }
                            else if (setting.Remark == "端口")
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.Remark, value = setting.Value, group = setting.Type, editor = new { type = "numberbox", options = new { required = true, min = 1, max = 65535 } } });
                            }
                            else if (setting.Remark == "同步時間")
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.Remark, value = setting.Value, group = setting.Type, editor = new { type = "timespinner", options = new { required = true } } });
                            }
                            else
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.Remark, value = setting.Value, group = setting.Type, editor = "text" });
                            }
                        }
                        break;
                    default:
                        {
                            if (setting.Remark == "地址")
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.Remark, value = setting.Value, group = setting.Type, editor = new { type = "validatebox", options = new { required = true, validType = "ip" } } });
                            }
                            else if (setting.Remark == "端口")
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.Remark, value = setting.Value, group = setting.Type, editor = new { type = "numberbox", options = new { required = true, min = 1, max = 65535 } } });
                            }
                            else if (setting.Remark == "同步时间")
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.Remark, value = setting.Value, group = setting.Type, editor = new { type = "timespinner", options = new { required = true } } });
                            }
                            else
                            {
                                result.Add(new { id = setting.ID, name = setting.Name, remark = setting.Remark, value = setting.Value, group = setting.Type, editor = "text" });
                            }
                        }
                        break;
                }
            }

            return Json(new { total = result.Count, rows = result });
        }

		#endregion

	}
}
