using RFrameGenerate.BLL;
using RFrameGenerate.BLL.WX_BaseCommon;
using RFrameGenerate.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wechat;
using Wechat.Enum;
using Wechat.Model;
using weixinopen;

namespace jiouosdp.Areas.WeiXinBase.Controllers
{
    public class BasicInformationController : Controller
    {
        //
        // GET: /System/BasicInformation/

        [PF(PCode.WeiXinManage.Menu)]
        public ActionResult Index()
        {
            string subscribe, nonematch;
            BaseCommonBLL.GetEventKeyword(out subscribe, out nonematch);
            ViewBag.subscribe = subscribe;
            ViewBag.nonematch = nonematch;
            return View();
        }

        public JsonResult SaveEnvents(string subscribe, string nonematch)
        {
            var result = BaseCommonBLL.SaveEventKeyword(subscribe, nonematch);
            if (result.State == true)
                return Json(new { Result = "OK" });
            else
                return Json(new { Result = "NO", Msg = result.Msg });
        }

        public JsonResult AddTextKeyword(string keyword, string content, string rulename)
        {
            var result = BaseCommonBLL.AddTextKeyword(keyword, content, rulename);
            if (result.State == true)
                return Json(new { Result = "OK" });
            else
                return Json(new { Result = "NO", Msg = result.Msg });
        }
        public JsonResult UpdateTextKeyword(Guid guid, string keyword, string content, string rulename)
        {
            var result = BaseCommonBLL.UpdateTextKeyword(guid, keyword, content, rulename);
            if (result.State == true)
                return Json(new { Result = "OK" });
            else
                return Json(new { Result = "NO", Msg = result.Msg });
        }
        public JsonResult DelTextKeyword(Guid guid)
        {
            var result = BaseCommonBLL.DelTextKeyword(guid);
            if (result.State == true)
                return Json(new { Result = "OK" });
            else
                return Json(new { Result = "NO", Msg = result.Msg });
        }
        public ActionResult Menu()
        {
            return View();
        }
        public JsonResult SaveMenu(T_WX_WeChatMenu menu)
        {
            MenuManage.UpdateMenu(menu);

            MenuMsg menumsg = new MenuMsg();
            #region 第一级菜单
            menumsg.FistMeun = new Menu(menu.name1);
            if (string.IsNullOrEmpty(menu.value1) == true)
            {
                if (string.IsNullOrEmpty(menu.value1_1) != true)
                {
                    menumsg.FistMeun.sub_button = true;
                    menumsg.FistMeun.SubMenu = new List<Menu>();
                    if (menu.value1_1.Contains("http://") == true || menu.value1_1.Contains("https://") == true)
                        menumsg.FistMeun.SubMenu.AddEx(new Menu(menu.name1_1, MenuType.view, menu.value1_1));
                    else
                        menumsg.FistMeun.SubMenu.AddEx(new Menu(menu.name1_1, MenuType.click, menu.value1_1));

                    if (string.IsNullOrEmpty(menu.value1_2) != true)
                    {
                        if (menu.value1_2.Contains("http://") == true || menu.value1_2.Contains("https://") == true)
                            menumsg.FistMeun.SubMenu.AddEx(new Menu(menu.name1_2, MenuType.view, menu.value1_2));
                        else
                            menumsg.FistMeun.SubMenu.AddEx(new Menu(menu.name1_2, MenuType.click, menu.value1_2));

                        if (string.IsNullOrEmpty(menu.value1_3) != true)
                        {
                            if (menu.value1_3.Contains("http://") == true || menu.value1_3.Contains("https://") == true)
                                menumsg.FistMeun.SubMenu.AddEx(new Menu(menu.name1_3, MenuType.view, menu.value1_3));
                            else
                                menumsg.FistMeun.SubMenu.AddEx(new Menu(menu.name1_3, MenuType.click, menu.value1_3));

                            if (string.IsNullOrEmpty(menu.value1_4) != true)
                            {
                                if (menu.value1_4.Contains("http://") == true || menu.value1_4.Contains("https://") == true)
                                    menumsg.FistMeun.SubMenu.AddEx(new Menu(menu.name1_4, MenuType.view, menu.value1_4));
                                else
                                    menumsg.FistMeun.SubMenu.AddEx(new Menu(menu.name1_4, MenuType.click, menu.value1_4));

                                if (string.IsNullOrEmpty(menu.value1_5) != true)
                                {
                                    if (menu.value1_5.Contains("http://") == true || menu.value1_5.Contains("https://") == true)
                                        menumsg.FistMeun.SubMenu.AddEx(new Menu(menu.name1_5, MenuType.view, menu.value1_5));
                                    else
                                        menumsg.FistMeun.SubMenu.AddEx(new Menu(menu.name1_5, MenuType.click, menu.value1_5));
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                menumsg.FistMeun.sub_button = false;
                if (menu.value1.Contains("http://") == true || menu.value1.Contains("https://") == true)
                {
                    menumsg.FistMeun.type = MenuType.view;
                    menumsg.FistMeun.url = menu.value1;
                }
                else
                {
                    menumsg.FistMeun.type = MenuType.click;
                    menumsg.FistMeun.key = menu.value1;
                }
            }
            #endregion

            #region 第二级菜单
            if (string.IsNullOrEmpty(menu.name1) != true)
            {
                menumsg.TwoMeun = new Menu(menu.name2);
                if (string.IsNullOrEmpty(menu.value2) == true)
                {
                    if (string.IsNullOrEmpty(menu.value2_1) != true)
                    {
                        menumsg.TwoMeun.sub_button = true;
                        menumsg.TwoMeun.SubMenu = new List<Menu>();
                        if (menu.value2_1.Contains("http://") == true || menu.value2_1.Contains("https://") == true)
                            menumsg.TwoMeun.SubMenu.AddEx(new Menu(menu.name2_1, MenuType.view, menu.value2_1));
                        else
                            menumsg.TwoMeun.SubMenu.AddEx(new Menu(menu.name2_1, MenuType.click, menu.value2_1));

                        if (string.IsNullOrEmpty(menu.value2_2) != true)
                        {
                            if (menu.value2_2.Contains("http://") == true || menu.value2_2.Contains("https://") == true)
                                menumsg.TwoMeun.SubMenu.AddEx(new Menu(menu.name2_2, MenuType.view, menu.value2_2));
                            else
                                menumsg.TwoMeun.SubMenu.AddEx(new Menu(menu.name2_2, MenuType.click, menu.value2_2));

                            if (string.IsNullOrEmpty(menu.value2_3) != true)
                            {
                                if (menu.value2_3.Contains("http://") == true || menu.value2_3.Contains("https://") == true)
                                    menumsg.TwoMeun.SubMenu.AddEx(new Menu(menu.name2_3, MenuType.view, menu.value2_3));
                                else
                                    menumsg.TwoMeun.SubMenu.AddEx(new Menu(menu.name2_3, MenuType.click, menu.value2_3));

                                if (string.IsNullOrEmpty(menu.value2_4) != true)
                                {
                                    if (menu.value2_4.Contains("http://") == true || menu.value2_4.Contains("https://") == true)
                                        menumsg.TwoMeun.SubMenu.AddEx(new Menu(menu.name2_4, MenuType.view, menu.value2_4));
                                    else
                                        menumsg.TwoMeun.SubMenu.AddEx(new Menu(menu.name2_4, MenuType.click, menu.value2_4));

                                    if (string.IsNullOrEmpty(menu.value2_5) != true)
                                    {
                                        if (menu.value2_5.Contains("http://") == true || menu.value2_5.Contains("https://") == true)
                                            menumsg.TwoMeun.SubMenu.AddEx(new Menu(menu.name2_5, MenuType.view, menu.value2_5));
                                        else
                                            menumsg.TwoMeun.SubMenu.AddEx(new Menu(menu.name2_5, MenuType.click, menu.value2_5));
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    menumsg.TwoMeun.sub_button = false;
                    if (menu.value2.Contains("http://") == true || menu.value2.Contains("https://") == true)
                    {
                        menumsg.TwoMeun.type = MenuType.view;
                        menumsg.TwoMeun.url = menu.value2;
                    }
                    else
                    {
                        menumsg.TwoMeun.type = MenuType.click;
                        menumsg.TwoMeun.key = menu.value2;
                    }
                }
            }
            #endregion

            #region 第三级菜单
            if (string.IsNullOrEmpty(menu.name1) != true && string.IsNullOrEmpty(menu.name2) != true)
            {
                menumsg.ThreeFistMeun = new Menu(menu.name3);
                if (string.IsNullOrEmpty(menu.value3) == true)
                {
                    if (string.IsNullOrEmpty(menu.value3_1) != true)
                    {
                        menumsg.ThreeFistMeun.sub_button = true;
                        menumsg.ThreeFistMeun.SubMenu = new List<Menu>();
                        if (menu.value3_1.Contains("http://") == true || menu.value3_1.Contains("https://") == true)
                            menumsg.ThreeFistMeun.SubMenu.AddEx(new Menu(menu.name3_1, MenuType.view, menu.value3_1));
                        else
                            menumsg.ThreeFistMeun.SubMenu.AddEx(new Menu(menu.name3_1, MenuType.click, menu.value3_1));

                        if (string.IsNullOrEmpty(menu.value3_2) != true)
                        {
                            if (menu.value3_2.Contains("http://") == true || menu.value3_2.Contains("https://") == true)
                                menumsg.ThreeFistMeun.SubMenu.AddEx(new Menu(menu.name3_2, MenuType.view, menu.value3_2));
                            else
                                menumsg.ThreeFistMeun.SubMenu.AddEx(new Menu(menu.name3_2, MenuType.click, menu.value3_2));

                            if (string.IsNullOrEmpty(menu.value3_3) != true)
                            {
                                if (menu.value3_3.Contains("http://") == true || menu.value3_3.Contains("https://") == true)
                                    menumsg.ThreeFistMeun.SubMenu.AddEx(new Menu(menu.name3_3, MenuType.view, menu.value3_3));
                                else
                                    menumsg.ThreeFistMeun.SubMenu.AddEx(new Menu(menu.name3_3, MenuType.click, menu.value3_3));

                                if (string.IsNullOrEmpty(menu.value3_4) != true)
                                {
                                    if (menu.value3_4.Contains("http://") == true || menu.value3_4.Contains("https://") == true)
                                        menumsg.ThreeFistMeun.SubMenu.AddEx(new Menu(menu.name3_4, MenuType.view, menu.value3_4));
                                    else
                                        menumsg.ThreeFistMeun.SubMenu.AddEx(new Menu(menu.name3_4, MenuType.click, menu.value3_4));

                                    if (string.IsNullOrEmpty(menu.value3_5) != true)
                                    {
                                        if (menu.value3_5.Contains("http://") == true || menu.value3_5.Contains("https://") == true)
                                            menumsg.ThreeFistMeun.SubMenu.AddEx(new Menu(menu.name3_5, MenuType.view, menu.value3_5));
                                        else
                                            menumsg.ThreeFistMeun.SubMenu.AddEx(new Menu(menu.name3_5, MenuType.click, menu.value3_5));
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    menumsg.ThreeFistMeun.sub_button = false;
                    if (menu.value3.Contains("http://") == true || menu.value3.Contains("https://") == true)
                    {
                        menumsg.ThreeFistMeun.type = MenuType.view;
                        menumsg.ThreeFistMeun.url = menu.value3;
                    }
                    else
                    {
                        menumsg.ThreeFistMeun.type = MenuType.click;
                        menumsg.ThreeFistMeun.key = menu.value3;
                    }
                }
            }
            #endregion

            var v = menumsg.ToJson();
            //添加菜单
            var dic = WeiXinOpenRequest.CreateMenu(v, RFrameGenerate.Common.CurrentManage.AppId);
            if (dic.errcode == "0")
                return Json(new { Result = "OK" });
            else
            {
                if (dic.errcode == "61007")
                    return Json(new { Result = "NO", Msg = "没有设置菜单权限,公众号未授权或授权给其它第三方平台." });
                else
                {
                    return Json(new { Result = "NO", Msg = "errcode:" + dic.errcode + ",errmsg:" + dic.errmsg });
                }
            }
        }
        public JsonResult LoadKeywordData(GridModel gridModel, int type)
        {
            List<T_WX_AutoReply> list;
            if (type == 0)
                list = BaseCommonBLL.LoadKeywordDataToText(gridModel);
            else
                list = BaseCommonBLL.LoadKeywordDataToTxt(gridModel);

            gridModel.datarows = (from s in list
                                  select new
                                  {
                                      id = s.AutoReplyGuid,//定义当前行数据的一个传输识别名称，一般以主键为名称，可以省略()
                                      cell = new object[]
                                                     {
                                                         s.RuleName,
                                                         Server.HtmlEncode(s.Keyword),
                                                         RFrameGenerate.BLL.BaseCommomDictionaryConst.AutoReplyType.GetAutoTypeText( s.Type),
                                                         s.IsEnable ,
                                                         Server.HtmlEncode(s.Value)                                                   
                                                      }
                                  }).ToArray();
            return Json(gridModel.PackagePageModel());
        }


        public JsonResult LoadTxtData(GridModel gridModel)
        {
            var list = BaseCommonBLL.GetMaterialPageList(gridModel);

            gridModel.datarows = (from s in list
                                  select new
                                  {
                                      id = s.MaterialGuid,//定义当前行数据的一个传输识别名称，一般以主键为名称，可以省略()
                                      cell = new object[]
                                                     {
                                                         s.MaterialTitle                                                       
                                                      }
                                  }).ToArray();
            return Json(gridModel.PackagePageModel());
        }

        public JsonResult AddTxt(string keyword, string guids, string rulenametxt)
        {
            var result = BaseCommonBLL.AddTxt(keyword, guids, rulenametxt);
            if (result.State == true)
                return Json(new { Result = "OK" });
            else
                return Json(new { Result = "NO", Msg = result.Msg });
        }

        public JsonResult DelTxt(Guid guid)
        {
            var result = BaseCommonBLL.DelTxt(guid);
            if (result.State == true)
                return Json(new { Result = "OK" });
            else
                return Json(new { Result = "NO", Msg = result.Msg });
        }
        public JsonResult GetTxtByID(Guid guid)
        {
            var result = BaseCommonBLL.GetTxtByID(guid);
            return Json(new { Result = "OK", Data = result });
        }
        public JsonResult UpdateTxt(Guid guid, string guids, string keyword, string rulenametxt)
        {
            var result = BaseCommonBLL.UpdateTxt(keyword, guids, guid, rulenametxt);
            if (result.State == true)
                return Json(new { Result = "OK" });
            else
                return Json(new { Result = "NO", Msg = result.Msg });
        }
    }
}
