using Valg.EVA.Skanning.ContainerExtensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Prism.Commands;
using Valg.EVA.Skanning.JobManagement.Startup.UI.Duplicates.Events;

namespace Valg.EVA.Skanning.JobManagement.Startup.UI.Duplicates
{
    public class ViewDuplicateBallotsViewModel : ReactiveViewModel
    {
        private readonly IJobManagementApplication jobManagementApplication;
        private ReadOnlyCollection<ResolveDuplicatesBox> boxCountings;
        private readonly ITypedFactory typedFactory;
        private int countingId;

        public ViewDuplicateBallotsViewModel(IJobManagementApplication jobManagementApplication, ITypedFactory typedFactory)
        {
            this.jobManagementApplication = jobManagementApplication;
            this.typedFactory = typedFactory;
            CloseCommand = new DelegateCommand(CloseWindow);
        }

        public void Initialize(int countId, ReadOnlyCollection<ResolveDuplicatesBox> bCountings)
        {
            countingId = countId;
            boxCountings = bCountings;

            ParseAndBuildViewModels();
        }

        public string Title => Valg.EVA.Skanning.Resources.Language.JobManagementLanguageString.OverviewOfDuplicates;

        public Action CloseWindowAction { get; set; }

        public ICommand CloseCommand { get; set; }

        public DuplicateBallotsViewModel DuplicateBallotsViewModel { get; private set; }

        public ImageNavigatorViewModel ImageNavigatorViewModel { get; private set; }

        public double WindowWidth => System.Windows.Application.Current.MainWindow.ActualWidth * 0.8D;

        public double WindowHeight => System.Windows.Application.Current.MainWindow.ActualHeight * 0.8D;

        private void CloseWindow()
        {
            CloseWindowAction();
        }

        private List<DuplicateBallotModel> ConvertToDuplicateBallotModel()
        {
            var duplicateBallots = jobManagementApplication.GetDuplicateBallots(countingId);

            var newListOfDuplicates = new List<DuplicateBallotModel>();
            foreach (var duplicateBallot in duplicateBallots)
            {
                var newDuplicateBallotModel = new DuplicateBallotModel(duplicateBallot.BallotId, duplicateBallot.BallotPaperId, duplicateBallot.BoxCountingId);
                var boxCountingDetailsModel = boxCountings.Single(p => p.Id == duplicateBallot.BoxCountingId);
                newDuplicateBallotModel.BoxCountingName = boxCountingDetailsModel.BoxName;
                newListOfDuplicates.Add(newDuplicateBallotModel);
            }

            return newListOfDuplicates;
        }

        private void ClearImageView()
        {
            PublishEvent<ViewImagesEvent, ViewImagesEventArgs>(new ViewImagesEventArgs());
        }

        private void ParseAndBuildViewModels()
        {
            CreateViewModels();

            ClearImageView();
            
            OnPropertyChanged(null);
        }

        private void CreateViewModels()
        {
            DuplicateBallotsViewModel = CreateDuplicateBallotsViewModel();
            ImageNavigatorViewModel = typedFactory.Create<ImageNavigatorViewModel>();
        }

        private DuplicateBallotsViewModel CreateDuplicateBallotsViewModel()
        {
            var allDuplicatesInCounting = ConvertToDuplicateBallotModel();

            var boxesWithDuplicates = 
                allDuplicatesInCounting.Select(duplicate => duplicate.BoxCountingId)
                .Distinct()
                .Select(boxId => CreateBoxViewModel(allDuplicatesInCounting, boxId))
                .OrderBy(k => k.BoxNumber).ToList();

            var viewModel = typedFactory.Create<DuplicateBallotsViewModel>();
            viewModel.Initialize(boxesWithDuplicates);
            return viewModel;
        }

        private BoxViewModel CreateBoxViewModel(List<DuplicateBallotModel> duplicatesInCounting, int boxId)
        {
            var boxCountingDetailsModel = boxCountings.Single(p => p.Id == boxId);

            var boxViewModel = typedFactory.Create<BoxViewModel>();
            boxViewModel.Id = boxId;
            boxViewModel.Name = boxCountingDetailsModel.BoxName;
            boxViewModel.BoxNumber = boxCountingDetailsModel.BoxNumber;
            boxViewModel.TotalScanned = boxCountingDetailsModel.ScannedBallots;
            boxViewModel.Duplicates = duplicatesInCounting.Where(p => p.BoxCountingId == boxId).ToList();

            return boxViewModel;
        }
    }
}
