using System;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Prism.Commands;
using Valg.EVA.Skanning.UserInterfaceElements.BallotPaperImage.Events;

namespace Valg.EVA.Skanning.UserInterfaceElements.BallotPaperImage
{
    public class BallotPaperImageViewModel : ViewModelBase, IBallotPaperImageViewModel
    {
        private readonly IBallotPaperImageLoader ballotPaperImageLoader;
        private int currentBallotPaperImageIndex;
        private BitmapSource front;
        private BitmapSource back;
        private BitmapSource selected;

        public BallotPaperImageViewModel(IBallotPaperImageLoader ballotPaperImageLoader)
        {
            this.ballotPaperImageLoader = ballotPaperImageLoader;
            WindowIcon = "/Valg.EVA.Skanning.Assets;component/Images/Icons/EvaVerify.ico";
        }

        public BitmapSource Front
        {
            get
            {
                if (front != null || ballotPaperImageLoader == null)
                    return front;
                try
                {
                    front = ballotPaperImageLoader.GetBallotPaperBitmaps(BallotPaperId, null).Front;
                }
                catch (NullReferenceException )
                {
                    
                }
                return front;
            }
            set
            {
                front = value;
                OnPropertyChanged();
            }
        }

        public BitmapSource Back
        {
            get
            {
                if (back != null || ballotPaperImageLoader == null)
                    return back;
                try
                {
                    back = ballotPaperImageLoader.GetBallotPaperBitmaps(BallotPaperId, null).Back;
                }
                catch (NullReferenceException)
                {
                    
                }
                return back;
            }
            set
            {
                back = value;
                OnPropertyChanged();
            }
        }

        public BitmapSource Selected
        {
            get
            {
                if (selected == null)
                {
                    selected = Front;
                }
                return selected;
            }
            set
            {
                selected = value;
                OnPropertyChanged();
            }
        }

        public bool ShowZoomPanelMiniMap { get; set; }

        public int CurrentBallotPaperImageIndex
        {
            get { return currentBallotPaperImageIndex; }
            set
            {
                currentBallotPaperImageIndex = value;
                Selected = currentBallotPaperImageIndex == 0 ? Front : Back;
                OnPropertyChanged();
            }
        }

        public int BallotPaperId { get; set; }
        public int BoxCountingId { get; set; }

        public ICommand FlipImage
        {
            get
            {
                return new DelegateCommand(
                    () =>
                    {
                        CurrentBallotPaperImageIndex = CurrentBallotPaperImageIndex == 0 ? 1 : 0;
                        PublishEvent<FlipImageEvent, FlipImageEventArgs>(new FlipImageEventArgs { Index = CurrentBallotPaperImageIndex });
                    });
            }
        }
        public string WindowIcon { get; }
    }
}
