using Brizco.Domain.Entities.Shift;
using Brizco.Domain.Entities.Task;

namespace Brizco.Core.CoreServices.ReportServices;
public class ShiftPlanReportCommandHandler : IRequestHandler<ShiftPlanReportCommand, byte[]>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public ShiftPlanReportCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<byte[]> Handle(ShiftPlanReportCommand request, CancellationToken cancellationToken)
    {
        var shiftPlan = await _repositoryWrapper.SetRepository<ShiftPlan>()
            .TableNoTracking
            .Where(sp => sp.Id == request.ShiftPlanId)
            .Select(ShiftPlanMapper.ProjectToSDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (shiftPlan == null)
            throw new AppException("ShiftPlan not found", ApiResultStatusCode.NotFound);
        var activities = await _repositoryWrapper.SetRepository<Activity>()
            .TableNoTracking
            .Where(sp => sp.ShiftId == shiftPlan.ShiftId && sp.SetFor.Date == shiftPlan.PlanFor.Date)
            .Select(ActivityMapper.ProjectToSDto)
            .ToListAsync(cancellationToken);


        var workbook = new XSSFWorkbook();
        var sheet = workbook.CreateSheet($"{shiftPlan.ShiftTitle} - {shiftPlan.PlanFor.ToPersianDateTime().ToString("yyyyMMMMdd")}");
        sheet.IsRightToLeft = true;
        int selectedRow = 1;
        CreateHeader(sheet, workbook);
        foreach (var activity in activities)
        {
            var row = sheet.GetRow(selectedRow) ?? sheet.CreateRow(selectedRow);
            row.Height = 700;

            var style = workbook.CreateCellStyle();

            style.VerticalAlignment = VerticalAlignment.Center;
            style.Alignment = HorizontalAlignment.Center;
            style.WrapText = true;

            var cell = row.CreateCell(0);
            cell.CellStyle = style;
            cell.SetCellValue(activity.Title);

            var personCell = row.CreateCell(1);
            personCell.CellStyle = style;
            personCell.SetCellValue(activity.UserFullName);

            var statusCell = row.CreateCell(2);
            statusCell.CellStyle = style;
            statusCell.SetCellValue(activity.Status.ToDisplay());

            var doneAtCell = row.CreateCell(3);
            doneAtCell.CellStyle = style;
            doneAtCell.SetCellValue(activity.Title);

            var unDoneReasonCell = row.CreateCell(4);
            unDoneReasonCell.CellStyle = style;
            unDoneReasonCell.SetCellValue(activity.UnDoneReason);

            selectedRow++;
        }

        sheet.SetColumnWidth(0, 25600);
        sheet.SetColumnWidth(1, 7000);
        sheet.SetColumnWidth(2, 7000);
        sheet.SetColumnWidth(3, 7000);
        sheet.SetColumnWidth(4, 7000);


        var stream = new MemoryStream();
        workbook.Write(stream);
        var content = stream.ToArray();

        return content;
    }

    void CreateHeader(ISheet sheet, XSSFWorkbook workbook)
    {
        var headerRow = sheet.GetRow(0) ?? sheet.CreateRow(0);
        headerRow.Height = 700;

        var font = workbook.CreateFont();
        font.IsBold = true;
        font.Color = NPOI.HSSF.Util.HSSFColor.White.Index;

        //font.FontHeight = 16;
        var style = workbook.CreateCellStyle();
        style.Alignment = HorizontalAlignment.Center;
        style.VerticalAlignment = VerticalAlignment.Center;
        style.WrapText = true;
        style.SetFont(font);

        byte[] rgb = new byte[3] { 30, 81, 123 };
        XSSFColor color = new XSSFColor(rgb);
        //style.SetFillBackgroundColor(color);
        //style.SetFillForegroundColor(color);

        style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BlueGrey.Index;
        style.FillPattern = FillPattern.SolidForeground;

        var shiftNameHeaderCell = headerRow.CreateCell(0);
        shiftNameHeaderCell.CellStyle = style;
        shiftNameHeaderCell.SetCellValue("تسکــــ");

        var taskNameHeaderCell = headerRow.CreateCell(1);
        taskNameHeaderCell.CellStyle = style;
        taskNameHeaderCell.SetCellValue("شخص");

        var positionNameHeaderCell = headerRow.CreateCell(2);
        positionNameHeaderCell.CellStyle = style;
        positionNameHeaderCell.SetCellValue("وضعیت");

        var doneAtHeaderCell = headerRow.CreateCell(3);
        doneAtHeaderCell.CellStyle = style;
        doneAtHeaderCell.SetCellValue("انجام شده در");

        var unDoneReasonAtHeaderCell = headerRow.CreateCell(4);
        unDoneReasonAtHeaderCell.CellStyle = style;
        unDoneReasonAtHeaderCell.SetCellValue("دلیل انجام نشدن");
    }
}
