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")]
    public class ProjectPrincipleRationale: Controller
    {
         readonly Setting _setting;

        public ProjectPrincipleRationale(Setting setting)
        {
            this._setting = setting;
        }
       [HttpPost, Route("reports/project_principle_rationale/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetProjectReport([FromRoute] string type, [FromBody] project_principle_rationale project)
        {
            var total = project.activity.Count;
            //var total2 = project.sixteenth.Count;
            foreach (var v in project.activity)
            {
                if (v.activity != null)
                {
                    //var total = project.activity.Select(r => r.count).Sum(t => t.Value);
                    project.together_with = "ประกอบด้วยกิจกรรมหรือโครงการย่อย " + total +" โครงการดังนี้คือ";
                }
                else
                {
                    project.together_with = null;
                }
            }
            foreach (var v in project.eighth)
            {
                string na = "ประเด็นยุทธศาสตร์ที่ " ;
                v.strategic_issues = na + v.strategic_issues;
                string na2 = "กลยุทธ์ที่ " ;
                v.measure = na2 + v.measure;
            }
            foreach (var v in project.fourteenth)
            {
                string na = "องค์ประกอบที่ " ;
                v.component = na + v.component;
                string na2 = "ตัวบ่งชี้ที่ " ;
                v.indicator = na2 + v.indicator;
           }
           // project.count_activity =  total2;
            foreach (var v in project.eighteenth)
            {
                foreach (var vv in v.eighteenths)
                {

                    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 + " วัน";
                   }
                }
            }

            if (project.total_target != null)
            {
                project.text1 = "ผลรวมกลุ่มเป้าหมาย  " + project.total_target;
            }
            if (project.total_board != null)
            {
                project.text2 = "ผลรวมคณะกรรมการ  " + project.total_board;
            }
            if (project.twenty_one != null)
            {


                //  foreach (var vv in project.twenty_one)
                //  {
                //      if (vv.activity != null || vv.activity!= "")
                //      {
                //          vv.activity = vv.activity;
                //      }
                //      if (vv.clause1 != null || vv.clause1!= "")
                //      {
                //          vv.clause1 = vv.clause1;
                //      }
                //      if (vv.clause2 != null || vv.clause2!= "")
                //      {
                //          vv.clause2 = vv.clause2;
                //      }
                //      if (vv.clause3 != null || vv.clause3!= "")
                //      {
                //          vv.clause3  = vv.clause3;
                //      }
                //      if (vv.clause4 != null || vv.clause4!= "")
                //      {
                //          vv.clause4  = vv.clause4;
                //      }
                //      if (vv.count_unit != null || vv.count_unit != "")
                //      {
                //          string na = "ผู้เข้าร่วมโครงการ  จำนวน ";
                //          string na2 = " หน่วยนับ";
                //          project.text1 = na + vv.count_unit + na2;
                //      }
                //
                //      if (vv.count_project != null || vv.count_project != "")
                //      {
                //          string na = "จำนวนครั้งที่ดำเนินโครงการ ";
                //          string na2 = " ครั้ง";
                //          project.text2 = na + vv.count_project + na2;
                //      }
                //
                //      if (vv.count_training != null || vv.count_training != "")
                //      {
                //          string na = "จำนวนการจัดอบรมและสัมมนา ";
                //          string na2 = " เรื่อง";
                //          project.text3 = na + vv.count_training + na2;
                //      }
                //
                //      if (vv.count_knowledge != null || vv.count_knowledge != "")
                //      {
                //          string na = "จำนวนเผยแพร่ความรู้บริการวิชาการ ";
                //          string na2 = " ครั้ง";
                //          project.text4 = na + vv.count_knowledge + na2;
                //      }
                //      string na3 = "วัน/เดือน/ปี ที่จะดำเนินโครงการ ";
                //          string na4 = " ถึง ";
                //          project.text5 = na3 + vv.project_start_date + na4 + vv.project_end_date;
                //          string na5 = "โครงการแล้วเสร็จตามเวลาที่กำหนด (ร้อยละ) ";
                //          project.text6 = na5 + vv.percentage_finish;
                //      
                // }

                if (project.twenty_one == null)
                {
                    project.twenty_one = null;
                }
            }

            var _project = new List<project_principle_rationale>() {project};

            Report report = new Report();
            report.Load(_setting.report_path + "project_principle_rationale.frx");
            report.RegisterData(_project, "project_principle_rationale");
            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();
        }
[HttpPost, Route("reports/project_principle_rationale_no_activity/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetProject2Report([FromRoute] string type, [FromBody] project_principle_rationale project)
        {
            var total = project.activity.Count;
            //var total2 = project.sixteenth.Count;
            foreach (var v in project.activity)
            {
                if (v.activity != null)
                {
                    //var total = project.activity.Select(r => r.count).Sum(t => t.Value);
                    project.together_with = "ประกอบด้วยกิจกรรมหรือโครงการย่อย " + total +" โครงการดังนี้คือ";
                }
                else
                {
                    project.together_with = null;
                }
            }
            foreach (var v in project.eighth)
            {
                string na = "ประเด็นยุทธศาสตร์ที่ " ;
                v.strategic_issues = na + v.strategic_issues;
                string na2 = "กลยุทธ์ที่ " ;
                v.measure = na2 + v.measure;
            }
            foreach (var v in project.fourteenth)
            {
                string na = "องค์ประกอบที่ " ;
                v.component = na + v.component;
                string na2 = "ตัวบ่งชี้ที่ " ;
                v.indicator = na2 + v.indicator;
           }
           // project.count_activity =  total2;
            foreach (var v in project.eighteenth)
            {
                foreach (var vv in v.eighteenths)
                {

                    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 + " วัน";
                   }
                }
            }

            if (project.total_target != null)
            {
                project.text1 = "ผลรวมกลุ่มเป้าหมาย  " + project.total_target;
            }
            if (project.total_board != null)
            {
                project.text2 = "ผลรวมคณะกรรมการ  " + project.total_board;
            }
            if (project.twenty_one != null)
            {


                //  foreach (var vv in project.twenty_one)
                //  {
                //      if (vv.activity != null || vv.activity!= "")
                //      {
                //          vv.activity = vv.activity;
                //      }
                //      if (vv.clause1 != null || vv.clause1!= "")
                //      {
                //          vv.clause1 = vv.clause1;
                //      }
                //      if (vv.clause2 != null || vv.clause2!= "")
                //      {
                //          vv.clause2 = vv.clause2;
                //      }
                //      if (vv.clause3 != null || vv.clause3!= "")
                //      {
                //          vv.clause3  = vv.clause3;
                //      }
                //      if (vv.clause4 != null || vv.clause4!= "")
                //      {
                //          vv.clause4  = vv.clause4;
                //      }
                //      if (vv.count_unit != null || vv.count_unit != "")
                //      {
                //          string na = "ผู้เข้าร่วมโครงการ  จำนวน ";
                //          string na2 = " หน่วยนับ";
                //          project.text1 = na + vv.count_unit + na2;
                //      }
                //
                //      if (vv.count_project != null || vv.count_project != "")
                //      {
                //          string na = "จำนวนครั้งที่ดำเนินโครงการ ";
                //          string na2 = " ครั้ง";
                //          project.text2 = na + vv.count_project + na2;
                //      }
                //
                //      if (vv.count_training != null || vv.count_training != "")
                //      {
                //          string na = "จำนวนการจัดอบรมและสัมมนา ";
                //          string na2 = " เรื่อง";
                //          project.text3 = na + vv.count_training + na2;
                //      }
                //
                //      if (vv.count_knowledge != null || vv.count_knowledge != "")
                //      {
                //          string na = "จำนวนเผยแพร่ความรู้บริการวิชาการ ";
                //          string na2 = " ครั้ง";
                //          project.text4 = na + vv.count_knowledge + na2;
                //      }
                //      string na3 = "วัน/เดือน/ปี ที่จะดำเนินโครงการ ";
                //          string na4 = " ถึง ";
                //          project.text5 = na3 + vv.project_start_date + na4 + vv.project_end_date;
                //          string na5 = "โครงการแล้วเสร็จตามเวลาที่กำหนด (ร้อยละ) ";
                //          project.text6 = na5 + vv.percentage_finish;
                //      
                // }

                if (project.twenty_one == null)
                {
                    project.twenty_one = null;
                }
            }

            var _project = new List<project_principle_rationale>() {project};

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

    
    }
}