using JinianNet.JNTemplate;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;
using System.Xml.Linq;
using 电子展板.Utility.Core;
using 电子展板.Utility.Extension;
using 电子展板.Utility.ResponseModels;


namespace 电子展板.Controller
{

    public class BaseController : ApiController
    {
        protected ActionResult Content(string content)
        {
            return new ActionResult { Content = new StringContent(content, Encoding.UTF8, "text/plain") };
        }

        protected ActionResult ContentType(string content, string contentType)
        {
            return new ActionResult { Content = new StringContent(content, Encoding.UTF8, contentType) };
        }

        public new ActionResult Redirect(string url)
        {
            ActionResult resp = new ActionResult(HttpStatusCode.Moved);
            string originalString = Request.RequestUri.OriginalString;
            string pathAndQuery = Request.RequestUri.PathAndQuery;
            string baseUrl = originalString.Substring(0, originalString.IndexOf(pathAndQuery));
            resp.Headers.Location = new Uri(baseUrl + url);
            return resp;
        }

        protected ActionResult File(string virtualPath, string contentType)
        {
            return File(virtualPath, contentType, "HelloWorld");
        }

        protected ActionResult File(string virtualPath, string contentType, string fileDownloadName)
        {
            string physicalPath = MyEnvironment.WebRootPath(virtualPath);
            return PhysicalFile(physicalPath, contentType, fileDownloadName);
        }

        protected ActionResult PhysicalFile(string physicalPath, string contentType)
        {
            return PhysicalFile(physicalPath, contentType, "HelloWorld");
        }

        protected ActionResult PhysicalFile(string physicalPath, string contentType, string fileDownloadName)
        {
            return File(System.IO.File.OpenRead(physicalPath), contentType, fileDownloadName);
        }


        protected ActionResult File(byte[] fileContents, string contentType)
        {
            return File(fileContents, contentType, "HelloWorld");
        }


        protected ActionResult File(byte[] fileContents, string contentType, string fileDownloadName)
        {
            ActionResult response = new ActionResult(HttpStatusCode.OK);
            response.Content = new ByteArrayContent(fileContents);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            response.Content.Headers.ContentLength = fileContents.Length;
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = HttpUtility.UrlEncode(fileDownloadName)
            };
            response.Headers.Add("Access-Control-Expose-Headers", "FileName");
            response.Headers.Add("FileName", HttpUtility.UrlEncode(fileDownloadName));
            return response;
        }

        protected ActionResult File(Stream fileStream, string contentType)
        {
            return File(fileStream, contentType, "HelloWorld");
        }

        protected ActionResult File(Stream fileStream, string contentType, string fileDownloadName)
        {
            ActionResult response = new ActionResult(HttpStatusCode.OK);
            response.Content = new StreamContent(fileStream);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            response.Content.Headers.ContentLength = fileStream.Length;
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = HttpUtility.UrlEncode(fileDownloadName)
            };
            response.Headers.Add("Access-Control-Expose-Headers", "FileName");
            response.Headers.Add("FileName", HttpUtility.UrlEncode(fileDownloadName));
            return response;
        }
        #region 快捷方法
        protected ActionResult Success(string message = "恭喜您，操作成功。", object data = null)
        {
            return Content(new AjaxResult(ResultType.Success, message, data).ToJson());
        }
        protected ActionResult Error(string message = "对不起，操作失败。", object data = null)
        {
            return Content(new AjaxResult(ResultType.Error, message, data).ToJson());
        }
        protected ActionResult Warning(string message, object data = null)
        {
            return Content(new AjaxResult(ResultType.Warning, message, data).ToJson());
        }
        protected ActionResult Info(string message, object data = null)
        {
            return Content(new AjaxResult(ResultType.Info, message, data).ToJson());
        }


        public ActionResult Json(object obj)
        {
            return new ActionResult { Content = new StringContent(obj.ToJson(), Encoding.UTF8, "application/json") };
        }



        public ActionResult Text(string str)
        {
            //return Content(str, "text/plain");
            return new ActionResult { Content = new System.Net.Http.StringContent(str, Encoding.UTF8, "text/plain") };

        }

        public ActionResult HtmlStr(string str)
        {
            return new ActionResult { Content = new System.Net.Http.StringContent(str, Encoding.UTF8, "text/html") };

        }

        public ActionResult Html(string view)
        {
            return Html(view, null);
        }
        public ActionResult Resouce(string viewName, string contentType)
        {
            string packUri = $"pack://application:,,,/Assets{viewName}";
            byte[] bytes = null;
            using (Stream stream = System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream)
            {
                bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
            }
            ActionResult response = new ActionResult(HttpStatusCode.OK);
            response.Content = new ByteArrayContent(bytes);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            response.Content.Headers.ContentLength = bytes.Length;
            return response;
        }


        public ActionResult Html(string view, Dictionary<string, object> dict)
        {
            if (!view.StartsWith("/"))
                view = "/" + view;
            string path = AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/") + "Views";


            var template = Engine.LoadTemplate(path + view + ".html");


            if (dict != null)
            {
                foreach (KeyValuePair<string, object> keyValue in dict)
                {
                    template.Set(keyValue.Key, keyValue.Value);
                }
            }
            var result = template.Render();
            // return Content(result, "text/html");
            return new ActionResult { Content = new System.Net.Http.StringContent(result, Encoding.UTF8, "text/html") };

        }
        #endregion
    }
}

namespace Microsoft.AspNetCore.Mvc
{
    public class ActionResult : System.Net.Http.HttpResponseMessage
    {
        public ActionResult() : base(HttpStatusCode.OK)
        {

        }
        public ActionResult(HttpStatusCode statusCode) : base(statusCode)
        {
        }
    }
}