using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HelloXaml.Models;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace HelloXaml.ViewModels
{
    public partial class PersonListPageViewModel: ObservableObject
    {

        [RelayCommand(CanExecute = nameof(CanDecrease))]
        private void DecreaseAge()
        {
            NewPerson.Age--;
            DecreaseAgeCommand.NotifyCanExecuteChanged();
        }

        private bool CanDecrease() => NewPerson.Age > 0;

        [RelayCommand(CanExecute = nameof(CanIncrease))]
        private void IncreaseAge()
        {
            NewPerson.Age++;
            IncreaseAgeCommand.NotifyCanExecuteChanged();
        }
        private bool CanIncrease() => NewPerson.Age < 150;

        public PersonListPageViewModel()
        {


            NewPerson = new Person()
            {
                Name = "Eric Cartman",
                Age = 8
            };

            People = new ObservableCollection<Person>()
            {
                new Person() { Name = "Peter Griffin", Age = 40 },
                new Person() { Name = "Homer Simpson", Age = 42 },
            };
        }

        public ObservableCollection<Person> People { get; set; }

        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(IsDecrementEnabled))]
        [NotifyPropertyChangedFor(nameof(IsIncrementEnabled))]
        [NotifyPropertyChangedFor(nameof(IsNonEmptyName))]
        private Person newPerson;

        public bool IsDecrementEnabled
        {
            get { return NewPerson.Age > 0; }
        }

        public bool IsIncrementEnabled
        {
            get { return NewPerson.Age < 150; }
        }

        public bool IsNonEmptyName
        {
            get { return !string.IsNullOrWhiteSpace(NewPerson.Name); }
        }

        public void AddPersonToList(object sender, RoutedEventArgs e)
        {
            People.Add(new Person()
            {
                Name = NewPerson.Name,
                Age = NewPerson.Age,
            });

            NewPerson.Name = string.Empty;
            NewPerson.Age = 0;

            OnPropertyChanged(nameof(IsNonEmptyName));
            OnPropertyChanged(nameof(IsDecrementEnabled));
        }

        public void RefreshNameValidation()
        {
            OnPropertyChanged(nameof(IsNonEmptyName));
        }
    }
}
