using ShipyardBusinessLogic.OfficePackage.HelperModels;
using ShipyardBusinessLogic.OfficePackage;
using ShipyardContracts.BindingModels;
using ShipyardContracts.BusinessLogicsContracts;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShipyardBusinessLogic.BusinessLogics
{
    public class ReportLogic: IReportLogic
    {
        private readonly IDetailStorage _detailStorage;

        private readonly IShipStorage _shipStorage;

        private readonly IOrderStorage _orderStorage;

        private readonly AbstractSaveToExcel _saveToExcel;

        private readonly AbstractSaveToWord _saveToWord;

        private readonly AbstractSaveToPdf _saveToPdf;

        public ReportLogic(IShipStorage shipStorage, IDetailStorage detailStorage, IOrderStorage orderStorage,
            AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf)
        {
            _shipStorage = shipStorage;
            _detailStorage = detailStorage;
            _orderStorage = orderStorage;

            _saveToExcel = saveToExcel;
            _saveToWord = saveToWord;
            _saveToPdf = saveToPdf;
        }
        public List<ReportShipDetailViewModel> GetShipDetail()
        {

            var ships = _shipStorage.GetFullList();

            var list = new List<ReportShipDetailViewModel>();

            foreach (var ship in ships)
            {
                var record = new ReportShipDetailViewModel
                {
                    ShipName = ship.ShipName,
                    Details = new List<(string, int)>(),
                    TotalCount = 0
                };
                foreach (var shipDetail in ship.ShipDetails)
                {

                    record.Details.Add(new(shipDetail.Value.Item1.DetailName, shipDetail.Value.Item2));
                    record.TotalCount += shipDetail.Value.Item2;
                }

                list.Add(record);
            }

            return list;
        }
        public List<ReportOrdersViewModel> GetOrders(ReportBindingModel model)
        {
            return _orderStorage.GetFilteredList(new OrderSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo })
                    .Select(x => new ReportOrdersViewModel
                    {
                        Id = x.Id,
                        DateCreate = x.DateCreate,
                        ShipName = x.ShipName,
                        OrderStatus=x.Status.ToString(),
                        Sum = x.Sum
                    })
                    .ToList();
        }

        public void SaveComponentsToWordFile(ReportBindingModel model)
        {
            _saveToWord.CreateDoc(new WordInfo
            {
                FileName = model.FileName,
                Title = "Список кораблей",
                Ships = _shipStorage.GetFullList()
            });
        }

        public void SaveProductComponentToExcelFile(ReportBindingModel model)
        {
            _saveToExcel.CreateReport(new ExcelInfo
            {
                FileName = model.FileName,
                Title = "Список деталей",
                ShipDetails = GetShipDetail()
            });
        }

        public void SaveOrdersToPdfFile(ReportBindingModel model)
        {
            _saveToPdf.CreateDoc(new PdfInfo
            {
                FileName = model.FileName,
                Title = "Список заказов",
                DateFrom = model.DateFrom!.Value,
                DateTo = model.DateTo!.Value,
                Orders = GetOrders(model)
            });
        }
    }
}
