﻿using NST.Core.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;


namespace AdminManager.uploadify
{
    /// <summary>
    /// savecutimg 的摘要说明
    /// </summary>
    public class savecutimg : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            JsonData model = JsonMapper.ToObject(context.Request.Form["data"]);
            JavaScriptSerializer json = new JavaScriptSerializer();
            string suffix = Config.GetConfig().GetAppSettingValue("recipesuffix");
            string resultUrl = "";
            List<FileUpload.Size> listSizes = new List<FileUpload.Size>();
            switch (model.Inst_Object["type"].ToString())
            {
                case "weixin":
                    string strSizes = Config.GetConfig().GetAppSettingValue("weixinImgSizes");
                    listSizes = json.Deserialize<List<FileUpload.Size>>(strSizes);
                    break;
            }
            FileUpload.FileUploadSoapClient fileUpload = Config.GetFileUploadService();
            FileUpload.UserHeader user = new FileUpload.UserHeader();
            user.Password = Config.GetConfig().GetAppSettingValue("pwd");
            user.UserName = Config.GetConfig().GetAppSettingValue("username");
            int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
            int.TryParse(model["x1"].ToString(), out x1);
            int.TryParse(model["x2"].ToString(), out x2);
            int.TryParse(model["y1"].ToString(), out y1);
            int.TryParse(model["y2"].ToString(), out y2);
            bool r = fileUpload.CutImgs(user, model["img"].ToString(), FileUpload.FolderType.Official, suffix, listSizes.ToArray(),
                                new FileUpload.Point() { X = x1, Y = y1 }, new FileUpload.Point() { X = x2, Y = y2 }, out resultUrl);
            context.Response.Write(resultUrl);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}