class MapboxOfflineDownloaderService extends Service implements OfflineRegionObserver, OnDownloadMapListener {

    protected boolean persistOfflineMapTask(@Nullable Intent intent) {
        persistOfflineMapTaskCalled = true;
        if (intent == null) {
            return false;
        }

        Bundle extras = intent.getExtras();
        if (extras != null
                && extras.containsKey(Constants.PARCELABLE_KEY_SERVICE_ACTION)) {
            final SERVICE_ACTION serviceAction = (SERVICE_ACTION) extras.get(Constants.PARCELABLE_KEY_SERVICE_ACTION);

            if (extras.containsKey(Constants.PARCELABLE_KEY_MAP_UNIQUE_NAME)
                    && extras.containsKey(Constants.PARCELABLE_KEY_MAPBOX_ACCESS_TOKEN)) {
                final String mapUniqueName = extras.getString(Constants.PARCELABLE_KEY_MAP_UNIQUE_NAME);
                mapBoxAccessToken = extras.getString(Constants.PARCELABLE_KEY_MAPBOX_ACCESS_TOKEN);

                Mapbox.getInstance(this, mapBoxAccessToken);

                MapBoxDownloadTask downloadTask = new MapBoxDownloadTask();
                downloadTask.setMapName(mapUniqueName);
                downloadTask.setMapBoxAccessToken(mapBoxAccessToken);

                if (serviceAction == SERVICE_ACTION.DOWNLOAD_MAP) {
                    if (extras.containsKey(Constants.PARCELABLE_KEY_STYLE_URL)
                            && extras.containsKey(Constants.PARCELABLE_KEY_MAX_ZOOM)
                            && extras.containsKey(Constants.PARCELABLE_KEY_MIN_ZOOM)
                            && extras.containsKey(Constants.PARCELABLE_KEY_TOP_LEFT_BOUND)
                            && extras.containsKey(Constants.PARCELABLE_KEY_TOP_RIGHT_BOUND)
                            && extras.containsKey(Constants.PARCELABLE_KEY_BOTTOM_RIGHT_BOUND)
                            && extras.containsKey(Constants.PARCELABLE_KEY_BOTTOM_LEFT_BOUND)) {

                        downloadTask.setPackageName("kl");
                        downloadTask.setMapBoxStyleUrl(extras.getString(Constants.PARCELABLE_KEY_STYLE_URL));
                        downloadTask.setMaxZoom(ObjectCoercer.coerceNumberObjectToDoublePrimitive(extras.get(Constants.PARCELABLE_KEY_MAX_ZOOM)));
                        downloadTask.setMinZoom(ObjectCoercer.coerceNumberObjectToDoublePrimitive(extras.get(Constants.PARCELABLE_KEY_MIN_ZOOM)));
                        downloadTask.setTopLeftBound((LatLng) extras.getParcelable(Constants.PARCELABLE_KEY_TOP_LEFT_BOUND));
                        downloadTask.setTopRightBound((LatLng) extras.getParcelable(Constants.PARCELABLE_KEY_TOP_RIGHT_BOUND));
                        downloadTask.setBottomRightBound((LatLng) extras.getParcelable(Constants.PARCELABLE_KEY_BOTTOM_RIGHT_BOUND));
                        downloadTask.setBottomLeftBound((LatLng) extras.getParcelable(Constants.PARCELABLE_KEY_BOTTOM_LEFT_BOUND));

                        realmDatabase.deletePendingOfflineMapDownloadsWithSimilarNames(mapUniqueName);

                        MapBoxDownloadTask.constructMapBoxOfflineQueueTask(downloadTask);
                        return true;
                    }
                } else if (serviceAction == SERVICE_ACTION.DELETE_MAP) {
                    MapBoxDeleteTask deleteTask = new MapBoxDeleteTask();
                    deleteTask.setMapBoxAccessToken(mapBoxAccessToken);
                    deleteTask.setMapName(mapUniqueName);

                    MapBoxDeleteTask.constructMapBoxOfflineQueueTask(deleteTask);

                    return true;
                } else {
                    final String taskType = extras.getString(Constants.PARCELABLE_KEY_DELETE_TASK_TYPE, "");

                    if (!TextUtils.isEmpty(taskType)) {
                        if (taskType.equals(MapBoxOfflineQueueTask.TASK_TYPE_DOWNLOAD) && mostRecentMapNameUpdate.equals(mapUniqueName)) {
                            MapBoxOfflineResourcesDownloader mapBoxOfflineResourcesDownloader = MapBoxOfflineResourcesDownloader.getInstance(this, mapBoxAccessToken);

                            // Remove the STOP DOWNLOAD ACTION from the notification so that it cannot be pressed and cause the app to crash!
                            showProgressNotification(mostRecentMapNameUpdate, mostRecentPercentageUpdate, false);

                            // Stop the download first
                            mapBoxOfflineResourcesDownloader.pauseMapDownload(mapUniqueName, new OnPauseMapDownloadCallback() {
                                @Override
                                public void onPauseSuccess() {
                                    MapBoxOfflineResourcesDownloader
                                            .getInstance(MapboxOfflineDownloaderService.this, mapBoxAccessToken)
                                            .deleteMap(mapUniqueName, new OfflineRegion.OfflineRegionDeleteCallback() {

                                                @Override
                                                public void onDelete() {
                                                    if (deleteTaskFromRealmDatabase(taskType, mapUniqueName)) {
                                                        sendBroadcast(SERVICE_ACTION_RESULT.SUCCESSFUL, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD);
                                                        showDownloadCompleteNotification(String.format(getString(R.string.notification_stopped_download_title), mapUniqueName), getString(R.string.notification_stopped_download_content));
                                                    } else {
                                                        sendBroadcast(SERVICE_ACTION_RESULT.FAILED, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD, getString(R.string.map_delete_task_error));
                                                    }
                                                    releaseQueueToPerformOtherJobs();

                                                    performNextTask();
                                                }

                                                @Override
                                                public void onError(String error) {
                                                    sendBroadcast(SERVICE_ACTION_RESULT.FAILED, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD, error);
                                                }
                                            });
                                }

                                @Override
                                public void onPauseError(String error) {
                                    sendBroadcast(SERVICE_ACTION_RESULT.FAILED, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD, String.format(getString(R.string.error_broadcast_for_download_pause), error));
                                }
                            });
                        } else {
                            if (deleteTaskFromRealmDatabase(taskType, mapUniqueName)) {
                                // Communicate if the delete task was successful
                                sendBroadcast(SERVICE_ACTION_RESULT.SUCCESSFUL, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD);
                            } else {
                                // Send a broadcast that the delete task was unsuccessful because the task could not be found
                                sendBroadcast(SERVICE_ACTION_RESULT.FAILED, mapUniqueName, SERVICE_ACTION.STOP_CURRENT_DOWNLOAD, String.format(getString(R.string.error_broadcast_for_download_pause), "Map could not be found"));
                            }
                        }
                    }

                }

            }

        }

        return false;
    }

    public  MapboxOfflineDownloaderService();

}