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

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

        public bool IsReceipt = false;

        [ObservableProperty]
        private string _title = "Lieferungen - Elwig";

        public Member? FilterMember { get; set; }

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

        [ObservableProperty]
        private string? _lastScaleError;
        [ObservableProperty]
        private string? _manualWeighingReason;
        [ObservableProperty]
        private string? _scaleId;
        [ObservableProperty]
        private string? _weighingData;

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

        [ObservableProperty]
        private Delivery? _selectedDelivery;
        [ObservableProperty]
        private IEnumerable<Delivery> _deliveries = [];

        [ObservableProperty]
        private string? _mgNrString;
        public int? MgNr {
            get => int.TryParse(MgNrString, out var mgnr) ? mgnr : null;
            set => MgNrString = $"{value}";
        }
        [ObservableProperty]
        private Member? _member;
        [ObservableProperty]
        private IEnumerable<Member> _memberSource = [];
        [ObservableProperty]
        private string? _memberAddress;
        [ObservableProperty]
        private string? _lsNr;
        [ObservableProperty]
        private string? _date;
        [ObservableProperty]
        private string? _time;
        [ObservableProperty]
        private Branch? _branch;
        [ObservableProperty]
        private IEnumerable<Branch> _branchSource = [];
        [ObservableProperty]
        private string? _comment;

        [ObservableProperty]
        private string? _sortId;
        [ObservableProperty]
        private WineVar? _wineVar;
        [ObservableProperty]
        private IEnumerable<WineVar> _wineVarSource = [];
        [ObservableProperty]
        private object? _wineAttrObj;
        public WineAttr? WineAttr {
            get => WineAttrObj as WineAttr;
            set => WineAttrObj = value ?? WineAttrSource.FirstOrDefault();
        }
        [ObservableProperty]
        private IEnumerable<object> _wineAttrSource = [];
        [ObservableProperty]
        private object? _wineCultObj;
        public WineCult? WineCult {
            get => WineCultObj as WineCult;
            set => WineCultObj = value ?? WineCultSource.FirstOrDefault();
        }
        [ObservableProperty]
        private IEnumerable<object> _wineCultSource = [];
        [ObservableProperty]
        private string? _gradationOeString;
        public double? GradationOe {
            get => double.TryParse(GradationOeString, out var oe) ? oe : null;
            set => GradationOeString = $"{value:0}";
        }
        [ObservableProperty]
        private string? _gradationKmwString;
        public double? GradationKmw {
            get => double.TryParse(GradationKmwString, out var kmw) ? kmw : null;
            set => GradationKmwString = $"{value:0.0}";
        }
        [ObservableProperty]
        private WineQualLevel? _wineQualityLevel;
        [ObservableProperty]
        private IEnumerable<WineQualLevel> _wineQualityLevelSource = [];
        [ObservableProperty]
        private bool _abgewertet;

        [ObservableProperty]
        private string? _weightString;
        public int? Weight {
            get => int.TryParse(WeightString?.Replace(Utils.GroupSeparator, ""), out var w) ? w : null;
            set => WeightString = $"{value:N0}";
        }
        [ObservableProperty]
        private bool _isManualWeighing;
        [ObservableProperty]
        private bool? _isNetWeightValue = false;
        public bool IsNetWeight {
            get => IsNetWeightValue ?? false;
            set => IsNetWeightValue = value;
        }

        [ObservableProperty]
        private WineOrigin? _wineOrigin;
        [ObservableProperty]
        private IEnumerable<WineOrigin> _wineOriginSource = [];
        [ObservableProperty]
        private object? _wineKgObj;
        public AT_Kg? WineKg {
            get => WineKgObj as AT_Kg;
            set => WineKgObj = value ?? WineKgSource.FirstOrDefault();
        }
        [ObservableProperty]
        private IEnumerable<object> _wineKgSource = [];
        [ObservableProperty]
        private object? _wineRdObj;
        public WbRd? WineRd {
            get => WineRdObj as WbRd;
            set => WineRdObj = value ?? WineRdSource.FirstOrDefault();
        }
        [ObservableProperty]
        private IEnumerable<object> _wineRdSource = [];
        [ObservableProperty]
        private bool? _isGebunden;

        public ObservableCollection<Modifier> Modifiers { get; set; } = [];
        [ObservableProperty]
        private IEnumerable<Modifier> _modifiersSource = [];
        [ObservableProperty]
        private string? _partComment;
        [ObservableProperty]
        private string? _temperatureString;
        public double? Temperature {
            get => double.TryParse(TemperatureString, out var t) ? t : null;
            set => TemperatureString = $"{value:0.0}";
        }
        [ObservableProperty]
        private string? _acidString;
        public double? Acid {
            get => double.TryParse(AcidString, out var a) ? a : null;
            set => AcidString = $"{value:0.0}";
        }
        [ObservableProperty]
        private bool? _isHandPicked;

        public string? Unloading {
            get => IsUnloadingDumper ? DeliveryPart.Dumper : IsUnloadingPumped ? DeliveryPart.Pumped : IsUnloadingBox ? DeliveryPart.Box : null;
            set {
                switch (value) {
                    case DeliveryPart.Dumper: IsUnloadingDumper = true; break;
                    case DeliveryPart.Pumped: IsUnloadingPumped = true; break;
                    case DeliveryPart.Box: IsUnloadingBox = true; break;
                    default: IsUnloadingOther = true; break;
                }
            }
        }
        [ObservableProperty]
        private bool _isUnloadingDumper;
        [ObservableProperty]
        private bool _isUnloadingPumped;
        [ObservableProperty]
        private bool _isUnloadingBox;
        [ObservableProperty]
        private bool _isUnloadingOther;

        [ObservableProperty]
        private string _statusMembers = "-";
        [ObservableProperty]
        private string _statusDeliveries = "-";
        [ObservableProperty]
        private string _statusVarieties = "-";
        [ObservableProperty]
        private string _statusWeight = "-";
        [ObservableProperty]
        private string _statusGradation = "-";

        [ObservableProperty]
        private Grid? _statusWeightToolTip;
        [ObservableProperty]
        private Grid? _statusGradationToolTip;
    }
}
