using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using Valg.EVA.Skanning.Entity.ReadModel;
using Valg.EVA.Skanning.Repositories.BallotPaper;
using Valg.EVA.Skanning.Sample.Startup.Ballot;
using Valg.EVA.Skanning.UserInterfaceElements;
using Valg.EVA.Skanning.UserInterfaceElements.BallotPaperImage;
using Valg.EVA.Skanning.UserInterfaceElements.Commands;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.BallotPaperXOfY;

namespace Valg.EVA.Skanning.Sample.Startup.Box
{
    public class NavigateBallotsViewModel : ViewModelBase
    {
        private readonly List<BallotPaperInBoxCounting> _ballotPapers = new List<BallotPaperInBoxCounting>();
        private int _currentIndex = -1;
        private readonly IBallotPaperRepository _ballotPaperRepository;
        private readonly BallotPaperXOfYViewModel _ballotPaperXofYViewModel = new BallotPaperXOfYViewModel();
        public IBallotPaperXOfYViewModel BallotPaperXOfYViewModel => _ballotPaperXofYViewModel;
        public BallotControlViewModel BallotControlViewModel { get; }

        public NavigateBallotsViewModel(IBallotPaperRepository ballotPaperRepository, BallotControlViewModel ballotControlViewModel)
        {
            _ballotPaperRepository = ballotPaperRepository;
            BallotControlViewModel = ballotControlViewModel;
            NextCommand = GuardedCommand.Create(GotoToNextBallotPaper);
            PreviousCommand = GuardedCommand.Create(GoToPreviousBallotPaper);
            FirstCommand = GuardedCommand.Create(Reset);
            LastCommand = GuardedCommand.Create(GoToLastBallotPaper);
            _ballotPaperXofYViewModel.PropertyChanged += _ballotPaperXofYViewModel_PropertyChanged;
        }

        private void _ballotPaperXofYViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(BallotPaperXOfYViewModel.Current))
            {
                SetCurrentIndex(((BallotPaperXOfYViewModel)sender).Current - 1);
            }
        }

        private BallotPaperInBoxCounting CurrentBallotPaper => _ballotPapers.Count >= _currentIndex + 1 ? _ballotPapers[_currentIndex] : null;
        public ICommand PreviousCommand { get; }
        public ICommand NextCommand { get; }
        public ICommand FirstCommand { get; }
        public ICommand LastCommand { get; }
        public bool IsPreviousButtonEnabled => _currentIndex > 0;
        public bool IsNextButtonEnabled => _currentIndex >= 0 && _currentIndex < _ballotPapers.Count - 1;
        public void Reset()
        {
            SetCurrentIndex(_ballotPapers.Any() ? 0 : -1);
        }

        void GotoToNextBallotPaper()
        {
            SetCurrentIndex(_currentIndex + 1);
            OnPropertyChanged(nameof(CurrentBallotPaper));
        }
        void GoToPreviousBallotPaper()
        {
            SetCurrentIndex(_currentIndex - 1);
        }
        public void LoadBox(int boxId)
        {
            _ballotPapers.Clear();
            BallotControlViewModel.BoxId = boxId;
            _ballotPapers.AddRange(_ballotPaperRepository.GetBallotPapersInBoxCounting(boxId));
            BallotPaperXOfYViewModel.Total = _ballotPapers.Count;
            SetCurrentIndex(_ballotPapers.Any() ? 0 : -1, true);
        }

        void GoToLastBallotPaper()
        {
            SetCurrentIndex(_ballotPapers.Count - 1);
        }

        private void SetCurrentIndex(int index)
        {
            SetCurrentIndex(index, false);
        }

        private void SetCurrentIndex(int index, bool force)
        {
            if (_currentIndex == index && !force)
            {
                return;
            }
            _currentIndex = index;
            BallotPaperXOfYViewModel.Current = _currentIndex + 1;
            BallotControlViewModel.BallotPaperId = CurrentBallotPaper?.BallotPaperId ?? 0;
            OnPropertyChanged(nameof(CurrentBallotPaper));
            OnPropertyChanged(nameof(IsNextButtonEnabled));
            OnPropertyChanged(nameof(IsPreviousButtonEnabled));
        }



    }
}

