using System.Text.Json;
using System.Text.Json.Serialization;

namespace View_by_Distance.Shared.Models;

public class Property : Properties.IProperty
{

    protected DateTime _CreationTime;
    protected DateTime? _DateTime;
    protected DateTime? _DateTimeDigitized;
    protected DateTime? _DateTimeFromName;
    protected DateTime? _DateTimeOriginal;
    protected long _FileSize;
    protected DateTime? _GPSDateStamp;
    protected int? _Height;
    protected int? _Id;
    protected DateTime _LastWriteTime;
    protected string _Make;
    protected string _Model;
    protected string _Orientation;
    protected int? _Width;
    public DateTime CreationTime => _CreationTime;
    public DateTime? DateTime => _DateTime;
    public DateTime? DateTimeDigitized => _DateTimeDigitized;
    public DateTime? DateTimeFromName => _DateTimeFromName;
    public DateTime? DateTimeOriginal => _DateTimeOriginal;
    public long FileSize => _FileSize;
    public DateTime? GPSDateStamp => _GPSDateStamp;
    public int? Height => _Height;
    public int? Id => _Id;
    public DateTime LastWriteTime => _LastWriteTime;
    public string Make => _Make;
    public string Model => _Model;
    public string Orientation => _Orientation;
    public int? Width => _Width;

    [JsonConstructor]
    public Property(DateTime creationTime, DateTime? dateTime, DateTime? dateTimeDigitized, DateTime? dateTimeFromName, DateTime? dateTimeOriginal, long fileSize, DateTime? gpsDateStamp, int? height, int? id, DateTime lastWriteTime, string make, string model, string orientation, int? width)
    {
        _DateTimeFromName = dateTimeFromName;
        _CreationTime = creationTime;
        _DateTime = dateTime;
        _DateTimeDigitized = dateTimeDigitized;
        _DateTimeOriginal = dateTimeOriginal;
        _FileSize = fileSize;
        _GPSDateStamp = gpsDateStamp;
        _Height = height;
        _Id = id;
        _LastWriteTime = lastWriteTime;
        _Make = make;
        _Model = model;
        _Orientation = orientation;
        _Width = width;
    }

    public override string ToString()
    {
        string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
        return result;
    } // ...

    public List<DateTime> GetDateTimes() => Stateless.Methods.Property.GetDateTimes(_CreationTime, _LastWriteTime, _DateTime, _DateTimeDigitized, _DateTimeFromName, _DateTimeOriginal, _GPSDateStamp);

    public (bool?, string[]) IsWrongYear(FileHolder fileHolder, DateTime? minimumDateTime)
    {
        string[] results = Array.Empty<string>();
        bool? result = null;
        string year;
        string directoryName;
        string[] directorySegments;
        string? check = Path.GetFullPath(fileHolder.FullName);
        string? pathRoot = Path.GetPathRoot(fileHolder.FullName);
        if (string.IsNullOrEmpty(pathRoot))
            throw new Exception();
        if (minimumDateTime.HasValue)
            year = minimumDateTime.Value.ToString("yyyy");
        else
        {
            List<DateTime> dateTimes = GetDateTimes();
            year = dateTimes.Min().ToString("yyyy");
        }
        for (int i = 0; i < int.MaxValue; i++)
        {
            check = Path.GetDirectoryName(check);
            if (string.IsNullOrEmpty(check) || check == pathRoot)
                break;
            directoryName = Path.GetFileName(check);
            directorySegments = directoryName.Split(' ');
            (result, results) = Stateless.Methods.Property.IsWrongYear(directorySegments, year);
            if (result.HasValue)
                break;
        }
        return new(result, results);
    }

}