using CsvHelper;
using CsvHelper.Configuration;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Net.Http.Headers; 
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.AspNetCore.Mvc.ApiExplorer;

namespace XYY.Core.Standard.Mvc
{


    public class ApiControllerBase : ControllerBase
    {
        string tempPath = System.AppContext.BaseDirectory;
        [NonAction]
        public async Task<IActionResult> OutCsv<X, Y>(List<X> list, string fileName) where Y : ClassMap<X>
        {
            if (!System.IO.Directory.Exists(tempPath))
                System.IO.Directory.CreateDirectory(tempPath);
            string filePath = tempPath + "\\" + Guid.NewGuid() + ".csv";
            using (var writer = new StreamWriter(filePath, false, Encoding.GetEncoding("GB2312")))
            {
                using (var csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture))
                {

                    csv.Context.RegisterClassMap<Y>();
                    await csv.WriteRecordsAsync(list);
                }
            }

            var data = System.IO.File.ReadAllBytes(filePath);
            System.IO.File.Delete(filePath);
            return CSVFile(fileName, data);
        }
        [NonAction]
        public IActionResult CSVFile(string fileName, byte[] data)
        {
            this.HttpContext.Response.Headers.Add("ContentDisposition", "attachment");
            return File(data, "text/csv", fileName);
        }
        [NonAction]
        public new IActionResult File(string fileName, byte[] data)
        {
            this.HttpContext.Response.Headers.Add("ContentDisposition", "attachment");
            new FileExtensionContentTypeProvider().TryGetContentType(fileName, out var contentType);
            return File(data, contentType, fileName);
        }
        [NonAction]
        public new IActionResult Ok(object o)
        {
            return new OkObjectResult(new ApiJsonModel
            {
                code = HttpStatusCode.OK,
                data = o,
                success = true,
                message = "操作成功！"
            });
        }
        [NonAction]
        public IActionResult Ok(string message)
        {
            return new OkObjectResult(new ApiJsonModel
            {
                code = HttpStatusCode.OK,
                data = message,
                success = true,
                message = "操作成功！"
            });
        }
        [NonAction]
        public new IActionResult Ok()
        {
            return new OkObjectResult(new ApiJsonModel
            {
                code = HttpStatusCode.OK,
                data = string.Empty,
                success = true,
                message = "操作成功！"
            });
        }
        [NonAction]
        public IActionResult Error(string message)
        {
            return new OkObjectResult(new ApiJsonModel
            {
                code = HttpStatusCode.InternalServerError,
                data = message,
                success = false,
                message = "操作失败！"
            });
        }
        [NonAction]
        public IActionResult GetError(string message, object response)
        {
            return new OkObjectResult(new ApiJsonModel
            {
                code = HttpStatusCode.InternalServerError,
                data = response,
                success = false,
                message = message,
                error = message
            });
        }

    }
}
