using System;
using System.Collections.Generic;
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 rmutr_report.Models.HrDevelopment;
using rmutr_report.Models.Hrrecruit;
using Swashbuckle.AspNetCore.Annotations;

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

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

        [HttpPost, Route("reports/passer_join_project/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr1Report([FromRoute] string type, [FromBody] List<passer_join_project> _hr)
        {
            // foreach (var deatil in _hr)
            // {
            //     foreach (var x in deatil.data)
            //     {
            //         if (x.quantity_person != null)
            //         {
            //             var total = deatil.data.Select(r => r.quantity_person).Sum(t=>t.Value);
            //             deatil.total_1 = total;
            //         }
            //         else
            //         {
            //             deatil.total_1 = 0;
            //         }
            //         if (x.quantity_trained != null)
            //         {
            //             var total =  deatil.data.Select(r => r.quantity_trained).Sum(t=>t.Value);
            //             deatil.total_2 = total;
            //         }else
            //         {
            //             deatil.total_2 = 0;
            //         }
            //     }
            //     }
           // var hr1 = new List<passer_join_project>() {_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "passer_join_project.frx");
            report.RegisterData(_hr, "passer_join_project");
            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":  case "xlsx":
                    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/passer_join_project_agency/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr2Report([FromRoute] string type, [FromBody] List<passer_join_project_agency> _hr)
        {
            Report report = new Report();
            report.Load(_setting.report_path + "passer_join_project_agency.frx");
            report.RegisterData(_hr, "passer_join_project_agency");
            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":  case "xlsx":
                    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/passer_join_project_person/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr3Report([FromRoute] string type, [FromBody] List<passer_join_project_agency> _hr)
        {
            Report report = new Report();
            report.Load(_setting.report_path + "passer_join_project_person.frx");
            report.RegisterData(_hr, "passer_join_project_agency");
            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":  case "xlsx":
                    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/person_academic_supportlines/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr4Report([FromRoute] string type, [FromBody] List<person_academic_and_supportlines> _hr)
        {
           Report report = new Report();
            report.Load(_setting.report_path + "person_academic_and_supportlines.frx");
            report.RegisterData(_hr, "person_academic_and_supportlines");
            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":  case "xlsx":
                    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/person_study_leave_scholarships/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr5Report([FromRoute] string type, [FromBody] List<person_study_leave_scholarships> _hr)
        {
            Report report = new Report();
            report.Load(_setting.report_path + "study_leave_scholarships_person.frx");
            report.RegisterData(_hr, "person_study_leave_scholarships");
            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":  case "xlsx":
                    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/study_leave_scholarships_year/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr6Report([FromRoute] string type, [FromBody] study_leave_scholarships_year _hr)
        {
            var hr = new List<study_leave_scholarships_year>(){_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "study_leave_scholarships_year.frx");
            report.RegisterData(hr, "study_leave_scholarships_year");
            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":  case "xlsx":
                    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/return_perform_duties_year/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr7Report([FromRoute] string type, [FromBody] return_perform_duties_year _hr)
        {
            var hr = new List<return_perform_duties_year>(){_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "return_perform_duties_year.frx");
            report.RegisterData(hr, "return_perform_duties_year");
            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":  case "xlsx":
                    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/study_leave_expand_study_period/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr8Report([FromRoute] string type, [FromBody] study_leave_expand_study_period _hr)
        {
            var hr = new List<study_leave_expand_study_period>(){_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "study_leave_expand_study_period.frx");
            report.RegisterData(hr, "study_leave_expand_study_period");
            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":  case "xlsx":
                    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/study_report/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr9Report([FromRoute] string type, [FromBody] study_report _hr)
        {
            var hr = new List<study_report>(){_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "study_report.frx");
            report.RegisterData(hr, "study_report");
            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":  case "xlsx":
                    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/summary_graduation_notification/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr10Report([FromRoute] string type, [FromBody] summary_graduation_notification _hr)
        {
            var hr = new List<summary_graduation_notification>(){_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "summary_graduation_notification.frx");
            report.RegisterData(hr, "summary_graduation_notification");
            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":  case "xlsx":
                    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/summary_not_graduation/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr11Report([FromRoute] string type, [FromBody] summary_not_graduation _hr)
        {
            var hr = new List<summary_not_graduation>(){_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "summary_not_graduation.frx");
            report.RegisterData(hr, "summary_not_graduation");
            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":  case "xlsx":
                    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/progress_report/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr12Report([FromRoute] string type, [FromBody] List<progress_report> _hr)
        {
            //var hr = new List<summary_not_graduation>(){_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "progress_report.frx");
            report.RegisterData(_hr, "progress_report");
            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":  case "xlsx":
                    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_leave/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr13Report([FromRoute] string type, [FromBody] academic_position_leave _hr)
        {
            var hr = new List<academic_position_leave>(){_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "academic_position_leave.frx");
            report.RegisterData(hr, "academic_position_leave");
            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":  case "xlsx":
                    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/company_database/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr14Report([FromRoute] string type, [FromBody] company_database _hr)
        {
            var hr = new List<company_database>(){_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "company_database.frx");
            report.RegisterData(hr, "company_database");
            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":  case "xlsx":
                    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/summary_academic_position_leave/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult Gethr15Report([FromRoute] string type, [FromBody] summary_academic_position_leave _hr)
        {
            var hr = new List<summary_academic_position_leave>(){_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "summary_academic_position_leave.frx");
            report.RegisterData(hr, "summary_academic_position_leave");
            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":  case "xlsx":
                    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();
        }
    }
}