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 Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using rmutr_report.Models;
using rmutr_report.Models.Graph;
using rmutr_report.Models.Hr;
using rmutr_report.Models.HrDevelopment;
using Swashbuckle.AspNetCore.Annotations;

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

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

        [HttpPost, Route("graph/passer_join_project_graph/{type}")]
        [ApiExplorerSettings(GroupName = "graph")]
        public IActionResult GetSum1Report([FromRoute] string type, [FromBody] List<passer_join_project_graph> reports)
        {
            Report report = new Report();
            report.Load(_setting.report_path + "passer_join_project_graph_year.frx");
            report.RegisterData(reports, "passer_join_project_graph");
            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("graph/train_graph/{type}")]
        [ApiExplorerSettings(GroupName = "graph")]
        public IActionResult GetSum2Report([FromRoute] string type, [FromBody] List<train_graph> graph)
        {
           
            //var graph = new List<passer_join_project_agency_graph>() {reports};
            Report report = new Report();
            report.Load(_setting.report_path + "train_graph.frx");
            report.RegisterData(graph, "train_graph");
            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("graph/person_study_leave_scholarships_graph/{type}")]
        [ApiExplorerSettings(GroupName = "graph")]
        public IActionResult GetSum3Report([FromRoute] string type, [FromBody] List<person_study_leave_scholarships_graph> graph)
        {
           
            //var graph = new List<passer_join_project_agency_graph>() {reports};
            Report report = new Report();
            report.Load(_setting.report_path + "study_leave_scholarships_person_graph.frx");
            report.RegisterData(graph, "person_study_leave_scholarships_graph");
            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("graph/student_graduation_notification_graph/{type}")]
        [ApiExplorerSettings(GroupName = "graph")]
        public IActionResult GetSum4Report([FromRoute] string type, [FromBody] List<student_graduation_notification_graph> graph)
        {
           
            //var graph = new List<passer_join_project_agency_graph>() {reports};
            Report report = new Report();
            report.Load(_setting.report_path + "student_graduation_notification_graph.frx");
            report.RegisterData(graph, "student_graduation_notification_graph");
            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("graph/student_not_graduate_graph/{type}")]
        [ApiExplorerSettings(GroupName = "graph")]
        public IActionResult GetSum5Report([FromRoute] string type, [FromBody] List<student_not_graduate_graph> graph)
        {
           
            //var graph = new List<passer_join_project_agency_graph>() {reports};
            Report report = new Report();
            report.Load(_setting.report_path + "student_not_graduate_graph.frx");
            report.RegisterData(graph, "student_not_graduate_graph");
            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("graph/study_results_graph/{type}")]
        [ApiExplorerSettings(GroupName = "graph")]
        public IActionResult GetSum6Report([FromRoute] string type, [FromBody] List<study_results> graph)
        {
           
            //var graph = new List<passer_join_project_agency_graph>() {reports};
            Report report = new Report();
            report.Load(_setting.report_path + "study_results_graph.frx");
            report.RegisterData(graph, "study_results");
            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("graph/person_request_extend/{type}")]
        [ApiExplorerSettings(GroupName = "graph")]
        public IActionResult GetSum7Report([FromRoute] string type, [FromBody] List<person_request_extend> graph)
        {
           
            //var graph = new List<passer_join_project_agency_graph>() {reports};
            Report report = new Report();
            report.Load(_setting.report_path + "person_request_extend_graph.frx");
            report.RegisterData(graph, "person_request_extend");
            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("graph/person_return_perform_duties/{type}")]
        [ApiExplorerSettings(GroupName = "graph")]
        public IActionResult GetSum8Report([FromRoute] string type, [FromBody] List<person_return_perform_duties> graph)
        {
           
            //var graph = new List<passer_join_project_agency_graph>() {reports};
            Report report = new Report();
            report.Load(_setting.report_path + "person_return_perform_duties_graph.frx");
            report.RegisterData(graph, "person_return_perform_duties");
            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("graph/academic_position_leave_graph/{type}")]
        [ApiExplorerSettings(GroupName = "graph")]
        public IActionResult GetSum9Report([FromRoute] string type, [FromBody] List<academic_position_leave_graph> graph)
        {
           
            //var graph = new List<passer_join_project_agency_graph>() {reports};
            Report report = new Report();
            report.Load(_setting.report_path + "academic_position_leave_graph.frx");
            report.RegisterData(graph, "academic_position_leave_graph");
            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("graph/summary_academic_position_leave_graph/{type}")]
        [ApiExplorerSettings(GroupName = "graph")]
        public IActionResult GetSum10Report([FromRoute] string type, [FromBody] List<summary_academic_position_leave_graph> graph)
        {
           
            //var graph = new List<passer_join_project_agency_graph>() {reports};
            Report report = new Report();
            report.Load(_setting.report_path + "summary_academic_position_leave_graph.frx");
            report.RegisterData(graph, "summary_academic_position_leave_graph");
            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("graph/passer_join_project_person_graph/{type}")]
        // [ApiExplorerSettings(GroupName = "graph")]
        // public IActionResult GetSum3Report([FromRoute] string type, [FromBody] List<passer_join_project_agency_graph> graph)
        // {
        //     var a = graph.ToArray();
        //     foreach (var v in a)
        //     {
        //         foreach (var vv in v.data)
        //         {
        //             if (vv.pertype != null)
        //             {
        //                 v.pertype = vv.pertype;
        //                 v.count_person_pertype = vv.data_detail.Select(g => g.display_name_th).Count();  
        //             }
        //             else
        //             {
        //                 return  null;
        //             }
        //             foreach (var detail in vv.data_detail)
        //             {
        //                 if (detail.quantity_project != null)
        //                 {
        //                     v.quantity_project = detail.quantity_project;
        //                 }
        //                 if (detail.registration_fee != null)
        //                 {
        //                     v.registration_fee = detail.registration_fee;
        //                 }
        //                 else
        //                 {
        //                     return  null;
        //                 }
        //             }
        //         }
        //        
        //     }
        //     //var graph = new List<passer_join_project_agency_graph>() {reports};
        //     Report report = new Report();
        //     report.Load(_setting.report_path + "passer_join_project_person_graph.frx");
        //     report.RegisterData(graph, "passer_join_project_agency_graph");
        //     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();
        // }
    }
}