using GMCabsDriverAssistant.Models;
using GMCabsDriverAssistant.Utils;
using GMCabsDriverAssistant.ViewModels;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using GMCabsDriverAssistantSolution.ViewModels;

namespace GMCabsDriverAssistant.Services
{
    public class DispatchAppComponentService : BaseViewModel
    {
        #region Properties
        public static DispatchAppComponentService _instance = null;
        public static object chekLock = new object();
        public static string startTripButtonText = "START TRIP";
        public static string startTimeOrEndLocation = "";
        public static string timerSeconds = "00:00:00";
        public static bool meterTripStarted = false;
        public static bool visibleTrip = false;
        public static string startSuburb = "";
        public static bool isOkButtonVisible = false;
        public static bool isStartTripButtonVisible = true;
        public static bool isPlotButtonVisible = true;
        public static string pickUpDateTimeOrEndSuburb = "";
        public static bool isTripStarted = false;
        public static bool isTripEndeded = false;
        public static int seconds = 0;
        public static int autocloseOkSecond = 30;
        public static string bluetoothConnectionStatusText;
        public static string imageIcon = "avail_button.png";
        #endregion

        #region Constructor
        private DispatchAppComponentService()
        { 
        }
        public static DispatchAppComponentService Instance
        {
            get
            {
                lock (chekLock)
                {
                    if (_instance == null)
                        _instance = new DispatchAppComponentService();
                    return _instance;
                }
            }

        }
        #endregion

        #region Methods
        public static void ManageDriverTrip(bool fromMeter)
        {
            if (startTripButtonText.Equals("START TRIP"))
            {
                startTimeOrEndLocation = "Start Time";
                startTrip(fromMeter);
                timerSeconds = "00:00:00";
            }
            else if (startTripButtonText.Equals("DROP OFF"))
            {
                startTimeOrEndLocation = "Ending At";
                endTrip(fromMeter);
                isOkButtonVisible = true;
            }
        }
        private static void startTrip(bool fromMeter)
        {
            meterTripStarted = true;
            imageIcon = "busy_button.png";
            visibleTrip = true;
            pickUpDateTimeOrEndSuburb = DateTime.Now.ToString("dd MMM hh:mm tt");
            startTripButtonText = "DROP OFF";
            Preferences.Set("MeterRunningStatus", "Running");
        }
        private static void endTrip(bool fromMeter)
        {
            meterTripStarted = true;
            imageIcon = "avail_button.png";
            isStartTripButtonVisible = false;
            Preferences.Set("MeterRunningStatus", "Stopped");
        }
        public async static Task<string> GetSuburbName(bool fromMeter, bool isStart)
        {
            int statusflags = 0;
            var token = Preferences.Get(SecureStorageData.Token,"");
            if (isStart)
                statusflags = Constant.DRIVER_STATUS_ON_TRIP;
            else
                statusflags = Constant.DRIVER_STATUS_AVAILABLE;

            GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
            var suburbName = await gmCabsDriverService.UpdateBookingStatus(token, statusflags, fromMeter);
            return suburbName;
        }
        public static void StartTrip(int seconds)
        {
            ++seconds;
            timerSeconds = (seconds / 3600 <= 9 ? "0" + seconds / 3600 : "" + seconds / 3600) + ":" + ((seconds % 3600) / 60 <= 9 ? "0" + (seconds % 3600) / 60 : "" + (seconds % 3600) / 60) + ":" + (seconds % 60 <= 9 ? "0" + seconds % 60 : "" + seconds % 60);
        }
        public static void OnOkClick()
        {
            visibleTrip = false;
            startTripButtonText = "START TRIP";
            isStartTripButtonVisible = true;
            isOkButtonVisible = false;
            Preferences.Set("MeterRunningStatus", "");
        }
        public static string MeterStartTrip()
        {
            startTripButtonText = "START TRIP";
            return startTripButtonText;
        }
        public static string MeterEndTrip()
        {
            startTripButtonText = "DROP OFF";
            return startTripButtonText;
        }
        public static void SetToInitialProperties()
        {
            startTripButtonText = "START TRIP";
            startTimeOrEndLocation = "";
            timerSeconds = "00:00:00";
            meterTripStarted = false;
            imageIcon = "avail_button.png";
            visibleTrip = false;
            startSuburb = "";
            isOkButtonVisible = false;
            isStartTripButtonVisible = true;
            pickUpDateTimeOrEndSuburb = "";
            isTripStarted = false;
            isTripEndeded = false;
            seconds = 0;
            autocloseOkSecond = 30;
        }
        #endregion

    }
}
