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

namespace GMCabsDriverAssistantSolution.ViewModels
{
    [QueryProperty(nameof(BookingId), nameof(BookingId))]
    class AcceptedFutureBookingDetailViewModel : BaseViewModel
    {
        #region Fields
        private string _bookingId;

        private float distance;

        private string formattedDistance;

        private string startAddress;

        private string startSuburb;

        private string endAddress;

        private string endSuburb;

        private string rewardPoints;

        private double bookingFee;

        private bool isCorporateBooking;

        private string fareTypeValue;

        private int? fixedAmount;

        private bool isFutureBooking;

        private int pickUpTime;

        private string formattedPickUpTime;
        private string formattedPickUpTimeDateOnly;
        private string formattedPickUpTimeTimeOnly;
        #endregion

        #region Properties
        public string BookingId
        {
            get => _bookingId;
            set
            {
                _bookingId = value;
                LoadAcceptedFutureBooking(Guid.Parse(value));
            }
        }

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

        public string FormattedDistance
        {
            get => (distance >= 1000) ? $"{distance / 1000:0.##}k" : $"{(int)distance}m";
            set => SetProperty(ref formattedDistance, value);
        }

        public string StartAddress
        {
            get => startAddress;
            set => SetProperty(ref startAddress, value);
        }

        public string StartSuburb
        {
            get => startSuburb;
            set => SetProperty(ref startSuburb, value);
        }

        public string EndAddress
        {
            get => endAddress;
            set => SetProperty(ref endAddress, value);
        }

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

        public string RewardPoints
        {
            get => rewardPoints;
            set => SetProperty(ref rewardPoints, value);
        }

        public double BookingFee
        {
            get => bookingFee;
            set => SetProperty(ref bookingFee, value);
        }

        public bool IsCorporateBooking
        {
            get => isCorporateBooking;
            set => SetProperty(ref isCorporateBooking, value);
        }

        public string FareTypeValue
        {
            get => fareTypeValue;
            set => SetProperty(ref fareTypeValue, value);
        }

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

        public int PickUpTime
        {
            get => pickUpTime;
            set => SetProperty(ref pickUpTime, value);
        }
        public string FormattedPickUpTime
        {
            get
            {
                DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                dateTime = dateTime.AddSeconds(pickUpTime).ToLocalTime();
                return dateTime.ToString("dd/MM/yyyy hh:mm tt");
            }
            set => SetProperty(ref formattedPickUpTime, value);
        }

        #endregion

        #region Constructor
        public AcceptedFutureBookingDetailViewModel()
        {
            Title = "Accepted Future Booking Detail";
        }
        #endregion

        #region Methods
        public async void LoadAcceptedFutureBooking(Guid bookingId)
        {
            string rydoAccessToken = Preferences.Get(EftposLoginResponse.RYDO_ACCESS_TOKEN, "");
            GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
            var booking = await gmCabsDriverService.GetBookingDetails(rydoAccessToken, bookingId);
            if (booking.BookingId == Guid.Empty)
            {
                await Shell.Current.GoToAsync("..");
            }

            Distance = booking.Distance;
            FormattedDistance = booking.Distance.ToString();
            StartAddress = booking.StartAddress;
            StartSuburb = booking.StartSuburb;
            EndAddress = booking.EndAddress;
            EndSuburb = booking.EndSuburb;
            IsCorporateBooking = booking.IsCorporate;
            FixedAmount = booking.FixedAmount;
            IsFutureBooking = booking.FutureBooking;
            PickUpTime = booking.PickupTime;
            FormattedPickUpTime = booking.PickupTime.ToString();
            BookingFee = 0;

            if (FixedAmount != null)
            {
                decimal amount = (decimal)(FixedAmount / 100.0);
                FareTypeValue = "$" + amount;
            }
            else
            {
                FareTypeValue = "METER";
            }

            RewardPoints = booking.RydoStars > 0 ? string.Format($"+{booking.RydoStars}") : booking.RydoStars.ToString();

            var preferredDriver = booking.PriorityDriver;
            if (preferredDriver)
            {
                if (booking.FareType == FareType.Fixed)
                {
                    if (booking.FixedAmount > booking.MinFareAmount)
                    {
                        BookingFee = booking.ProviderChargeFixedFarePreferred.Value;
                    }
                }
                else if (booking.FareType == FareType.Meter)
                {
                    BookingFee = booking.ProviderChargeMeterFarePreferred.Value;
                }
            }
            else
            {
                if (booking.FareType == FareType.Fixed)
                {
                    if (booking.FixedAmount > booking.MinFareAmount)
                    {
                        BookingFee = booking.ProviderChargeFixedFare.Value;
                    }
                }
                else if (booking.FareType == FareType.Meter)
                {
                    BookingFee = booking.ProviderChargeMeterFare.Value;
                }
            }
        }

        public async Task OnDeclineBookingClickedAsync()
        {
            string rydoAccessToken = Preferences.Get(EftposLoginResponse.RYDO_ACCESS_TOKEN, "");
            GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
            var result = await gmCabsDriverService.DeclineFutureBooking(rydoAccessToken, _bookingId);
            if (result)
            {
                await Shell.Current.GoToAsync("../AcceptedFutureBookingsPage");
            }

        }
        #endregion

    }
}
