using Prism.Events;
using Valg.EVA.Skanning.UserInterfaceElements.ApplicationTermination;

namespace Valg.EVA.Skanning.UserInterfaceElements.BallotPaperImage
{
    public class BallotPaperImageWindowProvider : IBallotPaperImageWindowProvider
    {
        private readonly IBallotPaperImageWindow ballotPaperImageWindow;

        public BallotPaperImageWindowProvider(IEventAggregator eventAggregator, IBallotPaperImageWindow ballotPaperImageWindow)
        {
            this.ballotPaperImageWindow = ballotPaperImageWindow;

            eventAggregator.GetEvent<BallotPaperImageUpdatedEvent>().Subscribe(UpdateBallotPaperImageViewModel);
            eventAggregator.GetEvent<ApplicationTerminateRequestedEvent>().Subscribe(CloseWindow);
        }

        public void Show(IBallotPaperImageViewModel viewModel)
        {
            UpdateBallotPaperImageViewModel(viewModel);
            ballotPaperImageWindow.Show();
            ballotPaperImageWindow.Activate();
            ballotPaperImageWindow.RestoreFromMinimized();
        }

        private void CloseWindow(System.EventArgs obj)
        {
            ballotPaperImageWindow.CloseRequested = true;
            ballotPaperImageWindow.Close();
        }

        public void Hide()
        {
            ballotPaperImageWindow.Hide();
        }

        private void UpdateBallotPaperImageViewModel(IBallotPaperImageViewModel ballotPaperImageViewModel)
        {
            ballotPaperImageWindow.SetBallotPaperImageViewModel(ballotPaperImageViewModel);
        }
    }
}
