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

namespace GMCabsDriverAssistantSolution.ViewModels
{
    [QueryProperty(nameof(BookingId), nameof(BookingId))]
    [QueryProperty(nameof(PickUpAddress), nameof(PickUpAddress))]
    [QueryProperty(nameof(DropUpAddress), nameof(DropUpAddress))]
    [QueryProperty(nameof(IsFutureBooking), nameof(IsFutureBooking))]
    [Obsolete]
    public class AcceptBookingViewModel : BaseViewModel
    {
        #region Constants

        private const int ASSISTANT_DRIVER_SOURCE = 4;

        #endregion Constants

        #region Fields
        public int seconds = 180;
        private string timerSeconds = "3:00";
        private string bookingId = "";
        public string pickUpAddress = "";
        public string dropUpAddress = "";
        private string errorMessage = "";
        private bool isProcessing = true;
        private bool isBookingConfirmed = false;
        public bool isBookingUnavailable = false;
        public bool isFutureBooking = false;
        private bool isBookingConfirmedAndOnDemand = false;
        #endregion

        #region Properties
        public string TimerSeconds
        {
            get => timerSeconds;
            set
            {
                SetProperty(ref timerSeconds, value);
            }
        }
        public string ErrorMessage
        {
            get => errorMessage;
            set
            {
                SetProperty(ref errorMessage, value);
            }
        }

        [Obsolete]
        public string BookingId
        {
            get => bookingId;
            set
            {
                SetProperty(ref bookingId, value);
                Processing(value);
            }
        }
        public string PickUpAddress
        {
            get => pickUpAddress;
            set
            {
                SetProperty(ref pickUpAddress, value);
            }
        }
        public string DropUpAddress
        {
            get => dropUpAddress;
            set
            {
                SetProperty(ref dropUpAddress, value);
            }
        }
        public bool IsProcessing
        {
            get => isProcessing;
            set => SetProperty(ref isProcessing, value);
        }
        public bool IsBookingConfirmed
        {
            get => isBookingConfirmed;
            set => SetProperty(ref isBookingConfirmed, value);
        }
        public bool IsBookingUnavailable
        {
            get => isBookingUnavailable;
            set => SetProperty(ref isBookingUnavailable, value);
        }
        public bool IsFutureBooking
        {
            get => isFutureBooking;
            set => SetProperty(ref isFutureBooking, value);
        }
        public bool IsBookingConfirmedAndOnDemand
        {
            get => isBookingConfirmedAndOnDemand;
            set => SetProperty(ref isBookingConfirmedAndOnDemand, value);
        }
        #endregion

        #region Constructor
        public AcceptBookingViewModel()
        {
            Title = "Processing";
        }
        #endregion

        #region Methods 
        [Obsolete]
        private async void Processing(string booking_Id)
        {
            string rydoAccessToken = Preferences.Get(EftposLoginResponse.RYDO_ACCESS_TOKEN, "");
            Guid driverId = Guid.Parse(Preferences.Get(LoginResponseDto.USER_CODE, ""));
            GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
            AcceptDeclineBookingRequest acceptBookingRequest = new AcceptDeclineBookingRequest();

            var location = await Geolocation.GetLastKnownLocationAsync();

            acceptBookingRequest.DriverId = driverId;
            acceptBookingRequest.DriverSource = ASSISTANT_DRIVER_SOURCE;
            acceptBookingRequest.DriverLatitude = location.Latitude;
            acceptBookingRequest.DriverLongitude = location.Longitude;

            var res = await gmCabsDriverService.AcceptBooking(acceptBookingRequest, rydoAccessToken, booking_Id);
            if (res.StatusCode == (int)HttpStatusCode.OK)
            {
                Title = "Booking Confirmed";
                IsProcessing = false;
                IsBookingConfirmed = true;

                if (!IsFutureBooking)
                {
                    IsBookingConfirmedAndOnDemand = true;
                    Device.StartTimer(TimeSpan.FromSeconds(1), () =>
                    {
                        if (seconds > 0)
                        {
                            --seconds;
                            TimerSeconds = seconds / 60 + ":" + (seconds % 60 <= 9 ? "0" + seconds % 60 : "" + seconds % 60);
                            return true;
                        }
                        else
                        {
                            return false;
                        }

                    });
                }
                else
                {
                    IsBookingConfirmedAndOnDemand = false;
                }
            }
            else
            {
                Title = "Booking Unavailable";
                ErrorMessage = res.Message;
                IsProcessing = false;
                IsBookingConfirmed = false;
                IsBookingUnavailable = true;
            }

        }
        #endregion
    }
}
