using System;
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 rmutr_report.Models.Hr;
using Swashbuckle.AspNetCore.Annotations;

namespace rmutr_report.Controllers
{
    [SwaggerTag("สำหรับรายงานนักศึกษาเข้าใหม่หลักสูตรบัณฑิตศึกษา")]
    public class StdNewsGraduateProgram : Controller
    {
        readonly Setting _setting;

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

        [HttpPost, Route("reports/student_news_graduate_program/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetStdNews1Report([FromRoute] string type,
            [FromBody] student_news_graduate_program _student_news_faculty)
        {
            var std = _student_news_faculty.data;
            var a1 = _student_news_faculty.data.Where(d => d.ph_degree == d.ph_degree)
                .Sum(o => o.ph_degree);
            var a2 = _student_news_faculty.data.Where(d => d.ph_master_degree == d.ph_master_degree)
                .Sum(o => o.ph_master_degree);
            var a3 = _student_news_faculty.data.Where(d => d.master_degree == d.master_degree)
                .Sum(o => o.master_degree);
            foreach (var data in std)
            {
                data.total1 = a1;
                data.total2 = a2;
                data.total3 = a3;
                var total = data.total1 + data.total2 + data.total3;
                var total2 = data.ph_degree + data.ph_master_degree + data.master_degree;
                var total3 = (total2 * 100) / total;
                if (data.ph_degree != 0 || data.ph_master_degree != 0 || data.master_degree != 0)
                {
                    data.sum_degree = data.ph_degree + data.ph_master_degree + data.master_degree;
                    //data.sum_percentage = (total2 * 100) / total;
                }

                data.sum_percentage = (total2 * 100) / total;

                data.total4 = a1 + a2 + a3;
                data.total5 = total3;
            }

            var student_news_facultys = new List<student_news_graduate_program>() {_student_news_faculty};

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

            return Ok();
        }

        [HttpPost, Route("reports/student_graduate_program_area/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetStdNews2Report([FromRoute] string type,
            [FromBody] student_graduate_program_area _student_news_faculty)
        {
            var std = _student_news_faculty.data;
            var a1 = _student_news_faculty.data.Where(d => d.ph_degree_science == d.ph_degree_science)
                .Sum(o => o.ph_degree_science);
            var a2 = _student_news_faculty.data.Where(d => d.ph_degree_social == d.ph_degree_social)
                .Sum(o => o.ph_degree_social);
            var a3 = _student_news_faculty.data.Where(d => d.ph_master_science == d.ph_master_science)
                .Sum(o => o.ph_master_science);
            var a4 = _student_news_faculty.data.Where(d => d.ph_master_social == d.ph_master_social)
                .Sum(o => o.ph_master_social);
            var a5 = _student_news_faculty.data.Where(d => d.master_science == d.master_science)
                .Sum(o => o.master_science);
            var a6 = _student_news_faculty.data.Where(d => d.master_social == d.master_social)
                .Sum(o => o.master_social);

            foreach (var data in std)
            {
                var s1 = data.ph_degree_science + data.master_science;
                var s2 = data.ph_degree_social + data.master_social;
                var s3 = data.ph_degree_science + data.master_science;
                var s4 = data.ph_degree_science + data.master_science + data.ph_degree_social + data.master_social;
                data.ph_total = data.ph_degree_science + data.ph_degree_social;
                data.ph_master_total = data.ph_master_science + data.ph_master_social;
                data.master_total = data.master_science + data.master_social;
                data.total1 = a1;
                data.total2 = a2;
                data.total3 = a1 + a2;
                data.total4 = a3;
                data.total5 = a4;
                data.total6 = a3 + a4;
                data.total7 = a5;
                data.total8 = a6;
                data.total9 = a5 + a6;
                data.sum_degree_science = data.ph_degree_science + data.master_science;
                data.sum_degree_social = data.ph_degree_social + data.master_social;
                data.total10 = data.total1 + data.total7;
                data.total11 = data.total2 + data.total8;

                data.sum_degree = s4;
                data.total12 = data.total10 + data.total11;
                //data.sum_degree = (data.ph_degree_science + data.master_science)+(data.ph_degree_social + data.master_social);
                // if (data.ph_degree_science != 0 && data.master_science != 0 && data.sum_degree_science != 0&& data.sum_degree_social != 0)
                // {
                    data.sum_percentage_science = ((data.ph_degree_science + data.master_science) * 100) /
                                                  (data.sum_degree_science + data.sum_degree_social);
                //}

                // data.sum_percentage_science = ((data.ph_degree_science + data.master_science) * 100) /
                //                               (data.sum_degree_science + data.sum_degree_social);
                
                // if (data.ph_degree_social != 0 && data.master_social != 0 && data.sum_degree_science != 0&& data.sum_degree_social != 0)
                // {
                    data.sum_percentage_social = ((data.ph_degree_social + data.master_social) * 100) /
                                                 (data.sum_degree_science + data.sum_degree_social);
                //}
                // data.sum_percentage_social = ((data.ph_degree_social + data.master_social) * 100) /
                //                              (data.sum_degree_science + data.sum_degree_social);

                data.total13 = (data.total10 * 100) / data.total12;
                data.total14 = (data.total11 * 100) / data.total12;
            }

            var student_news_facultys = new List<student_graduate_program_area>() {_student_news_faculty};

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

            return Ok();
        }

        [HttpPost, Route("reports/student_graduate_program_faculty/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetStdNews3Report([FromRoute] string type,
            [FromBody] student_graduate_program_faculty _student_news_faculty)
        {
            var std = _student_news_faculty.data;
            var a1 = _student_news_faculty.data.Where(d => d.ph_degree_science == d.ph_degree_science)
                .Sum(o => o.ph_degree_science);
            var a2 = _student_news_faculty.data.Where(d => d.ph_master_degree_science == d.ph_master_degree_science)
                .Sum(o => o.ph_master_degree_science);
            var a3 = _student_news_faculty.data.Where(d => d.master_science == d.master_science)
                .Sum(o => o.master_science);
            var a4 = _student_news_faculty.data.Where(d => d.sum_degree_ph == d.sum_degree_ph)
                .Sum(o => o.sum_degree_ph);
            var a5 = _student_news_faculty.data.Where(d => d.ph_degree_social == d.ph_degree_social)
                .Sum(o => o.ph_degree_social);
            var a6 = _student_news_faculty.data.Where(d => d.ph_master_degree_social == d.ph_master_degree_social)
                .Sum(o => o.ph_master_degree_social);
            var a7 = _student_news_faculty.data.Where(d => d.master_social == d.master_social)
                .Sum(o => o.master_social);
            var a8 = _student_news_faculty.data.Where(d => d.social_total == d.social_total)
                .Sum(o => o.social_total);
            foreach (var data in std)
            {
                var s1 =  data.science_total +data.social_total;
                
                data.total1 = a1;
                data.total2 = a2;
                data.total3 = a3;
                data.science_total = data.ph_degree_science + data.ph_master_degree_science + data.master_science;
                data.total4 = data.total1 + data.total2 + data.total3;
                
                data.total5 = a5;
                data.total6 = a6;
                data.total7 = a7;
                data.social_total = data.ph_degree_social + data.ph_master_degree_social + data.master_social;
                data.total8 = data.total5 + data.total6 + data.total7;
                data.sum_degree_ph = data.ph_degree_science + data.ph_degree_social;
                data.sum_degree_ph_master = data.ph_master_degree_science + data.ph_master_degree_social;
                data.sum_degree_master = data.master_science + data.master_social;
                data.sum_degree = data.sum_degree_ph + data.sum_degree_ph_master+data.sum_degree_master;
                data.total9 = data.total1 + data.total5;
                data.total10 = data.total2 + data.total6;
                data.total11 = data.total3 + data.total7;
                data.total12 = data.total9 + data.total10+data.total11;

                data.sum_percentage_science = ((data.ph_degree_science + data.ph_master_degree_science + data.master_science) * 100) /
                                              (data.science_total +data.social_total);
                data.sum_percentage_social = ((data.ph_degree_social + data.ph_master_degree_social + data.master_social) * 100) /
                                             (data.science_total +data.social_total);

                if (data.total4 != 0 && data.total12!= 0 )
                {
                    data.total13 = (data.total4 * 100) / data.total12;

                }
                if (data.total8 != 0 && data.total12!= 0 )
                {
                    data.total14 = (data.total8 * 100) / data.total12;

                }
                //data.total13 = (data.total4 * 100) / data.total12;
                //data.total14 = (data.total8 * 100) / data.total12;
            }

            var student_news_facultys = new List<student_graduate_program_faculty>() {_student_news_faculty};

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

            return Ok();
        }
         [HttpPost, Route("reports/student_graduate_program_year/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetStdNews4Report([FromRoute] string type,
            [FromBody] student_graduate_program_year _student_news_faculty)
        {
            var std = _student_news_faculty.data;
            var a1 = _student_news_faculty.data.Where(d => d.ph_degree_science1 == d.ph_degree_science1)
                .Sum(o => o.ph_degree_science1);
            var a2 = _student_news_faculty.data.Where(d => d.ph_master_degree_science1 == d.ph_master_degree_science1)
                .Sum(o => o.ph_master_degree_science1);
            var a3 = _student_news_faculty.data.Where(d => d.master_science1 == d.master_science1)
                .Sum(o => o.master_science1);
            var a4 = _student_news_faculty.data.Where(d => d.ph_degree_social1 == d.ph_degree_social1)
                .Sum(o => o.ph_degree_social1);
            var a5 = _student_news_faculty.data.Where(d => d.ph_master_degree_social1 == d.ph_master_degree_social1)
                .Sum(o => o.ph_master_degree_social1);
            var a6 = _student_news_faculty.data.Where(d => d.master_social1 == d.master_social1)
                .Sum(o => o.master_social1);
            var a7 = _student_news_faculty.data.Where(d => d.ph_degree_science2 == d.ph_degree_science2)
                .Sum(o => o.ph_degree_science2);
            var a8 = _student_news_faculty.data.Where(d => d.ph_master_degree_science2 == d.ph_master_degree_science2)
                .Sum(o => o.ph_master_degree_science2);
            var a9 = _student_news_faculty.data.Where(d => d.master_science2 == d.master_science2)
                .Sum(o => o.master_science2);
            var a10 = _student_news_faculty.data.Where(d => d.ph_degree_social2 == d.ph_degree_social2)
                .Sum(o => o.ph_degree_social2);
            var a11 = _student_news_faculty.data.Where(d => d.ph_master_degree_social2 == d.ph_master_degree_social2)
                .Sum(o => o.ph_master_degree_social2);
            var a12 = _student_news_faculty.data.Where(d => d.master_social2 == d.master_social2)
                .Sum(o => o.master_social2);
            foreach (var data in std)
            {
                data.science_total1 = data.ph_degree_science1 + data.ph_master_degree_science1 + data.master_science1;
                data.social_total1 = data.ph_degree_social1 + data.ph_master_degree_social1 + data.master_social1;
                data.science_social_total1 = data.science_total1+ data.social_total1;
                data.total1 = a1;
                data.total2 = a2;
                data.total3 = a3;
                data.total4 = data.total1 + data.total2 + data.total3;
                
                data.total5 = a4;
                data.total6 = a5;
                data.total7 = a6;
                data.total8 = data.total5 + data.total6 + data.total7;
                data.total9 = data.total4 + data.total8;
                
                data.science_total2 = data.ph_degree_science2 + data.ph_master_degree_science2 + data.master_science2;
                data.social_total2 = data.ph_degree_social2 + data.ph_master_degree_social2 + data.master_social2;
                data.science_social_total2 = data.science_total2+ data.social_total2;
                data.total10 = a7;
                data.total11 = a8;
                data.total12 = a9;
                
                data.total13 = data.total10 + data.total11 + data.total12;
                data.total14 = a10;
                data.total15 = a11;
                data.total16 = a12;
                data.total17 = data.total14 + data.total15 + data.total16;
                data.total18 = data.total13 + data.total17;
                if (data.science_total2 != 0 && data.science_total1 != 0)
                {
                    data.sum_percentage_science =
                        ((data.science_total2 - data.science_total1) / data.science_total1) * 100;
                }
                if (data.science_total2 == 0)
                {
                    data.sum_percentage_science = 0;
                }
                if (data.science_total1 == 0)
                {
                    data.sum_percentage_science = 0;
                }
                if (data.social_total2 != 0 && data.social_total1 != 0 )
                {
                    data.sum_percentage_social =((data.social_total2 - data.social_total1) / data.social_total1) * 100;

                }
                if (data.social_total2 == 0  )
                {
                    data.sum_percentage_social = 0;

                }
                if (data.social_total1 == 0  )
                {
                    data.sum_percentage_social = 0;

                }
                if (data.sum_percentage_science == null)
                {
                    data.sum_percentage_science = 0;
                }
                if (data.sum_percentage_social == null)
                {
                    data.sum_percentage_social = 0;
                }
                if (data.total4 != 0 && data.total13!= 0 )
                {
                    data.total19 = ((data.total13 - data.total4) / data.total4) * 100;

                }
                if (data.total8 != 0 && data.total17!= 0 )
                {
                    data.total20 = ((data.total17 - data.total8) / data.total8) * 100;

                }
                //data.total13 = (data.total4 * 100) / data.total12;
                //data.total14 = (data.total8 * 100) / data.total12;
            }

            var student_news_facultys = new List<student_graduate_program_year>() {_student_news_faculty};

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

            return Ok();
        }
    }
}