using System.Collections.Generic;
using System.IO;
using System.Linq;
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-3")]
    public class project_under_integrated_plan: Controller
    {
         readonly Setting _setting;

        public project_under_integrated_plan(Setting setting)
        {
            this._setting = setting;
        }
       [HttpPost, Route("reports/project_under_integrated/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetIntegratedProjectReport([FromRoute] string type, [FromBody] project_under_integrated project)
        {
          
            foreach (var aa in project.sixteen)
            {
                var a = project.sixteen.Where(p => p.operating_budget == p.operating_budget)
                    .Sum(j => j.operating_budget);
                var b = project.sixteen.Where(p => p.investment_budget == p.investment_budget)
                    .Sum(j => j.investment_budget);
                var c = project.sixteen.Where(p => p.other_budget == p.other_budget)
                    .Sum(j => j.other_budget);
                var d = project.sixteen.Where(p => p.subsidy_budget == p.subsidy_budget)
                    .Sum(j => j.subsidy_budget);
                project.operating_budget = a;
                project.investment_budget = b;
                project.other_budget = c;
                project.subsidy_budget = d;
                project.total_budget_all = a+b+c+d;

            }
            
            // 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;
            // }
            // }
          
        var _project = new List<project_under_integrated>() {project};

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

    }
}