using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;

namespace XLAB
{
    internal sealed class TpvdklEditWindowViewModel : ObservableObject
    {
        private readonly IReadOnlyList<TpvdklDirectoryItem> _existingItems;
        private int _stampKindId;
        private string _validationMessage;
        private int _verificationTypeId;

        public TpvdklEditWindowViewModel(TpvdklDirectoryItem seed, bool isNew, IReadOnlyList<TpvdklDirectoryItem> existingItems, TypeSizeDirectoryService service)
        {
            var source = seed ?? new TpvdklDirectoryItem();
            _existingItems = existingItems ?? Array.Empty<TpvdklDirectoryItem>();
            Id = source.Id;
            TipsId = source.TipsId;
            IsNew = isNew;
            VerificationTypes = service.LoadSpvdmkReferences();
            StampKinds = service.LoadSpvdklReferences();
            VerificationTypeId = source.VerificationTypeId;
            StampKindId = source.StampKindId;
            ConfirmCommand = new RelayCommand(Confirm);
            CancelCommand = new RelayCommand(Cancel);
        }

        public event EventHandler<bool?> CloseRequested;
        public ICommand CancelCommand { get; private set; }
        public ICommand ConfirmCommand { get; private set; }
        public int Id { get; private set; }
        public bool IsNew { get; private set; }
        public IReadOnlyList<DirectoryLookupItem> VerificationTypes { get; private set; }
        public IReadOnlyList<DirectoryLookupItem> StampKinds { get; private set; }
        public int TipsId { get; private set; }
        public int VerificationTypeId { get { return _verificationTypeId; } set { SetProperty(ref _verificationTypeId, value); } }
        public int StampKindId { get { return _stampKindId; } set { SetProperty(ref _stampKindId, value); } }
        public string Title { get { return IsNew ? "Новый вид клейма" : "Редактирование вида клейма"; } }
        public string ValidationMessage { get { return _validationMessage; } private set { SetProperty(ref _validationMessage, value); } }

        public TpvdklDirectoryItem ToResult()
        {
            return new TpvdklDirectoryItem { Id = Id, TipsId = TipsId, VerificationTypeId = VerificationTypeId, StampKindId = StampKindId };
        }

        private void Cancel(object parameter) { RaiseCloseRequested(false); }

        private void Confirm(object parameter)
        {
            if (VerificationTypeId <= 0) { ValidationMessage = "Укажите вид МК."; return; }
            if (StampKindId <= 0) { ValidationMessage = "Укажите вид клейма."; return; }
            var duplicate = _existingItems.FirstOrDefault(delegate(TpvdklDirectoryItem item) { return item != null && item.Id != Id && item.VerificationTypeId == VerificationTypeId && item.StampKindId == StampKindId; });
            if (duplicate != null) { ValidationMessage = "Такая комбинация вида МК и вида клейма уже существует."; return; }
            ValidationMessage = string.Empty;
            RaiseCloseRequested(true);
        }

        private void RaiseCloseRequested(bool? dialogResult)
        {
            var handler = CloseRequested;
            if (handler != null) handler(this, dialogResult);
        }
    }
}
