using PCUT.Models;
using System.Threading.Tasks;

namespace PCUT.ViewModels
{
    public abstract class UpsertViewModel<TKey> : NotificationBase
    {
        protected const string AddMode = "ADD";
        protected const string EditMode = "EDIT";

        public string Title => $"{Mode} {Entity.ToUpper()}";

        private string _mode;
        public string Mode
        {
            get { return _mode; }
            private set
            {
                SetProperty(ref _mode, value);
                RaisePropertyChanged(nameof(Title));
            }
        }

        private string _entity;
        public string Entity
        {
            get { return _entity; }
            set
            {
                SetProperty(ref _entity, value);
                RaisePropertyChanged(nameof(Title));
            }
        }

        private TKey _id;
        public TKey Id
        {
            get { return _id; }
            set
            {
                SetProperty(ref _id, value);
                Mode = value == null ? AddMode : EditMode;
            }
        }

        protected UpsertViewModel(string entity)
        {
            Entity = entity;
        }

        public void Initialize(TKey id)
        {
            Id = id;
        }

        public async Task LoadAsync()
        {
            if (Mode == EditMode)
            {
                await LoadAsync(Id);
            }
        }

        protected abstract Task LoadAsync(TKey id);
    }
}
