﻿using System.IO;
using System.Linq;
using BaseStation.Domain.Abstract;
using BaseStation.Domain.Entities;
using BaseStation.Domain.Models;
using OfficeOpenXml;
using OfficeOpenXml.Style;

namespace BaseStation.Domain.Concrete.Exporter
{
    public class WorkExcelExporter
    {
        private readonly ITicketRepository _repository;

        public WorkExcelExporter(ITicketRepository repository)
        {
            _repository = repository;
        }

        public MemoryStream Export(ExcelFilterModel filter)
        {
            filter.Type = TicketType.WorkOnGenerator;
            var tickets = _repository.FindByParam(filter);
            var stream = new MemoryStream();
            using (var package = new ExcelPackage(stream))
            {
                var worksheet = package.Workbook.Worksheets.Add("Работа бензогенераторов");
                //Add the headers
                var header = worksheet.Cells["A1:C1"];
                header.Style.Font.Bold = true;

                worksheet.Cells["A1"].Value = "Работа бензогенераторов";
                worksheet.Cells["B1"].Value = $"с {filter.From.Date:dd-MM-yyyy}";
                worksheet.Cells["C1"].Value = $"по {filter.To.Date:dd-MM-yyyy}";

                worksheet.Cells["A3"].Value = "Дата заведения заявки";
                worksheet.Cells["B3"].Value = "ФИО рег. инженера";
                worksheet.Cells["C3"].Value = "Наименование сайта кир.";
                worksheet.Cells["D3"].Value = "Наименование сайта лат.";
                worksheet.Cells["E3"].Value = "Регион БС";
                worksheet.Cells["F3"].Value = "Время начала работы на генераторе";
                worksheet.Cells["G3"].Value = "Время окончания работы на генераторе";
                worksheet.Cells["H3"].Value = "Количество часов работы генератора";
                worksheet.Cells["I3"].Value = "Лимит расход ГСМ на БГУ, литр/час";
                worksheet.Cells["J3"].Value = "Общий расход ГСМ на БГУ, литр";
                worksheet.Cells["K3"].Value = "Пробег км";
                worksheet.Cells["L3"].Value = "Выполняемые работы";

                var index = 3;
                foreach (var ticket in tickets)
                {
                    index++;

                    worksheet.Cells["A" + index].Value = ticket.Created.ToString("hh:mm, dd:MM:yyyy");
                    worksheet.Cells["B" + index].Value = ticket.Executor.GetFullName();
                    worksheet.Cells["C" + index].Value = ticket.Station?.Name;
                    worksheet.Cells["D" + index].Value = ticket.Station?.NameLat;
                    worksheet.Cells["E" + index].Value = ticket.Station?.Region?.Name;
                    worksheet.Cells["F" + index].Value = ticket.WorkStart?.ToString("HH:mm:ss");
                    worksheet.Cells["G" + index].Value = ticket.WorkEnd?.ToString("HH:mm:ss");
                    worksheet.Cells["H" + index].Value = ticket.Hours;

                    var works = ticket.Works.Select(x => x.Work.Name);
                    var names = string.Join(", ", works);
                    worksheet.Cells["L" + index].Value = names;
                }

                var border = worksheet.Cells["A3:L" + index].Style.Border;
                border.Top.Style = ExcelBorderStyle.Thin;
                border.Right.Style = ExcelBorderStyle.Thin;
                border.Bottom.Style = ExcelBorderStyle.Thin;
                border.Left.Style = ExcelBorderStyle.Thin;

//                worksheet.Cells.AutoFitColumns(0);
                package.Save();
            }
            return stream;
        }
    }
}