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

namespace GMCabsDriverAssistantSolution.ViewModels
{
    public class AcceptedFutureBookingsViewModel : BaseViewModel
    {
        #region Fields
        private string emptyViewMessage;

        private string startSuburb;

        private string endSuburb;

        private float distance;
        //private string formattedDistance;

        private bool isFutureBooking;

        private BookingDto _selectedBooking;
        #endregion

        #region Properties
        public string EmptyViewMessage
        {
            get => emptyViewMessage;
            set => SetProperty(ref emptyViewMessage, value);
        }
        public string StartSuburb
        {
            get => startSuburb;
            set => SetProperty(ref startSuburb, value);
        }

        public string EndSuburb
        {
            get => endSuburb;
            set => SetProperty(ref endSuburb, value);
        }

        public float Distance
        {
            get => distance;
            set => SetProperty(ref distance, value);
        }

        public bool IsFutureBooking
        {
            get => isFutureBooking;
            set => SetProperty(ref isFutureBooking, value);
        }

        public BookingDto SelectedBooking
        {
            get => _selectedBooking;
            set
            {
                SetProperty(ref _selectedBooking, value);
                OnFutureBookingSelected(value);
            }
        }

        public ObservableCollection<BookingDto> FutureBookings { get; }
        public Command<BookingDto> BookingTapped { get; }
        public Command OnRefreshClicked { get; }
        #endregion

        #region Constructor
        public AcceptedFutureBookingsViewModel()
        {
            Title = "Accepted Bookings";
            FutureBookings = new ObservableCollection<BookingDto>();
            OnRefreshClicked = new Command(async () => { await GetRydoFutureBookings(); });
            BookingTapped = new Command<BookingDto>(OnFutureBookingSelected);
        }
        #endregion

        #region Methods
        public void OnAppearing()
        {
            //IsBusy = true;
            SelectedBooking = null;
            Task.Run(async () =>
            {
                await GetRydoFutureBookings();
            });
        }

        private async Task GetRydoFutureBookings()
        {
            EmptyViewMessage = "Loading Future Bookings...";
            string rydoAccessToken = Preferences.Get(EftposLoginResponse.RYDO_ACCESS_TOKEN, "");
            Guid driverId = Guid.Parse(Preferences.Get(LoginResponseDto.USER_CODE, ""));
            GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
            FutureBookings.Clear();
            var bookings = await gmCabsDriverService.GetAcceptedFutureBookings(rydoAccessToken, driverId);
            var futureBookings = bookings.Where(b => b.FutureBooking == true);
            if (futureBookings.Count() > 0)
            {
                foreach (BookingDto booking in futureBookings)
                {
                    FutureBookings.Add(booking);
                }
            }
            else
            {
                EmptyViewMessage = "No Future Bookings available";
            }
        }

        async void OnFutureBookingSelected(BookingDto booking)
        {
            if (booking == null) return;

            await Shell.Current.GoToAsync($"{nameof(AcceptedFutureBookingDetailPage)}?{nameof(AcceptedFutureBookingDetailViewModel.BookingId)}={booking.BookingId:N}");
        }
        #endregion
    }
}
