using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace PhotoboothUploader.Models;

public enum UploadStatus
{
    Queued,
    Uploading,
    Success,
    Failed,
}

public sealed class UploadItem : INotifyPropertyChanged
{
    private UploadStatus _status;
    private DateTimeOffset _updatedAt;

    public UploadItem(string path)
    {
        Path = path;
        FileName = System.IO.Path.GetFileName(path);
        UpdatedAt = DateTimeOffset.Now;
        Status = UploadStatus.Queued;
    }

    public string Path { get; }

    public string FileName { get; }

    public UploadStatus Status
    {
        get => _status;
        set
        {
            if (_status != value)
            {
                _status = value;
                UpdatedAt = DateTimeOffset.Now;
                OnPropertyChanged();
                OnPropertyChanged(nameof(StatusLabel));
            }
        }
    }

    public DateTimeOffset UpdatedAt
    {
        get => _updatedAt;
        private set
        {
            _updatedAt = value;
            OnPropertyChanged();
            OnPropertyChanged(nameof(UpdatedLabel));
        }
    }

    public string StatusLabel => Status switch
    {
        UploadStatus.Uploading => "Upload läuft",
        UploadStatus.Success => "Fertig",
        UploadStatus.Failed => "Fehlgeschlagen",
        _ => "Wartet",
    };

    public string UpdatedLabel => $"{UpdatedAt:HH:mm}";

    public event PropertyChangedEventHandler? PropertyChanged;

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