using PageAdmin.Web.Filter;
using PageAdmin.Utils;
using System;
using System.Web.Mvc;
using System.Linq;
using System.Collections.Generic;

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class FileManagerController : FileManageBaseController
    {
        public FileManagerController() : base("/", "Tool/")
        {
            base.FuncFilter = List => List.OrderBy(c=>c.Type);
        }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [AjaxOnly]
        [HttpPost]
        public JsonResult Zip(FormCollection form)
        {
            string path = form["path"];
            string files = form["files"];
            string directorys = form["directorys"];
            string zipPath = path + "/" + DateTime.Now.ToString("yyyMMddHHmmss") + ".zip";
            string[] arrFiles = files.Split('|');
            string[] arrDirectorys = directorys.Split('|');
            List<string> sources = new List<string>();
            foreach (var file in arrFiles)
            {
                if (!string.IsNullOrEmpty(file))
                {
                    sources.Add(file);
                }
            }
            foreach (var directory in arrDirectorys)
            {
                if (!string.IsNullOrEmpty(directory))
                {
                    sources.Add(directory);
                }
            }
            bool state=ZipHelper.Compress(sources, zipPath);
            TipsInfo tipsInfo = new TipsInfo();
            if(!state)
            {
                tipsInfo.State = 0;
                tipsInfo.Msg = "压缩失败";
            }
            return Json(tipsInfo);
        }

        /// <summary>
        /// 解压，一次只能操作一个文件
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [AjaxOnly]
        [HttpPost]
        public JsonResult Extract(FormCollection form)
        {
            string path = form["path"];
            string file = form["file"];
            if (!string.IsNullOrEmpty(file))
            {
                ZipHelper.DeCompress(file, path, true);
            }
            TipsInfo tipsInfo = new TipsInfo();
            return Json(tipsInfo);
        }

    }
}
