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

namespace rmutr_report.Controllers
{
    [SwaggerTag("สำหรับรายงาน ง.3")]
    public class DurableArticlesThree : Controller
    {
        readonly Setting _setting;

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

        [HttpPost, Route("reports/durable_articles_three/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHrReport([FromRoute] string type,
            [FromBody] durable_articles_three durable_articles_threes)
        {
           
                var a1 = durable_articles_threes.data.Where(d => d.amount == d.amount)
                    .Sum(o => o.amount);
                durable_articles_threes.total_amount = a1;
                
                int a = 1;
                foreach (var q in durable_articles_threes.quotation)
                {
                    
                    string t = "ใบเสนอราคาบริษัทที่ " + a++ +" ";
                    q.company = t + q.company;
                    //a++;
                }

                foreach (var detail in durable_articles_threes.invest_detail)
                {
                    
                }

            var _durable_articles_three = new List<durable_articles_three>() {durable_articles_threes};

            Report report = new Report();
            report.Load(_setting.report_path + "durable_articles_three.frx");
            report.RegisterData(_durable_articles_three, "durable_articles_three");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "view":
                    return File(stream, "application/pdf");
                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();
        }

    }
}