#if NETSTANDARD2_0
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
#endif
#if NET35
using System.Web;
#endif
using Hncore.Pass.Vpn.Service;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using UEditor.Core.Handlers;

namespace UEditor.Core
{
    public class UEditorService
    {
        UploadService m_UploadService;
        public UEditorService(IHostingEnvironment env,UploadService uploadService)
        {
            // .net core的名字起的比较怪而已，并不是我赋值赋错了
            if (string.IsNullOrWhiteSpace(Config.WebRootPath))
            {
                Config.WebRootPath = env.ContentRootPath;
            }

            Config.EnvName = env.EnvironmentName;
            m_UploadService = uploadService;
        }


        /// <summary>
        /// 上传并返回结果，已处理跨域Jsonp请求
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public UEditorResponse UploadAndGetResponse(HttpContext context)
        {

             var action = context.Request.Query["action"];

            object result;
            if (AppConsts.Action.Config.Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                var configHandle = new ConfigHandler();
                result = configHandle.Process();
            }
            else
            {
                var handle = HandelFactory.GetHandler(action, context, m_UploadService);
                result = handle.Process();
            }
            string resultJson = JsonConvert.SerializeObject(result, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });
            string contentType = "text/plain";
             string jsonpCallback = context.Request.Query["callback"];

            if (!jsonpCallback.IsNullOrWhiteSpace())
            {
                contentType = "application/javascript";
                resultJson = string.Format("{0}({1});", jsonpCallback, resultJson);
                UEditorResponse response = new UEditorResponse(contentType, resultJson);
                return response;
            }
            else
            {
                UEditorResponse response = new UEditorResponse(contentType, resultJson);
                return response;
            }
        }

        /// <summary>
        /// 单纯的上传并返回结果，未处理跨域Jsonp请求
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public object Upload(HttpContext context)
        {
             var action = context.Request.Query["action"];
            object result;
            if (AppConsts.Action.Config.Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                result = new ConfigHandler();
            }
            else
            {
                var handle = HandelFactory.GetHandler(action, context, m_UploadService);
                result = handle.Process();
            }
            return result;
        }
    }
}
