using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using FastReport;
using FastReport.Export.Csv;
using FastReport.Export.Mht;
using FastReport.Export.OoXML;
using FastReport.Export.Pdf;
using Microsoft.AspNetCore.Mvc;
using rmutr_report.Models;
using rmutr_report.Models.Hr;
using Swashbuckle.AspNetCore.Annotations;

namespace rmutr_report.Controllers
{
    [SwaggerTag("สำหรับรายงาน HR")]
    public class PersonnelController : Controller
    {
        readonly Setting _setting;

        public PersonnelController(Setting setting)
        {
            this._setting = setting;
        }

        [HttpPost, Route("reports/personnel_summary/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHrReport([FromRoute] string type, [FromBody] personnel_summary personnel_summarys)
        {
          foreach (var v in personnel_summarys.personnel_types)
            {
                if (v.count != null)
                {
                    var total = personnel_summarys.personnel_types.Select(r => r.count).Sum(t=>t.Value);
                    personnel_summarys.total_pertype = total;
                }
                else
                {
                    return null;
                }
            }
           var personnel_summaryss = new List<personnel_summary>() { personnel_summarys };

            Report report = new Report();
            report.Load(_setting.report_path + "personnel_summary.frx");
            report.RegisterData(personnel_summaryss, "personnel_summary");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }
[HttpPost, Route("reports/man_power/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetManReport([FromRoute] string type, [FromBody] List<man_power> man_powers)
        {
         
           //var personnel_summaryss = new List<personnel_summary>() { personnel_summarys };

            Report report = new Report();
            report.Load(_setting.report_path + "man_power.frx");
            report.RegisterData(man_powers, "man_power");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }
        [HttpPost, Route("reports/data_line_support/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetDataLineReport([FromRoute] string type, [FromBody] data_line_support data_line_supports)
        {
         
            var data_line_supportss = new List<data_line_support>() { data_line_supports };

            Report report = new Report();
            report.Load(_setting.report_path + "data_line_support.frx");
            report.RegisterData(data_line_supportss, "data_line_support");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }
        [HttpPost, Route("reports/data_line_academic/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetDataLineAcaReport([FromRoute] string type, [FromBody] data_line_academic data_line_academicss)
        {
         
            var _data_line_academics = new List<data_line_academic>() { data_line_academicss };

            Report report = new Report();
            report.Load(_setting.report_path + "data_line_academic.frx");
            report.RegisterData(_data_line_academics, "data_line_academic");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }
        [HttpPost, Route("reports/qualifications_teacher/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetQualificationsTeacherReport([FromRoute] string type, [FromBody] qualifications_teacher qualifications_teachers)
        {
         
            var qualifications_teacherss = new List<qualifications_teacher>() { qualifications_teachers };

            Report report = new Report();
            report.Load(_setting.report_path + "qualifications_teacher.frx");
            report.RegisterData(qualifications_teacherss, "qualifications_teacher");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }
        [HttpPost, Route("reports/academic_position/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetAcademicPositionReport([FromRoute] string type, [FromBody] academic_position _academic_position)
        {
         
            var academic_positions = new List<academic_position>() { _academic_position };

            Report report = new Report();
            report.Load(_setting.report_path + "academic_position.frx");
            report.RegisterData(academic_positions, "academic_position");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }
    }
}