using GMCabsDriverAssistant.Models;
using GMCabsDriverAssistant.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GMCabsDriverAssistantSolution.ViewModels
{
    class ImeiNumberInstallViewModel : BaseViewModel
    {
        #region Fields
        private string selectedImei;
        private bool isSelectImeiDetailsVisisble = false;
        private string selectedCarNumber;
        private string installedImei;
        private bool installedImeiExists;
        #endregion

        #region Properties
        public ObservableCollection<ImeiDto> ImeiNumberLists { get; }
        public Command<ImeiDto> SelectImei { get; }
        public string SelectedImei
        {
            get => selectedImei;
            set => SetProperty(ref selectedImei, value);
        }
        public bool IsSelectImeiDetailsVisisble
        {
            get => isSelectImeiDetailsVisisble;
            set
            {
                SetProperty(ref isSelectImeiDetailsVisisble, value);
            }
        }
        public string InstalledImei
        {
            get => installedImei;
            set => SetProperty(ref installedImei, value);
        }
        public bool InstalledImeiExists
        {
            get => installedImeiExists;
            set => SetProperty(ref installedImeiExists, value);
        }
        #endregion

        #region Constructor
        public ImeiNumberInstallViewModel()
        {
            Title = "Taxi Installation";
            ImeiNumberLists = new ObservableCollection<ImeiDto>();
            SelectImei = new Command<ImeiDto>(OnselectedImeiButtonClicked);
            InstalledImei = Preferences.Get("imeiNumber", null);
            if (!string.IsNullOrEmpty(InstalledImei))
            {
                InstalledImeiExists = true;
            }
            else
            {
                InstalledImeiExists = false;
                InstalledImei = "NOT INSTALLED";
            }
        }
        #endregion

        #region Methods
        public async void GetImeiDetails(string entry)
        {
            if (entry == String.Empty)
            {
                ImeiNumberLists.Clear();
            }
            else
            {
                GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
                var imeiLists = await gmCabsDriverService.GetAvailableImeiNumbers(entry);
                if (imeiLists != null && imeiLists.Count > 0)
                {
                    ImeiNumberLists.Clear();
                    foreach (var imei in imeiLists)
                    {
                        ImeiDto imeiDto = new ImeiDto();
                        imeiDto.ImeiNumber = imei.ImeiNumber;
                        imeiDto.CarNumber = imei.CarNumber;
                        ImeiNumberLists.Add(imeiDto);
                    }
                }
                else
                {
                    ImeiNumberLists.Clear();
                }
            }
        }
        private void OnselectedImeiButtonClicked(ImeiDto imeiDto)
        {
            IsSelectImeiDetailsVisisble = true;
            SelectedImei = imeiDto.ImeiNumber;
            selectedCarNumber = imeiDto.CarNumber;
        }
        public void CompleteInstallation()
        {
            Preferences.Set("imeiNumber", SelectedImei);
            Preferences.Set("carNumber", selectedCarNumber);
            Preferences.Set("IsTablet", true);
            //GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
            //await gmCabsDriverService.CompleteInstallation(SelectedImei);            
        }
        #endregion
    }
}
