using DeviceRepair.Api.Common;
using DeviceRepair.Api.CustomAttribute;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using DeviceRepair.Models.Preserve;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Reflection;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace DeviceRepair.Api.Controllers
{
    [RoutePrefix("Api/Form")]
    public class FormController : CFController
    {
        /// <summary>
        /// 点检表新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertSingle")]
        [HttpAuthorize]
        public APIResponseData InsertSingle(MaintenanceFormVersionInfo Item)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                if (Item == null)
                    throw new Exception("操作失败，新增的点检表对象不能为空！");

                base.GetParams();
                return FormAccess.Instance.InsertSingle(Item, OperationInfo);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 判断数据是否存在
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("FormIsExists")]
        [HttpAuthorize]
        public APIResponseData FormDataIsExists(MaintenanceFormVersionInfo Item)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                if (Item == null)
                    throw new Exception("操作失败，待检测的点检表对象不能为空！");

                if (string.IsNullOrWhiteSpace(Item.FormName))
                    throw new Exception("操作失败，点检表的名称不能为空！");

                if (string.IsNullOrWhiteSpace(Item.VersionCode))
                    throw new Exception("操作失败，点检表的文件编号不能为空！");

                if (string.IsNullOrWhiteSpace(Item.VersionRev))
                    throw new Exception("操作失败，点检表的文件版本不能为空！");

                return FormAccess.Instance.DataIsExists(Item);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        [HttpPost]
        [Route("Upload")]
        [HttpAuthorize]
        public async Task<APIResponseData> UploadFile()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "上传失败！" };
            try
            {
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }

                string root = Path.Combine(Utils.Config.Configurations.Properties.AttachmentDirectory, "FormVer");
                var provider = new DoMultipartFormDataStreamProvider(root);

                await Request.Content.ReadAsMultipartAsync(provider);
                string FileName = "";
                foreach (var file in provider.FileData)
                {
                    FileName = file.LocalFileName;
                    break;
                }

                apiResponseData.Code = 1;
                apiResponseData.Data = FileName;
                apiResponseData.Message = "上传成功";
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        [HttpGet]
        [Route("GetSingle")]
        [HttpAuthorize]
        public APIResponseData GetSingle(int pk)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                apiResponseData = FormAccess.Instance.GetSingle(pk);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        [HttpGet]
        [Route("GetQuery")]
        [HttpAuthorize]
        public APIResponseData GetQuery(string FilterString)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                apiResponseData = FormAccess.Instance.GetQuery(FilterString);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 修改点检表备注信息
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ChangeRemark")]
        [HttpAuthorize]
        public APIResponseData ChangeRemark(MaintenanceFormVersionInfo Item)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                base.GetParams();
                apiResponseData = FormAccess.Instance.ChangeRemark(Item, OperationInfo);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 单行数据翻转当前状态(启用/禁用)
        /// </summary>
        /// <param name="pk"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ChangeStauts")]
        [HttpAuthorize]
        public APIResponseData ChangeStauts(int pk)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                base.GetParams();
                apiResponseData = FormAccess.Instance.ChangeStauts(pk, OperationInfo);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        [HttpGet]
        [Route("GetDeviceInformationTreeView")]
        [HttpAuthorize]
        public APIResponseData GetDeviceInformationTreeView()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                apiResponseData = FormAccess.Instance.GetDeviceInformationTreeView();
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 分配点检表到指定设备
        /// </summary>
        /// <param name="Datas"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AssigningFormToDevices")]
        [HttpAuthorize]
        public APIResponseData AssigningFormToDevices(List<SubmitAssignFormsToDevices> Datas)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                if (Datas == null || Datas.Count == 0)
                    return apiResponseData;

                base.GetParams();
                apiResponseData = FormAccess.Instance.AssigningFormToDevices(Datas, OperationInfo);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

    }
}
