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

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

        [ObservableProperty]
        private string _title = "Flächenbindungen - Elwig";

        public required Member FilterMember { get; set; }

        [ObservableProperty]
        private string? _searchQuery = "";
        public List<string> TextFilter {
            get => [.. SearchQuery?.ToLower().Split(' ').ToList().FindAll(e => e.Length > 0) ?? []];
            set => SearchQuery = string.Join(' ', value.Where(e => e.Length > 0));
        }

        [ObservableProperty]
        private bool _showOnlyActiveAreaComs;

        [ObservableProperty]
        private string? _fbNrString;
        public int? FbNr {
            get => int.TryParse(FbNrString, out var mgnr) ? mgnr : null;
            set => FbNrString = $"{value}";
        }
        [ObservableProperty]
        private string? _mgNrString;
        public int? MgNr {
            get => int.TryParse(MgNrString, out var mgnr) ? mgnr : null;
            set => MgNrString = $"{value}";
        }
        [ObservableProperty]
        private string? _yearFromString;
        public int? YearFrom {
            get => int.TryParse(YearFromString, out var year) ? year : null;
            set => YearFromString = $"{value}";
        }
        [ObservableProperty]
        private string? _yearToString;
        public int? YearTo {
            get => int.TryParse(YearToString, out var year) ? year : null;
            set => YearToString = $"{value}";
        }

        [ObservableProperty]
        private AreaComType? _areaComType;
        [ObservableProperty]
        private IEnumerable<AreaComType> _areaComTypeSource = [];
        [ObservableProperty]
        private object? _wineCultObj;
        public WineCult? WineCult {
            get => WineCultObj as WineCult;
            set => WineCultObj = value ?? WineCultSource.FirstOrDefault();
        }
        [ObservableProperty]
        private IEnumerable<object> _wineCultSource = [];
        [ObservableProperty]
        private string? _comment;

        [ObservableProperty]
        private AT_Kg? _kg;
        [ObservableProperty]
        private IEnumerable<AT_Kg> _kgSource = [];
        [ObservableProperty]
        private object? _rdObj;
        public WbRd? Rd {
            get => RdObj as WbRd;
            set => RdObj = value ?? RdSource.FirstOrDefault();
        }
        [ObservableProperty]
        private IEnumerable<object> _rdSource = [];
        [ObservableProperty]
        private string? _gstNr;
        [ObservableProperty]
        private string? _areaString;
        public int? Area {
            get => int.TryParse(AreaString, out var area) ? area : null;
            set => AreaString = $"{value}";
        }

        [ObservableProperty]
        private string _statusAreaCommitments = "-";
        [ObservableProperty]
        private string _statusContracts = "-";
        [ObservableProperty]
        private string _statusArea = "-";
        [ObservableProperty]
        private Grid? _statusAreaToolTip;
    }
}
