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

namespace PageAdmin.Web.Areas.Admin.Controllers
{

    public class PluginAdminController:BaseController
    {
        public PluginAdminController():base("App/")
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        [AjaxOnly]
        [HttpGet]
        public JsonResult PageListData()
        {
            MvcControllerContext context = new MvcControllerContext();
            context.PageInfo.PageSize = 10;
            context.Data = Request.QueryString;
            PluginAdminBLL bll = new PluginAdminBLL();
            var list = bll.PageList(context);
            return base.Json(list, context.PageInfo);
        }


        [HttpGet]
        public ActionResult Set()
        {
            string pluginName = Request.QueryString["pluginName"];
            if (string.IsNullOrEmpty(pluginName))
            {
                throw new System.Exception("pluginName参数不能为空！");
            }
            GetPluginsBLL getInstalledPluginsBLL = new GetPluginsBLL();
            PluginInfo pluginInfo = getInstalledPluginsBLL.GetPluginInfo(pluginName);
            ViewBag.PluginInfo = JsonHelper.SerializeObject(pluginInfo);
            return View();
        }

        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HttpPost]
        public JsonResult Set(FormCollection form)
        {
            string pluginName = form["pluginName"];
            if(string.IsNullOrEmpty(pluginName))
            {
                throw new  System.Exception("pluginName参数不能为空！");
            }
            TipsInfo tipsInfo = new TipsInfo();
            string version= form["version"];
            string act=form["act"];
            PluginAdminBLL bll = new PluginAdminBLL();
            switch (act)
            {
                case "open":
                    bll.Open(pluginName, version);
                    break;

                case "close":
                    bll.Close(pluginName, version);
                    break;

                case "uninstall":
                    bll.Uninstall(pluginName,version);
                    PluginManager.Unload(pluginName);
                    break;
            }
            return base.Json(tipsInfo);
        }


        //[AjaxOnly]
        //[HttpPost]
        //public JsonResult Open(FormCollection form)
        //{
        //    string pluginName = form["pluginName"];
        //    PluginAdminBLL bll = new PluginAdminBLL();
        //    bll.Open(pluginName);
        //    TipsInfo tipsInfo = new TipsInfo();
        //    return base.Json(tipsInfo);
        //}


        //[AjaxOnly]
        //[HttpPost]
        //public JsonResult Close(FormCollection form)
        //{
        //    string pluginName = form["pluginName"];
        //    PluginAdminBLL bll = new PluginAdminBLL();
        //    bll.Close(pluginName);
        //    TipsInfo tipsInfo = new TipsInfo();
        //    return base.Json(tipsInfo);
        //}

        //[AjaxOnly]
        //[HttpPost]
        //public JsonResult Unload(FormCollection form)
        //{
        //    string pluginName = form["pluginName"];
        //    PluginManager.Unload(pluginName);
        //    TipsInfo tipsInfo = new TipsInfo();
        //    return base.Json(tipsInfo);
        //}


    }
}