using System.Collections.ObjectModel;
using System.Windows.Input;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.MAUI.Pages.BaseStock.CheckPoints;
using WorkFlowCheck.MAUI.Services.Interfaces;
using Plugin.NFC;
using WorkFlowCheck.MAUI.Pages.NFC;

namespace WorkFlowCheck.MAUI.Pages.BaseStock;

public partial class CheckPointsPage : ContentPage
{
    private readonly IBaseStockService _baseStockService;
    private readonly ISyncService _syncService;
    

    public CheckPointsPage()
    {
        InitializeComponent();
        _baseStockService = MauiProgram.ServiceProvider.GetRequiredService<IBaseStockService>();
        _syncService = MauiProgram.ServiceProvider.GetRequiredService<ISyncService>();
    }

    public ICommand RefreshCommand { get; set; }
    public ICommand EditCommand { get; set; }
    public ICommand PairCommand { get; set; }
    public bool IsRefreshing { get; set; }

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        RefreshCommand = new Command(async () => await LoadCheckPoints());
        EditCommand = new Command<CheckPointDTO>(OnEditItem);
        PairCommand = new Command<CheckPointDTO>(OnPairItem);

        BindingContext = this;
        await LoadCheckPoints();

    }
    private async Task LoadCheckPoints()
    {
        IsRefreshing = true;
        //await _syncService.SyncDatas();
        CheckPointsList.ItemsSource = await _baseStockService.GetAllCheckPoints();
        IsRefreshing = false;
        OnPropertyChanged(nameof(IsRefreshing));
    }
    private async void OnEditItem(CheckPointDTO checkPointDTO)
    {
        // Navigálás a CheckPointEdit.xaml-ra, és átadjuk az editálni kívánt elemet
        await Navigation.PushAsync(new CheckPointEditPage(checkPointDTO));
    }
    private async void OnPairItem(CheckPointDTO checkPointDTO)
    {
        var checkPointCode = await NFCWaiter.ShowModalAsync();
        if (!string.IsNullOrEmpty(checkPointCode))
        {
            if (Navigation.ModalStack.Count > 0)
            {
                await Navigation.PopModalAsync();
            }
            if (checkPointDTO != null)
            {
                checkPointDTO.Code = checkPointCode;
                await _baseStockService.UpdateCheckPoint(checkPointDTO);
                await LoadCheckPoints();
            }
        }
    }
    protected override bool OnBackButtonPressed()
    {
        Shell.Current.GoToAsync("//BaseStockPage");
        return true;
    }
    private async void OnButtonSync(object sender, EventArgs e)
    {
        IsRefreshing = true;
        ButtonSync.IsEnabled = false;
        
        progressLabel.Text = "Szinkronizálás indítása...";
        progressBar.Progress = 0;

        try
        {
            await Task.Run(async () =>
            {
                await _syncService.SyncCheckPoints_Up((percentage, infostring) =>
                {
                    MainThread.BeginInvokeOnMainThread(() =>
                    {
                        progressBar.Progress = percentage / 100;
                        progressLabel.Text = $"{infostring}: {percentage:0.00}%";
                    });
                });
            });

            progressLabel.Text = "Szinkronizálás befejezve!";
        }
        catch (Exception ex)
        {
            progressLabel.Text = $"Hiba történt: {ex.Message}";
        }
        finally
        {
            ButtonSync.IsEnabled = true;
            IsRefreshing = false;
        }
    }
}