using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Extensions;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Application.Repositories.Reports;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Swashbuckle.AspNetCore.Annotations;
using DocumentFormat.OpenXml.Drawing;
using Telerik.Reporting;
using Telerik.Reporting.Processing;
using System.IO;

namespace BMA.EHR.Report.Service.Controllers
{
    [Route("api/v{version:apiVersion}/report/resign")]
    [ApiVersion("2.0")]
    [ApiController]
    [Produces("application/json")]
    [Authorize]
    [SwaggerTag("API รายงานระบบลาออก")]
    public class ResignReportController : BaseController
    {
        #region " Fields "

        private readonly IWebHostEnvironment _hostingEnvironment;
        private readonly IConfiguration _configuration;
        private readonly RetireReportRepository _service;
        private readonly GenericReportGenerator _reportGenerator;


        #endregion

        #region " Constuctor and Destructor "

        public ResignReportController(IWebHostEnvironment hostingEnvironment, IConfiguration configuration, RetireReportRepository service, GenericReportGenerator reportGenerator)
        {
            
            _hostingEnvironment = hostingEnvironment;
            _configuration = configuration;
            _service = service;
            _reportGenerator = reportGenerator;
        }

        #endregion

        #region " Methods "

        #region 33-แบบฟอร์มหนังสือขอลาออกจากราชการ
        /// <summary>
        /// 33-แบบฟอร์มหนังสือขอลาออกจากราชการ
        /// </summary>
        /// <param name="id">id </param>
        /// <param name="exportType">pdf, docx หรือ xlsx</param>
        /// <returns></returns>
        /// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
        /// <response code="401">ไม่ได้ Login เข้าระบบ</response>
        /// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
        [HttpGet("33/{exportType}/{id}")]
        public async Task<ActionResult<ResponseObject>> GetResign33ConvertReportAsync(Guid id, string exportType = "pdf")
        {
            try
            {
                var resign = await _service.GetResignByUser(id);
                if (resign == null)
                    return NotFound();
                
                var mimeType = "";
                switch (exportType.Trim().ToLower())
                {
                    case "pdf": mimeType = "application/pdf"; break;
                    case "docx": mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break;
                    case "xlsx": mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break;
                }
                //var FullName = resign.GetType().GetProperty("FirstName").GetValue(resign);

                //var rptFile1 = System.IO.Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", $"33-แบบฟอร์มหนังสือขอลาออกจากราชการ-1.trdp");
                //var rptFile2 = System.IO.Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", $"33-แบบฟอร์มหนังสือขอลาออกจากราชการ-2.trdp");

                //ReportPackager reportPacker = new ReportPackager();
                //Telerik.Reporting.Report? report1 = null;
                //Telerik.Reporting.Report? report2 = null;

                //using (var sourceStream1 = System.IO.File.OpenRead(rptFile1))
                //using (var sourceStream2 = System.IO.File.OpenRead(rptFile2))

                //{
                //    report1 = (Telerik.Reporting.Report)reportPacker.UnpackageDocument(sourceStream1);
                //    report2 = (Telerik.Reporting.Report)reportPacker.UnpackageDocument(sourceStream2);
                //}

                //report1.DataSource = resign;

                //var reportBook = new ReportBook();
                //reportBook.Reports.Add(report1);
                //reportBook.Reports.Add(report2);

                //System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
                //InstanceReportSource instanceReportSource = new InstanceReportSource()
                //{
                //    ReportDocument = reportBook,
                //};

                //ReportProcessor reportProcessor = new ReportProcessor(_configuration);
                //RenderingResult result = reportProcessor.RenderReport($"{exportType}", instanceReportSource, deviceInfo);
                //return File(result.DocumentBytes, mimeType, $"แบบฟอร์มหนังสือขอลาออกจากราชการ.{exportType.Trim().ToLower()}");
                var data = new
                {
                    template = "resign",
                    reportName = "docx-report",
                    data = resign
                };
                return Success(data);
            }
            catch
            {
                throw;
            }
        }
        #endregion

        #endregion
    }
}
