/*
         * Gets the position of the given transfer in terms of longitude and latitude
         */
        private Position GetCurrentPoint(OrganTransfer transfer)
        {
            double degToRad = Math.PI / 180;

            double timeToDest = transfer.arrivalTime.ToDateTimeWithSeconds().Subtract(DateTime.Now).TotalSeconds;

            double distToDest = timeToDest * 70 / 1852;

            double distRads = (Math.PI / (180 * 60)) * distToDest;

            double deltaLongitude = transfer.startLon - transfer.endLon;



            double x = Math.Cos(transfer.endLat * degToRad) * Math.Sin(deltaLongitude * degToRad);
            double y = Math.Cos(transfer.startLat * degToRad) * Math.Sin(transfer.endLat * degToRad) - Math.Sin(transfer.startLat * degToRad) * Math.Cos(transfer.endLat * degToRad) * Math.Cos(deltaLongitude * degToRad);

            double bearing = Math.Atan2(x, y) / degToRad - 180;

            double currentLat = Math.Asin(Math.Sin(transfer.endLat * degToRad) * Math.Cos(distRads) + Math.Cos(transfer.endLat * degToRad) * Math.Sin(distRads) * Math.Cos(bearing * degToRad));

            double test = currentLat / degToRad;

            double distanceLon = Math.Atan2(Math.Sin(bearing * degToRad) * Math.Sin(distRads) * Math.Cos(transfer.endLat * degToRad), Math.Cos(distRads) - Math.Sin(transfer.endLat * degToRad) * Math.Sin(currentLat));
            double currentLon  = ((transfer.endLon * degToRad - distanceLon + Math.PI) % (2 * Math.PI)) - Math.PI;

            return(new Position(currentLat / degToRad, currentLon / degToRad));
        }
        /*
         * Starts a new transfer to the hospital closest to the given selectedRecipient but does not add a helicopter to the map
         */
        public async Task NewTransferWithoutAddingHelicpoter(DonatableOrgan currentOrgan, User selectedRecipient, Position donorPosition)
        {
            await InitialiseHospitalsWithoutAddingToMap();

            OrganTransfer newOrganTransfer = new OrganTransfer();

            newOrganTransfer.id         = currentOrgan.id;
            newOrganTransfer.receiverId = selectedRecipient.id;

            //Find the position of the donor
            newOrganTransfer.startLat = donorPosition.Latitude;
            newOrganTransfer.startLon = donorPosition.Longitude;

            Hospital receiverHospital = null;

            //await InitialiseHospitalsWithoutAddingToMap();
            foreach (Hospital hospital in hospitals)
            {
                if (hospital.region.Equals(selectedRecipient.region))
                {
                    receiverHospital = hospital;
                }
            }

            //Find the nearest hospital
            newOrganTransfer.endLat = receiverHospital.latitude;
            newOrganTransfer.endLon = receiverHospital.longitude;

            newOrganTransfer.organType = OrganExtensions.ToOrgan(currentOrgan.organType);

            Position HospitalPosition = new Position(receiverHospital.latitude, receiverHospital.longitude);

            newOrganTransfer.arrivalTime = new CustomDateTime(DateTime.Now.AddSeconds(distance(donorPosition.Latitude, HospitalPosition.Latitude,
                                                                                               donorPosition.Longitude, HospitalPosition.Longitude, 0, 0) / 70));

            TransplantListAPI transplantListAPI = new TransplantListAPI();
            await transplantListAPI.InsertTransfer(newOrganTransfer);

            await transplantListAPI.SetInTransfer(currentOrgan.id, 1);
        }
        /*
         * Starts a transfer to the hospital closest to the given selectedRecipient and adds the helicopter to the map
         */
        public async Task NewTransfer(DonatableOrgan currentOrgan, User selectedRecipient, Position donorPosition)
        {
            OrganTransfer newOrganTransfer = new OrganTransfer();

            newOrganTransfer.id         = currentOrgan.id;
            newOrganTransfer.receiverId = selectedRecipient.id;

            //Find the position of the donor
            newOrganTransfer.startLat = donorPosition.Latitude;
            newOrganTransfer.startLon = donorPosition.Longitude;

            Hospital receiverHospital = null;

            //await InitialiseHospitalsWithoutAddingToMap();
            foreach (Hospital hospital in hospitals)
            {
                if (hospital.region.Equals(selectedRecipient.region))
                {
                    receiverHospital = hospital;
                }
            }

            //Find the nearest hospital
            newOrganTransfer.endLat = receiverHospital.latitude;
            newOrganTransfer.endLon = receiverHospital.longitude;

            newOrganTransfer.organType = OrganExtensions.ToOrgan(currentOrgan.organType);

            Position HospitalPosition = new Position(receiverHospital.latitude, receiverHospital.longitude);

            newOrganTransfer.arrivalTime = new CustomDateTime(DateTime.Now.AddSeconds(distance(donorPosition.Latitude, HospitalPosition.Latitude,
                                                                                               donorPosition.Longitude, HospitalPosition.Longitude, 0, 0) / 70));

            TransplantListAPI transplantListAPI = new TransplantListAPI();

            if (await transplantListAPI.InsertTransfer(newOrganTransfer) != HttpStatusCode.OK)
            {
                await DisplayAlert("", "Failed to start transfer (failed to insert transfer)", "OK");

                return;
            }

            if (await transplantListAPI.SetInTransfer(currentOrgan.id, 1) != true)
            {
                await DisplayAlert("", "Failed to start transfer (failed to set in transfer)", "OK");

                return;
            }

            int TTA = (int)newOrganTransfer.arrivalTime.ToDateTimeWithSeconds().Subtract(DateTime.Now).TotalSeconds;

            //int waitingListId = await transplantListAPI.GetWaitingListId((int)newOrganTransfer.receiverId, newOrganTransfer.organType);
            //if (waitingListId == 0) {
            //    await DisplayAlert("", "Failed to start transfer (failed to get waiting list id)", "OK");
            //    return;
            //}

            int waitingListId = 0;

            foreach (WaitingListItem item in selectedRecipient.waitingListItems)
            {
                if (item.organType == OrganExtensions.ToOrgan(currentOrgan.organType))
                {
                    waitingListId = item.id;
                }
            }

            AddHelicopter(donorPosition,
                          HospitalPosition,
                          newOrganTransfer.organType,
                          TTA,
                          newOrganTransfer.receiverId,
                          waitingListId,
                          newOrganTransfer.id
                          );
        }