using System.ComponentModel;
using Valg.EVA.Skanning.Contracts;
using Valg.EVA.Skanning.Services.SystemSettings;
using Valg.EVA.Skanning.ServicesModel.Internal;
using System.Windows.Input;
using Prism.Commands;
using Valg.EVA.Skanning.UserInterfaceElements;

namespace Valg.Eva.Skanning.Scan.Presentation.RegisterBarcode
{
    public class RegisterBarcodeViewModel : ViewModelBase, IRegisterBarcodeViewModel
    {
        private readonly ISystemSettingsService systemSettingsService;
        private string barcodeLongText;
        private string barcodeShortText;
        private bool isBarcodeLongFocused;
        private bool isBarcodeShortFocused;

        public RegisterBarcodeViewModel(ISystemSettingsService systemSettingsService)
        {
            this.systemSettingsService = systemSettingsService;
            PropertyChanged += OnPropertyChanged;
            ClearBarcodesCommand = new DelegateCommand(Reset);
        }

        private void Reset()
        {
            ClearBarcodes();
            SetFocusOnFirstBarcode();
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
        {
            if (string.IsNullOrEmpty(propertyChangedEventArgs.PropertyName)
                || propertyChangedEventArgs.PropertyName == PropertyHelper.GetName<RegisterBarcodeViewModel>(vm => vm.BarcodeLongText))
            {
                ChangeFocusIfLengthOfBarcodeLongIsCorrect();
            }

            if (propertyChangedEventArgs.PropertyName == PropertyHelper.GetName<RegisterBarcodeViewModel>(v => v.BarcodeLongText)
                || propertyChangedEventArgs.PropertyName == PropertyHelper.GetName<RegisterBarcodeViewModel>(v => v.BarcodeShortText))
            {
                if (IsBarcodeLongValid && IsBarcodeShortValid)
                {
                    RaiseBarcodeCompleteEvent();
                }
            }
        }

        private bool IsBarcodeShortValid
        {
            get
            {
                return !string.IsNullOrEmpty(BarcodeShortText) &&
                       BarcodeShortText.Length == BarCodeShortLength;
            }
        }

        private bool IsBarcodeLongValid
        {
            get
            {
                return !string.IsNullOrEmpty(BarcodeLongText) &&
                       BarcodeLongText.Length == BarCodeLongLength;
            }
        }

        private void ChangeFocusIfLengthOfBarcodeLongIsCorrect()
        {
            if (IsBarcodeLongValid)
            {
                IsBarcodeShortFocused = true;
            }
        }

        public string BarcodeLongText
        {
            get { return barcodeLongText; }
            set
            {
                barcodeLongText = value;
                OnPropertyChanged();
            }
        }

        public int BarCodeLongLength { get { return systemSettingsService.GetBarcodeLongLength(); } }
        public int BarCodeShortLength { get { return GetBarCodeShortLength(); } }


        
        
        
        private int GetBarCodeShortLength()
        {
            int barCodeLength = systemSettingsService.GetBarcodeShortLength();
            if (IsBarcodeLongValid && IsBydelsvalgOsloAndNotVO())
            {
                barCodeLength = systemSettingsService.GetBarcodeShortBoroughLength();
            }

            return barCodeLength;
        }

        private bool IsBydelsvalgOsloAndNotVO()
        {
            bool isBydelsvalgAndNotVO = false;
            var contextId = BarcodeLongText.GetContestIdFromBarcodeLong();
            var votingCategory = BarcodeLongText.GetVotingCategoryTypeFromBarcodeLong();
            if (!contextId.StartsWith("00") && votingCategory.ToUpperInvariant() != VotingCategoryKeys.VO_Valgtingsstemmer)
            {
                isBydelsvalgAndNotVO = true;
            }

            return isBydelsvalgAndNotVO;
        }

        public string BarcodeShortText
        {
            get { return barcodeShortText; }
            set
            {
                barcodeShortText = value;
                OnPropertyChanged();
            }
        }

        public bool IsBarcodeLongFocused
        {
            get { return isBarcodeLongFocused; }
            set
            {
                isBarcodeLongFocused = false;
                OnPropertyChanged();
                isBarcodeLongFocused = value;
                OnPropertyChanged();
            }
        }

        public bool IsBarcodeShortFocused
        {
            get { return isBarcodeShortFocused; }
            set
            {
                isBarcodeShortFocused = false;
                OnPropertyChanged();
                isBarcodeShortFocused = value;
                OnPropertyChanged();
            }
        }
        public void ClearBarcodes()
        {
            BarcodeLongText = null;
            BarcodeShortText = null;
        }

        public void SetFocusOnFirstBarcode()
        {
            DelayedFocusSetter.SetFocus(() => IsBarcodeLongFocused = true);
        }

        public string RegisterBarcodeInfoText
        {
            get { return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.RegisterBarcodeInfoText; }
        }
        public ICommand ClearBarcodesCommand { get; set; }

        private void RaiseBarcodeCompleteEvent()
        {
            PublishEvent<BarcodeCompleteEvent, BarcodeCompleteEventArgs>(new BarcodeCompleteEventArgs { BarcodeLong = BarcodeLongText, BarcodeShort = barcodeShortText });
        }

      }
}

