using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Prism.Commands;
using Valg.EVA.Skanning.JobManagement.Startup.Data;
using Valg.EVA.Skanning.UserInterfaceElements;

namespace Valg.EVA.Skanning.JobManagement.Startup.UI.Counting
{
    public class GenerateBarcodesViewModel : ViewModelBase
    {
        private int numberOfBoxes = 1;
        private CountingModel countingModel;

        public ObservableCollection<BarcodeItem> BarcodeItems { get; } = new ObservableCollection<BarcodeItem>();
        public ICommand GenerateBarcodesCommand => new DelegateCommand(GenerateBarcodesAndUpdateView);

        public void Initialize(CountingModel model)
        {
            countingModel = model;
            GenerateBarcodesAndUpdateView(); 
        }

        public int NumberOfBoxes
        {
            get { return numberOfBoxes; }
            set
            {
                numberOfBoxes = value;
                OnPropertyChanged();
            }
        }

        public Boolean HasBarcodes => BarcodeItems.Count > 0;

        public void GenerateBarcodesAndUpdateView()
        {
            BarcodeItems.Clear();

            for (var i = 0; i < NumberOfBoxes; i++)
            {
                BarcodeItems.Add(new BarcodeItem
                {
                    Code1 = $"{countingModel.ElectionEventId}{countingModel.ElectionGroup}{countingModel.ElectionIdentifier}{countingModel.ContestId}{i + 1:D2}{NumberOfBoxes:D2}1{countingModel.VotingCategory}",
                    Code2 = countingModel.EmlAreaContextId.Replace(".", "")
                });
            }

            OnPropertyChanged(nameof(HasBarcodes));
        }
    }
}

