using PageAdmin.BLL;
using PageAdmin.Common;
using PageAdmin.Utils;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class UploadPanelController:BaseController
    {
        string _UploadRootPath, _UploadPath;
        int _ReName;
        public UploadPanelController() :base("Tool/FileManager")
        {

        }

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public async Task<ActionResult> Index(FormCollection form)
        {
            string rv = "";
            _ReName = StringHelper.Format<int>(form["rename"]);
            _UploadRootPath = StringHelper.Format<string>(form["uploadRootPath"]);
            _UploadPath = StringHelper.Format<string>(form["uploadPath"]);
            string size = StringHelper.Format<string>(form["size"]); //webupload自带的参数
            if (StringHelper.IsNum(size))
            {
                Action action = () => { rv = Save(form); };
                await Task.Run(action);
            }
            else
            {
                rv = Result(0, "InValid Submit");
            }
            return Content(rv);
        }

      
        /// <summary>
        /// 
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [NonAction]
        private string Save(FormCollection form)
        {
            HttpPostedFileBase hpf = Request.Files["file"];
            int hpfLength = hpf.ContentLength;
            if (hpfLength == 0)
            {
                return Result(0, "emptyfile","","");
            }
            string sourceFileName = Path.GetFileNameWithoutExtension(hpf.FileName);
            string fileExt = Path.GetExtension(hpf.FileName).ToLower();
            if(SysHelper.IsForbiddenFileExt(fileExt))
            {
                return Result(0, "禁止的文件格式！","", "");
            }
            string fileName = form["fileName"];
            bool deleteRepeate = false;
            if(string.IsNullOrEmpty(fileName))
            {
                if (_ReName == 1)
                {
                    fileName = SysHelper.NewGuid() + fileExt;
                }
                else
                {
                    fileName = sourceFileName + fileExt;
                }
            }
            else
            {
                fileName = fileName + fileExt;
               deleteRepeate = true;
            }

            if(!string.IsNullOrEmpty(_UploadPath))
            {
                _UploadPath = _UploadPath + "/";
            }
            string savePath = "/"+_UploadRootPath+"/"+_UploadPath;
            savePath = savePath.Replace("//", "/");
            savePath =IOHelper.UploadFile(hpf,savePath,fileName, deleteRepeate);
            string fileSize = ((hpfLength) / 1024).ToString("F2");
            return Result(1, "", savePath, fileSize);
        }

        private string Result(int state, string info)
        {
            return Result(state, info, "", "");
        }


        private string Result(int state, string info, string path, string thumbnail)
        {
            string result = "{\"state\" : " + state + ", \"msg\" :\"" + info + "\", \"path\" : \"" + path + "\",\"thumbnail\" : \"" + thumbnail + "\",\"name\" : \"" + name + "\"}";
            return result;
        }

    }
}