using System.Collections.Generic;
using System.IO;
using ClosedXML.Excel;
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 Swashbuckle.AspNetCore.Annotations;

namespace rmutr_report.Controllers
{
    [SwaggerTag("สำหรับรายงานคำขอโครงการพันธุกรรมพืช ง.5-2.1")]
    public class plant_genetic_conservation_project: Controller
    {
         readonly Setting _setting;

        public plant_genetic_conservation_project(Setting setting)
        {
            this._setting = setting;
        }
       [HttpPost, Route("reports/plant_genetic_conservation_project/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetConservationProjectReport([FromRoute] string type, [FromBody] conservation_project project)
        {
            
            foreach (var v in project.five)
            {
                string na = "ประเด็นยุทธศาสตร์ที่ " ;
                v.strategic_issues = na + v.strategic_issues;
                string na2 = "กลยุทธ์ที่ " ;
                v.measure = na2 + v.measure;
            }
            foreach (var v in project.eleventh)
            {
                foreach (var vv in v.elevenths)
                {

                    string na = "ครั้งที่ ";
                    vv.the_time = na +  vv.the_time;
                    // string na2 = " จำนวน ";
                    // vv.count_day = na2 + vv.count_day + " วัน";
                    // if (project.count_day != null)
                    // {
                    //     project.text3 = "จำนวน " + project.count_day + " วัน";
                    // }
                }
            }
          
        var _project = new List<conservation_project>() {project};

            Report report = new Report();
            report.Load(_setting.report_path + "plant_genetic_conservation_project.frx");
            report.RegisterData(_project, "conservation_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;
                case "doc": case "docx":
                    Word2007Export word = new Word2007Export();
                    report.Export(word, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "appllication/vnd.ms-word");
                    break;
            }

            return Ok();
        }

    }
}