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 Swashbuckle.AspNetCore.Annotations;

namespace rmutr_report.Controllers
{
    [SwaggerTag("สำหรับรายงานสรุปแผนความต้องการ")]
    public class SummaryController: Controller
    {
         readonly Setting _setting;

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

        [HttpPost, Route("reports/summary_of_equipment/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetSum1Report([FromRoute] string type, [FromBody] summary_of_equipment summary_of_equipments)
        {
          
           var _summary_of_equipment = new List<summary_of_equipment>() { summary_of_equipments };

            Report report = new Report();
            report.Load(_setting.report_path + "summary_of_equipment.frx");
            report.RegisterData(_summary_of_equipment, "summary_of_equipment");
            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/summary_of_equipment_five_year/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetEquipmentReport([FromRoute] string type, [FromBody] summary_of_equipment_five_year summary_of_equipments)
        {
          
            var _summary_of_equipment = new List<summary_of_equipment_five_year>() { summary_of_equipments };

            Report report = new Report();
            report.Load(_setting.report_path + "summary_of_equipment_5_year.frx");
            report.RegisterData(_summary_of_equipment, "summary_of_equipment_five_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":
                    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/statement_of_equipment/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetStEquipmentReport([FromRoute] string type, [FromBody] statement_of_equipment equipments)
        {
            var a = equipments.page_1.ToArray();
            foreach (var aa in a)
            {
                var s1 = aa.page_1_detail.Sum(d => d.quantity);
                var s2 = aa.page_1_detail.Sum(d => d.unit_price);
                var s3 = aa.page_1_detail.Sum(d => d.amount);
                foreach (var b in aa.page_1_detail)
                {
                    if (b.quantity != null)
                    {
                        aa.sum1 = s1;
                    }
                    if (b.quantity == null)
                    {
                        aa.sum1 = 0;
                    }
                    if (b.unit_price != null)
                    {
                        aa.sum2 = s2;
                    }
                    if (b.unit_price == null)
                    {
                        aa.sum2 = 0;
                    }
                    if (b.amount != null)
                    {
                        aa.sum3 = s3;
                    }
                    if (b.amount == null)
                    {
                        aa.sum3 = 0;
                    }
                }
            }
          
            var _statement_of_equipment = new List<statement_of_equipment>() { equipments };

            Report report = new Report();
            report.Load(_setting.report_path + "statement_of_equipment.frx");
            report.RegisterData(_statement_of_equipment, "statement_of_equipment");
            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/summary_building_construction/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetSum2Report([FromRoute] string type, [FromBody] summary_building_construction summary_building_constructions)
        {
          
            var _summary_building_constructions = new List<summary_building_construction>() { summary_building_constructions };

            Report report = new Report();
            report.Load(_setting.report_path + "summary_building_constructions.frx");
            report.RegisterData(_summary_building_constructions, "summary_building_construction");
            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();
        }
    }
}