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("สำหรับรายงานคำขอโครงการ Thailand 4.0 (ง.5-2.2)")]
    public class project_request_thailand: Controller
    {
         readonly Setting _setting;

        public project_request_thailand(Setting setting)
        {
            this._setting = setting;
        }
       [HttpPost, Route("reports/project_request_thailand/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetRequestProjectReport([FromRoute] string type, [FromBody] project_request_thai 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<project_request_thai>() {project};

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

    }
}