using PushPlatform.Data.MvcModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PushPlatform.Data;
using PushPlatform.Data.Bll;
using PushPlatform.Data.Criteria;
using PushPlatform.Common.Util;
using PushPlatform.Common.Enumerate;
using PushPlatform.Background.DropDown;

namespace PushPlatform.Background.Areas.BackGround.Controllers
{
    public class ProjectController : BaseController
    {
        BllProjectInfo bllProInfo = new BllProjectInfo();
        BllSysSendCfg bllSendCfg = new BllSysSendCfg();
        #region 项目列表
        public ActionResult Index()
        {
            CurrentUserModel user = BllSysUser.GetCurrentUser();
            return View(user.ChargeProject);
        }
        #endregion

        #region 选择项目保存
        public ActionResult Save(string projectId)
        {
            CurrentUserModel user = BllSysUser.GetCurrentUser();
            ProjectInfo project = bllProInfo.Get(projectId);
            BllSysUser.ChangeProject(user.UserId, user.LoginName, project.Id, project.ProjectName);
            return RedirectToAction("Index", "ChildProject");
        }
        #endregion

        #region 切换项目加载
        [HttpGet]
        public ActionResult ChangeProject()
        {
            CurrentUserModel user = BllSysUser.GetCurrentUser();
            return PartialView(user.ChargeProject);
        }
        #endregion

        #region 项目编辑

        #region 项目编辑主页
        /// <summary>
        /// 项目编辑主页
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <returns></returns>
        public ActionResult ProjectInfo(string projectId)
        {
            ProjectInfo projectModel = null;
            if(!string.IsNullOrEmpty(projectId))
            {
                projectModel = bllProInfo.Get(projectId);
            }
            if (projectModel == null)
            {
                projectModel = new ProjectInfo();
            }
            ViewData["CONNECTION_LIST"] = DropDownManager.DbConnectDDL();
            return PartialView(projectModel);
        }
        #endregion

        #region 项目基本信息编辑
        /// <summary>
        /// 基本信息编辑页面
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <returns></returns>
        public ActionResult ProjectEdit(string projectId)
        {
            var projectModel = bllProInfo.Get(projectId);
            if(projectModel == null)
            {
                projectModel = new ProjectInfo();
            }
            ViewData["CONNECTION_LIST"] = DropDownManager.DbConnectDDL();
            return PartialView(projectModel);
        }

        /// <summary>
        /// 保存项目基本信息
        /// </summary>
        /// <param name="pModel">数据对象</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ProjectEditSave(ProjectInfo pModel)
        {
            JsonResult jr = new JsonResult();
            string userId = BllSysUser.GetCurrentUser().UserId;
            try
            {
                ProjectInfo proModel = null;
                if(!string.IsNullOrEmpty(pModel.Id))
                {
                    proModel = bllProInfo.Get(pModel.Id);
                    proModel.Update_Time = DateTime.Now;
                    proModel.Update_User = userId;
                    proModel.IsEdit = true;
                }
                else
                {
                    proModel = new ProjectInfo();
                    proModel.IsEdit = false;
                    proModel.Id = Guid.NewGuid().ToString();
                    proModel.Create_Time = DateTime.Now;
                    proModel.Create_User = userId;
                    proModel.Is_Deleted = 0;
                    proModel.Timestamp = DateTimeHelper.GetCurrentTimestamp();

                }
                proModel.ProjectName = pModel.ProjectName;
                proModel.ProjectType = pModel.ProjectType;
                proModel.InternalPrincipal = pModel.InternalPrincipal;
                proModel.InternalPrincipalMobile = pModel.InternalPrincipalMobile;
                proModel.Db_ConnectId = pModel.Db_ConnectId;
                proModel.Object_Sql = pModel.Object_Sql;
                proModel.Child_ProjectInfo = pModel.Child_ProjectInfo;
                proModel.User_Sql = pModel.User_Sql;
                List<ProjectInfo> pInfoList = new List<ProjectInfo>();
                pInfoList.Add(proModel);
                if(bllProInfo.SaveOrUpdate(pInfoList)>0)
                {
                    jr.Data = new { result = 1, msg = "保存成功" };
                }
                else
                {
                    jr.Data = new { result = 0, msg = "保存失败！" };
                }
            }
            catch(Exception ex)
            {
                LogHelper.Error(ex);
                jr.Data = new { result = 0, msg = "服务器异常，请联系管理员！" };
            }
            return jr;
        }
        #endregion

        #region 项目各种推送配置
        /// <summary>
        /// 项目各种推送配置页面
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <param name="type">推送类型</param>
        /// <returns></returns>
        public ActionResult SendCfgPage(string projectId, int type)
        {
            SysSendCfg sendModel = null;
            sendModel = bllSendCfg.Fetch(new CriteriaSysSendCfg { EqProjectid = projectId, EqType = type, EqIsDeleted = 0 }).FirstOrDefault();
            if (sendModel == null)
            {
                sendModel = new SysSendCfg();
            }
            switch(type)
            {
                case (int)SendCfgType.短信:
                    return PartialView("SmsCfgPage", sendModel);
                case (int)SendCfgType.APP:
                    return PartialView("AppCfgPage", sendModel);
                case (int)SendCfgType.微信:
                    return PartialView("WechatCfgPage", sendModel);
                case (int)SendCfgType.邮件:
                    return PartialView("MailCfgPage", sendModel);
            }
            return PartialView("SmsCfgPage", sendModel);
        }

        #region 项目各种推送配置保存
        /// <summary>
        /// 项目各种推送配置保存
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <param name="type">推送类型</param>
        /// <returns></returns>
        public JsonResult SendCfgSave(SysSendCfg sendModel)
        {
            JsonResult jr = new JsonResult();
            try
            {
                CurrentUserModel user = BllSysUser.GetCurrentUser();
                SysSendCfg saveModel = null;
                if(!string.IsNullOrEmpty(sendModel.Id))
                {
                    saveModel = bllSendCfg.Get(sendModel.Id);
                    saveModel.Update_Time = DateTime.Now;
                    saveModel.Update_User = user.UserId;
                    saveModel.IsEdit = true;
                }
                else
                {
                    saveModel = new SysSendCfg();
                    saveModel.Id = Guid.NewGuid().ToString();
                    saveModel.ProjectId = user.CurrentProjectId;
                    saveModel.Create_Time = DateTime.Now;
                    saveModel.Create_User = user.UserId;
                    saveModel.Is_Deleted = 0;
                    saveModel.Timestamp = DateTimeHelper.GetCurrentTimestamp();
                }
                saveModel.Config_Info = sendModel.Config_Info;
                saveModel.OriginalId = sendModel.OriginalId;
                saveModel.Type = sendModel.Type;
                List<SysSendCfg> sendList = new List<SysSendCfg>();
                sendList.Add(saveModel);
                if (bllSendCfg.SaveOrUpdate(sendList) > 0)
                {
                    jr.Data = new { result = 1, msg = "保存成功" };
                }
                else
                {
                    jr.Data = new { result = 0, msg = "保存失败！" };
                }
            }
            catch(Exception ex)
            {
                LogHelper.Error(ex);
                jr.Data = new { result = 0, msg = "服务器异常，请联系管理员！" };
            }
            return jr;
        }
        #endregion

        #endregion

        #endregion
    }
}