using PageAdmin.BLL;
using PageAdmin.Utils;
using PageAdmin.Web.Areas.E.Controllers.UEditor.Filter;
using System;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using UEditor;

namespace PageAdmin.Web.Areas.E.Controllers
{
    public class EditorUploadController : BaseController
    {
        private UeditorConfig _EditorConfig;
        //百度编辑器配置文件的路径。
        private string _ConfigFile;
        public EditorUploadController()
        {
            _ConfigFile = "/Incs/UEditor/net/config.json";
            _EditorConfig = new UeditorConfig(_ConfigFile);
        }


        [HttpGet]
        public ActionResult Index()
        {
            var context = System.Web.HttpContext.Current;
            string post = context.Request.RequestType;
            Handler action = null;
            //百度编辑器配置文件的路径。
            switch (context.Request.QueryString["action"])
            {
                case "config":
                    action = new ConfigHandler(context, _EditorConfig);
                    break;

                case "listimage":
                    action = new ListFileManager(context, _EditorConfig.GetString("imageManagerListPath"), _EditorConfig.GetStringList("imageManagerAllowFiles"), _EditorConfig);
                    break;
                case "listfile":
                    action = new ListFileManager(context, _EditorConfig.GetString("fileManagerListPath"), _EditorConfig.GetStringList("fileManagerAllowFiles"), _EditorConfig);
                    break;
                case "catchimage":
                    action = new CrawlerHandler(context, _EditorConfig);
                    break;
                default:
                    action = new NotSupportedHandler(context);
                    break;
            }
            action.Process();
            return null;
        }


        /// <summary>
        /// 上传文件对应的方法
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [EditorUploadFilter]
        [HttpPost]
        public void Index(FormCollection form)
        {
            var context = System.Web.HttpContext.Current;
            var request = context.Request;
            Handler action = null;
            switch (context.Request.QueryString["action"])
            {
                case "uploadimage":
                    action = new UploadHandler(context, new UploadConfig()
                    {
                        AllowExtensions = _EditorConfig.GetStringList("imageAllowFiles"),
                        PathFormat = _EditorConfig.GetString("imagePathFormat"),
                        SizeLimit = _EditorConfig.GetInt("imageMaxSize"),
                        UploadFieldName = _EditorConfig.GetString("imageFieldName")
                    });
                    break;
                case "uploadscrawl":
                    action = new UploadHandler(context, new UploadConfig()
                    {
                        AllowExtensions = new string[] { ".png" },
                        PathFormat = _EditorConfig.GetString("scrawlPathFormat"),
                        SizeLimit = _EditorConfig.GetInt("scrawlMaxSize"),
                        UploadFieldName = _EditorConfig.GetString("scrawlFieldName"),
                        Base64 = true,
                        Base64Filename = "scrawl.png"
                    });
                    break;
                case "uploadvideo":
                    action = new UploadHandler(context, new UploadConfig()
                    {
                        AllowExtensions = _EditorConfig.GetStringList("videoAllowFiles"),
                        PathFormat = _EditorConfig.GetString("videoPathFormat"),
                        SizeLimit = _EditorConfig.GetInt("videoMaxSize"),
                        UploadFieldName = _EditorConfig.GetString("videoFieldName")
                    });
                    break;
                case "uploadfile":
                    action = new UploadHandler(context, new UploadConfig()
                    {
                        AllowExtensions = _EditorConfig.GetStringList("fileAllowFiles"),
                        PathFormat = _EditorConfig.GetString("filePathFormat"),
                        SizeLimit = _EditorConfig.GetInt("fileMaxSize"),
                        UploadFieldName = _EditorConfig.GetString("fileFieldName")
                    });
                    break;
                default:
                    action = new NotSupportedHandler(context);
                    break;
            }

            action.Process();
            UploadResult result = action.Result;
            ///上传成功后附件保存到数据库
            if (result.State == UploadState.Success)
            {
                InsertData(request, result.Url);
            }
        }


        /// <summary>
        /// 每次上传都保存到数据库对应的附件表
        /// </summary>
        private void InsertData(HttpRequest request, string fileUrl)
        {
            AttachmentsBLL attachmentDataBLL = new AttachmentsBLL();
            string tableName = request["tableName"], fieldName = request["fieldName"], guid = request["guid"], url = fileUrl, thumbnail = "", userName = "";
            CurrentUser user = ViewBag.CurrentUser;
            if (user != null)
            {
                userName = user.UserName;
            }
            attachmentDataBLL.Add(tableName, fieldName, guid, userName, url, thumbnail);
        }

    }
}