using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using NetTaste;
using OpenAuth.App;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Request;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 流程签章
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WFStampController : ControllerBase
    {
        WFStampApp stampApp;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="stampIBLL"></param>

        public WFStampController(WFStampApp stampApp)
        {
            this.stampApp = stampApp;
        }

        /// <summary>
        /// 获取属于登录者签章
        /// workflow/stamps/{id}
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<WFStamp>>> Load(string id)
        {
            var list = await stampApp.GetList(id);
            return new Response<List<WFStamp>>
            {
                Result = list,
                Message = "success"
            };
        }

        /// <summary>
        /// 获取管理分页列表
        /// workflow/stamp/page
        /// </summary>
        /// <param name="paginationInputDto">分页参数</param>
        /// <param name="queryParams">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<WFStamp>>>> LoadPage([FromQuery] PageReq pageReq, [FromQuery] WFStamp queryParams)
        {
            var pageInfo = await stampApp.GetPageList(pageReq, queryParams);

            return new Response<PageInfo<List<WFStamp>>>
            {
                Result = pageInfo,
                Message = "success"
            };
        }

        /// <summary>
        /// 新增签章
        /// workflow/stamp
        /// </summary>
        /// <param name="entity">提交参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> Add(WFStamp entity)
        {
            var result = new Response<bool>();
            try
            {
                result = await stampApp.SaveEntity(string.Empty, entity);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 更新签章
        /// workflow/stamp/{id}
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="entity">提交参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> Update(string id, WFStamp entity)
        {
            var result = new Response<bool>();
            try
            {
                result = await stampApp.SaveEntity(id, entity);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 删除签章
        /// workflow/stamp/{id}
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> Delete(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await stampApp.DeleteEntity(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 更新状态
        /// workflow/stamp/{id}/{state}
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> UpdateSate(string id, int state)
        {
            var result = new Response<bool>();
            try
            {
                result = await stampApp.UpdateState(id, state);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 验证密码
        /// workflow/stamp/{id}/{password}
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<bool>> EqualPassword(string id, string password)
        {
            var result = new Response<bool>();
            try
            {
                result = await stampApp.EqualPassword(id, password);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取流程文件
        /// </summary>
        /// <param name="fileId">文件id</param>
        /// <returns></returns>
        [HttpGet("system/stamp/wfimg/{fileId}")]
        [ProducesResponseType(typeof(FileContentResult), 200)]
        public FileContentResult DownAnnexesFile(string fileId)
        {
            return null;
            //var basePath = $"{ConfigHelper.GetConfig().FilePath}/Learun_WF_IMG/{fileId}";
            //byte[] arr = FileHelper.Read(basePath);
            //return File(arr, FileHelper.getContentType(fileId.Split(".")[1]), "learun_qz");
        }
    }
}
