using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Ropin.Inspection.Api.Common;
using Ropin.Inspection.Model;
using Ropin.Inspection.Service;
using Ropin.Inspection.Service.DEV.Interface;
using System.Threading.Tasks;
using System;
using Ropin.Inspection.Model.ViewModel.DEV;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.Util;
using NPOI.XWPF.UserModel;
using NPOI.SS.Formula;
using Newtonsoft.Json;

namespace Ropin.Inspection.Api.Controllers.DEV
{
    public class devHandController : BaseController
    {
        public ILogger<devHandController> _logger { get; }
        private readonly IDev_HandService _repository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="repository"></param>
        public devHandController( ILogger<devHandController> logger, IDev_HandService repository)
        {
            _logger = logger;
            _repository = repository;
        }

        /// <summary>
        /// 创建设备手持信息
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreatedevHandAsync")]
        public async Task<ApiResult> CreatedevHandAsync(dev_HandModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _repository.CreateOneAsync(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除设备手持信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeletedevHandAsync/{id}")]
        public async Task<ApiResult> DeletedevHandAsync(Guid id)
        {
            if (id==Guid.Empty)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _repository.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 更新设备手持信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        [HttpPut("UpdatedevHandAsync/{id}")]
        [AllowAnonymous]
        public async Task<ApiResult> UpdatedevHandAsync(Guid id, dev_HandModel updateModel)
        {
            if (id==Guid.Empty)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _repository.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 获取设备手持信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetdevHandAsync")]
        [AllowAnonymous]
        public async Task<ApiResult> GetdevHandAsync(TdevHandSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var contentList = await _repository.GetConditionAsync(searchModel);
                return new ApiResult<PagesModel<devHandViewModel>>(new PagesModel<devHandViewModel>(contentList, searchModel));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过id获取设备手持信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetdevHandByIDAsync")]
        [AllowAnonymous]
        public async Task<ApiResult> GetdevHandByIDAsync(Guid Id)
        {
            if (Id==Guid.Empty)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                devHandViewModel data = new devHandViewModel();
                data = await _repository.GetConditionByIdAsync(Id);
                return new ApiResult<devHandViewModel>(data);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过id获取设备手持信息返回word文件
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetdevHandFileByIDAsync")]
        [AllowAnonymous]
        public async Task<IActionResult> GetdevHandFileByIDAsync(Guid Id)
        {
            try
            {
                devHandViewModel data = new devHandViewModel();
                data = await _repository.GetConditionByIdAsync(Id);
                XWPFDocument doc = new XWPFDocument();
                var paragraph = doc.CreateParagraph();
                var run = paragraph.CreateRun();
                run.FontSize = 16;
                string fileName = "null.docx";
                if (data!=null)
                {
                    string json = JsonConvert.SerializeObject(data);
                    run.SetText(json);
                    fileName = data.C_ID + ".docx";
                }
                else
                {
                    run.SetText("null");
                }
                using (MemoryStream stream = new MemoryStream())
                {
                    doc.Write(stream);
                    byte[] bytes = stream.ToArray();
                    string contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    return File(bytes, contentType, fileName);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}