using DeviceRepair.Api.Common;
using DeviceRepair.Api.CustomAttribute;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;

namespace DeviceRepair.Api.Controllers
{
    [RoutePrefix("Api/Common")]
    public class CommonController : CFController
    {
        [HttpGet]
        [Route("CurrentDateTime")]
        public string CurrentDateTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="FilterString"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetQuery")]
        [HttpAuthorize]
        public APIResponseData GetQuery()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                apiResponseData = SystemUtil.Instance.GetAll();
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 更改系统设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("EditConfigs")]
        [HttpAuthorize]
        public APIResponseData EditConfigs(List<SysConfigInfo> Datas)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                apiResponseData = SystemUtil.Instance.EditConfigs(Datas);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取附件信息
        /// </summary>
        /// <param name="FilterString"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAttachments")]
        [HttpAuthorize]
        public APIResponseData GetAttachment(string TableName, string PrimaryKey, string PrimaryValue)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                apiResponseData = SystemUtil.Instance.GetAttachment(TableName, PrimaryKey, PrimaryValue);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取图片
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAttachments")]
        [HttpAuthorize]
        public HttpResponseMessage GetImage(int AttachmentId)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            // 创建一个HttpResponseMessage实例
            HttpResponseMessage response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);

            try
            {
                apiResponseData = SystemUtil.Instance.GetAttachmentImg(AttachmentId);
                if (apiResponseData.IsSuccess)
                {
                    AttachmentInfo atta = (AttachmentInfo)apiResponseData.Data;
                    // 设置响应头，以便客户端能够正确识别内容类型和长度
                    response.Content = new ByteArrayContent(File.ReadAllBytes(atta.FilePath));
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                    response.Content.Headers.ContentLength = new FileInfo(atta.FilePath).Length;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return response;
        }

    }
}
