using Android.DeviceLock;
using Firebase.Auth;
using System.Globalization;
using System.Threading.Tasks;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.MAUI.Services.Interfaces;

namespace WorkFlowCheck.MAUI.Pages.CheckList;

public partial class CheckListInfoPage : ContentPage
{
    private readonly ICheckListService _checkListService;
    private readonly IUserService _userService;
    private UserDTO _currentUser;
    private CheckListHeaderDTO _checkListHeaderDTO;
    private CheckListHeaderInfoDTO _checkListHeaderInfoDTO;
    public CheckListInfoPage(CheckListHeaderDTO checkListHeaderDTO)
    {
        InitializeComponent();
        _userService = MauiProgram.ServiceProvider.GetRequiredService<IUserService>();
        _checkListService = MauiProgram.ServiceProvider.GetRequiredService<ICheckListService>();
        _checkListHeaderDTO = checkListHeaderDTO;
    }
    private async Task SetInfo()
    {
        _currentUser = _userService.GetCurrentUser();
        DeviceId.Text = _userService.DeviceId;
        UserInfo.Text = $"{_currentUser?.LastName} {_currentUser?.FirstName}";
        HeaderLabel.Text = _checkListHeaderDTO.DocumentNumber;
    }
    protected override async void OnAppearing()
    {
        base.OnAppearing();
        BindingContext = this;
        await SetInfo();
        BuildCheckListHeaderInfo();
    }
    private async Task BuildCheckListHeaderInfo()
    {
        _checkListHeaderInfoDTO = new CheckListHeaderInfoDTO();

        var result = _checkListHeaderDTO.CheckListRowDTO
                                        .GroupBy(r => 1)
                                        .Select(g => new
                                        {
                                            TotalRowCount = g.Count(),
                                            EmptyAnswerCount = g.Count(r => string.IsNullOrEmpty(r.Answer)),
                                            CheckPointCount = g.Select(r => r.CheckListTemplateRowDTO.CheckPointId).Distinct().Count()
                                        })
                                        .FirstOrDefault();
        if (result != null)
        {
            _checkListHeaderInfoDTO.TotalRowCount = result.TotalRowCount;
            _checkListHeaderInfoDTO.CheckPointCount = result.CheckPointCount;
            _checkListHeaderInfoDTO.EmptyAnswerCount = result.EmptyAnswerCount;
            if (_checkListHeaderInfoDTO.TotalRowCount > 0)
            {
                _checkListHeaderInfoDTO.ReadyPercent = ((double)(_checkListHeaderInfoDTO.TotalRowCount - _checkListHeaderInfoDTO.EmptyAnswerCount) / _checkListHeaderInfoDTO.TotalRowCount * 100)
                                                           .ToString("0.00", new CultureInfo("hu-HU")) + " %";
            }
        }

        var checkPointDTO = await _checkListService.GetNextCheckpointNotSend(_checkListHeaderDTO);

        row1.Text = $"Összes ellenőrizendő lépés: {_checkListHeaderInfoDTO.TotalRowCount} db";
        row2.Text = $"Összes még nem ellenőrzött lépés: {_checkListHeaderInfoDTO.EmptyAnswerCount} db";
        row3.Text = $"Ellenőrzési pontok száma: {_checkListHeaderInfoDTO.CheckPointCount} db";
        row4.Text = $"Készültség: {_checkListHeaderInfoDTO.ReadyPercent}";
        row5.Text = $"Következő ellenőrzési pont : {checkPointDTO.ShortName}";
    }
}