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

namespace DeviceRepair.Api.Controllers
{
    [RoutePrefix("Api/Preserve")]
    public class PreserveController : CFController
    {
        /// <summary>
        /// 获取保养计划及对应的点检表信息
        /// </summary>
        /// <param name="objjson"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetPlanAndFormInfo")]
        public APIResponseData GetPlanAndFormInfo(int pk)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "获取数据失败！" };

            try
            {
                if (pk == 0)
                    throw new Exception("点检表编号是必须的参数！");

                return PreserveAccess.Instance.GetPlanAndFormInfo(pk);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message.ToString();
            }

            return apiResponseData;
        }

        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="AutoID"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetFileItem")]
        [HttpAuthorize]
        public IHttpActionResult DownloadFile(int pk)
        {
            try
            {
                if (pk <= 0)
                    throw new Exception("参数【主键编号】不能为空！");

                MaintenanceFormVersionInfo Item = PreserveAccess.Instance.GetMaintenanceFormVersionSingle(pk);
                string filePath = Item.FormPath; // 替换为实际的文件路径

                string fileName = Path.GetFileName(filePath);
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                var fileStream = new FileStream(filePath, FileMode.Open);

                response.Content = new StreamContent(fileStream);
                response.Content.Headers.Add("FileName", fileName);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = fileName
                };

                return ResponseMessage(response);
            }
            catch (Exception ex)
            {
                return Ok(ex.Message);
            }
        }

        [HttpGet]
        [Route("GetDownPDFFile")]
        [HttpAuthorize]
        public IHttpActionResult DownPDFFile(int pk)
        {
            try
            {
                if (pk <= 0)
                    throw new Exception("参数【主键编号】不能为空！");

                AttachmentInfo Item = PreserveAccess.Instance.GetAttachmentInfo(pk);
                string filePath = Item.FilePath; // 替换为实际的文件路径

                string fileName = Path.GetFileName(filePath);
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                var fileStream = new FileStream(filePath, FileMode.Open);

                response.Content = new StreamContent(fileStream);
                response.Content.Headers.Add("FileName", fileName);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = fileName
                };

                return ResponseMessage(response);
            }
            catch (Exception ex)
            {
                return Ok(ex.Message);
            }
        }

        /// <summary>
        /// 根据主键编号获取对象
        /// </summary>
        /// <param name="objjson"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetPreserveRecordItemByPrimaryKey")]
        [HttpAuthorize]
        public APIResponseData GetPreserveRecordItemByPrimaryKey(int pk)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据获取失败,主键ID是必须的参数" };
            try
            {
                if (pk <= 0)
                    throw new Exception("参数【主键编号】不能为空！");

                apiResponseData = PreserveAccess.Instance.GetPreserveRecordItemByPrimaryKey(pk);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 通过设备主键编号及点检表主键编号获取单条保养信息记录
        /// </summary>
        /// <param name="EquipmentID"></param>
        /// <param name="FormID"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetSingleByEquipmentIDAndFormID")]
        [HttpAuthorize]
        public APIResponseData GetSingleByEquipmentPrimaryIDAndFormPrimaryID(int EquipmentID, int FormID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            if (EquipmentID <= 0)
                return new APIResponseData { Code = -1, Message = $"参数【设备表主键编号】不能为空！" };

            if (FormID <= 0)
                return new APIResponseData { Code = -1, Message = $"参数【保养点检表主键编号】不能为空！" };

            try
            {
                return PreserveAccess.Instance.GetSingleByEquipmentPrimaryIDAndFormPrimaryID(EquipmentID, FormID);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取所有的保养记录数据
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetPreserveDatas")]
        [HttpAuthorize]
        public APIResponseData GetPreserveDatas(string filter)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "获取数据失败" };
            try
            {
                return PreserveAccess.Instance.GetPreserveDatas(filter);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 获取保养数据
        /// </summary>
        /// <param name="pk"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetDetailById")]
        [HttpAuthorize]
        public APIResponseData GetDetailById(int pk)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据获取失败,主键ID是必须的参数" };
            try
            {
                if (pk <= 0)
                    throw new Exception("主键ID是必须的参数！");
                apiResponseData = PreserveAccess.Instance.GetPreserveDetailByID(pk);

            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 插入保养数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Insertable")]
        public APIResponseData Insertable(MaintenanceRecordSubmit entity)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            if (entity == null)
                return apiResponseData;

            try
            {
                base.GetParams();
                apiResponseData = PreserveAccess.Instance.Insertable(entity, OperationInfo);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
