using System.Collections.ObjectModel;
using System.Windows.Input;
using Valg.EVA.Skanning.Services.Verification;
using Valg.EVA.Skanning.UserInterfaceElements.BallotPaperImage;
using Valg.EVA.Skanning.UserInterfaceElements.Commands;
using Valg.EVA.Skanning.UserInterfaceElements;
using Valg.EVA.Skanning.Verify.Startup.PlacementInBox;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.Messaging.Events;

namespace Valg.EVA.Skanning.Verify.Startup.Preview
{
    public class PreviewViewModel : ViewModelBase, IPreviewViewModel
    {
        private readonly IBallotPaperImageWindowProvider ballotPaperImageWindowProvider;
        private readonly IPlacementInBoxViewModel placementInBoxViewModel;
        private readonly IVerificationService verificationService;
        private int currentBallotPaperImagePageIndex;

        private readonly RelayCommand showBallotPaperImageCommand;
        private readonly RelayCommand showBallotPaperPlacementInBoxCommand;

        public PreviewViewModel(IBallotPaperImageViewModel ballotPaperImageViewModel, IBallotPaperImageWindowProvider ballotPaperImageWindowProvider, IPlacementInBoxViewModel placementInBoxViewModel, IVerificationService verificationService)
        {
            BallotPaperImageViewModel = ballotPaperImageViewModel;
            this.ballotPaperImageWindowProvider = ballotPaperImageWindowProvider;
            this.placementInBoxViewModel = placementInBoxViewModel;
            this.verificationService = verificationService;
            showBallotPaperPlacementInBoxCommand = new RelayCommand(c => ShowBallotPaperPlacementInBox());
            showBallotPaperImageCommand = new RelayCommand(c => ShowBallotPaperImage());
        }

        public void SetModel(BallotPaperImageInformation bitmapInfo, int ballotPaperId, int boxCountingId)
        {
            BallotPaperImageViewModel.BallotPaperId = ballotPaperId;
            BallotPaperImageViewModel.BoxCountingId = boxCountingId;
            BallotPaperImageViewModel.Front = bitmapInfo.Front;
            BallotPaperImageViewModel.Back = bitmapInfo.Back;
            BallotPaperImageViewModel.Selected = bitmapInfo.Front;
            BallotPaperImageViewModel.ShowZoomPanelMiniMap = true;
        }

        public void AddPreviewImageViewModel(PreviewBallotPaperImageViewModel previewViewModel)
        {
            ImageViewModels.Add(previewViewModel);
        }

        protected override void SubscribeToViewModelSpecificEvents()
        {
            SubscribeToEvent<PageChangedEvent, PageChangedEventArgs>(OnPageChanged);
        }

        private void ShowBallotPaperPlacementInBox()
        {
            var placementInBoxInfo = verificationService.GetBallotPaperPositionInBox(BallotPaperImageViewModel.BallotPaperId, BallotPaperImageViewModel.BoxCountingId);
            placementInBoxViewModel.ShowPlacementInBoxWindow(placementInBoxInfo.PlacementIndex, placementInBoxInfo.TotalNumberOfBallotPapers);
        }

        private void ShowBallotPaperImage()
        {
            ballotPaperImageWindowProvider.Show(BallotPaperImageViewModel);
        }

        private void OnPageChanged(PageChangedEventArgs eventArgs)
        {
            CurrentBallotPaperImagePageIndex = eventArgs.NewPage - 1;
        }

        public int CurrentBallotPaperImagePageIndex
        {
            get
            {
                return currentBallotPaperImagePageIndex;
            }
            set
            {
                currentBallotPaperImagePageIndex = value;
                OnPropertyChanged();
            }
        }

        public ObservableCollection<PreviewBallotPaperImageViewModel> ImageViewModels { get; } = new ObservableCollection<PreviewBallotPaperImageViewModel>();

        public ICommand ShowBallotPaperImageCommand => showBallotPaperImageCommand;
        public ICommand ShowBallotPaperPlacementInBoxCommand => showBallotPaperPlacementInBoxCommand;

        public IBallotPaperImageViewModel BallotPaperImageViewModel { get; set; }
    }
}

