using CommunityToolkit.Mvvm.ComponentModel;
using Elwig.Models.Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace Elwig.ViewModels {
    public partial class DeliveryScheduleAdminViewModel : ObservableObject {

        [ObservableProperty]
        private string? _searchQuery = "";
        public List<string> TextFilter => [.. SearchQuery?.ToLower().Split(' ').ToList().FindAll(e => e.Length > 0) ?? []];

        [ObservableProperty]
        private bool _filterOnlyUpcoming;
        [ObservableProperty]
        private string? _filterSeasonString;
        public int? FilterSeason {
            get => int.TryParse(FilterSeasonString, out var year) ? year : null;
            set => FilterSeasonString = $"{value}";
        }

        [ObservableProperty]
        private DeliverySchedule? _selectedDeliverySchedule;
        [ObservableProperty]
        private IEnumerable<DeliverySchedule> _deliverySchedules = [];
        [ObservableProperty]
        private bool _enableSearchInputs = true;

        [ObservableProperty]
        private string _dateString = "";
        public DateOnly? Date {
            get => DateOnly.TryParseExact(DateString, "dd.MM.yyyy", out var d) ? d : null;
            set => DateString = $"{value:dd.MM.yyyy}";
        }
        [ObservableProperty]
        private Branch? _branch;
        [ObservableProperty]
        private IEnumerable<Branch> _branchSource = [];
        [ObservableProperty]
        private string _description = "";
        [ObservableProperty]
        private string? _maxWeightString = "";
        public int? MaxWeight {
            get => int.TryParse(MaxWeightString, out var w) ? w : null;
            set => MaxWeightString = $"{value}";
        }
        [ObservableProperty]
        private bool _isCancelled;
        public ObservableCollection<WineVar> MainVarieties { get; set; } = [];
        [ObservableProperty]
        private IEnumerable<WineVar> _mainVarietiesSource = [];
        public ObservableCollection<WineVar> OtherVarieties { get; set; } = [];
        [ObservableProperty]
        private IEnumerable<WineVar> _otherVarietiesSource = [];
        [ObservableProperty]
        private IEnumerable<object> _attributeSource = [];
        [ObservableProperty]
        private object? _attributeObj;
        public WineAttr? Attribute {
            get => AttributeObj as WineAttr;
            set => AttributeObj = value ?? AttributeSource.FirstOrDefault();
        }
        [ObservableProperty]
        private IEnumerable<object> _cultivationSource = [];
        [ObservableProperty]
        private object? _cultivationObj;
        public WineCult? Cultivation {
            get => CultivationObj as WineCult;
            set => CultivationObj = value ?? CultivationSource.FirstOrDefault();
        }

        [ObservableProperty]
        private string _ancmtFromDateString = "";
        public DateOnly? AncmtFromDate {
            get => DateOnly.TryParseExact(AncmtFromDateString, "dd.MM.yyyy", out var d) ? d : null;
            set => AncmtFromDateString = $"{value:dd.MM.yyyy}";
        }
        [ObservableProperty]
        private string _ancmtFromTimeString = "";
        public TimeOnly? AncmtFromTime {
            get => TimeOnly.TryParseExact(AncmtFromTimeString, "HH:mm", out var t) ? t : null;
            set => AncmtFromTimeString = $"{value:HH:mm}";
        }
        public DateTime? AncmtFrom {
            get => AncmtFromDate != null && AncmtFromTime != null ? AncmtFromDate.Value.ToDateTime(AncmtFromTime.Value) : null;
            set {
                AncmtFromDateString = $"{value:dd.MM.yyyy}";
                AncmtFromTimeString = $"{value:HH:mm}";
            }
        }
        [ObservableProperty]
        private string _ancmtToDateString = "";
        public DateOnly? AncmtToDate {
            get => DateOnly.TryParseExact(AncmtToDateString, "dd.MM.yyyy", out var d) ? d : null;
            set => AncmtToDateString = $"{value:dd.MM.yyyy}";
        }
        [ObservableProperty]
        private string _ancmtToTimeString = "";
        public TimeOnly? AncmtToTime {
            get => TimeOnly.TryParseExact(AncmtToTimeString, "HH:mm", out var t) ? t : null;
            set => AncmtToTimeString = $"{value:HH:mm}";
        }
        public DateTime? AncmtTo {
            get => AncmtToDate != null && AncmtToTime != null ? AncmtToDate.Value.ToDateTime(AncmtToTime.Value) : null;
            set {
                AncmtToDateString = $"{value:dd.MM.yyyy}";
                AncmtToTimeString = $"{value:HH:mm}";
            }
        }

        [ObservableProperty]
        private Visibility _controlButtonsVisibility = Visibility.Visible;
        [ObservableProperty]
        private Visibility _editingButtonsVisibility = Visibility.Hidden;
    }
}
