using LSFE.MobApp.Contracts;
using LSFE.MobApp.Entities.Doctor;
using LSFE.MobApp.Helpers;
using LSFE.MobApp.Services;
using Microsoft.Extensions.Configuration;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace LSFE.MobApp.ViewModels.Common;
public abstract class BaseDoctorVM : INotifyPropertyChanged, IDisposable
{
    protected readonly Repository<Doctor> _doctorRepository;
    protected readonly ISyncService _syncService;
    protected readonly AddressService _addressService;
    protected readonly IConfiguration _configuration;

    private bool _disposed;
    protected Doctor _doctorEntity;
    protected BaseDoctorVM(
       Repository<Doctor> doctorRepository,
       ISyncService syncService,
       AddressService addressService,
       IConfiguration configuration)
    {
        _doctorRepository = doctorRepository;
        _syncService = syncService;
        _addressService = addressService;
        _configuration = configuration;

        // Automatically register for status updates
        DoctorStatusHelper.RegisterForStatusUpdates(this, OnDoctorStatusUpdated);
    }
    /// <summary>
    /// Override this method to handle status updates in derived classes
    /// </summary>
    protected abstract void OnDoctorStatusUpdated(int doctorId);

    /// <summary>
    /// Helper method to refresh a specific doctor from repository
    /// </summary>
    protected async Task<Doctor> RefreshDoctorFromRepositoryAsync(int doctorId)
    {
        return await _doctorRepository.FirstOrDefaultAsync(x => x.DoctorId == doctorId);
    }

    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion

    #region IDisposable
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            if (disposing)
            {
                // Unregister from status updates
                DoctorStatusHelper.UnregisterFromStatusUpdates(this);
            }
            _disposed = true;
        }
    }
    #endregion
}