// Path: src/it/reyboz/bustorino/backend/Fetcher.java
// public interface Fetcher {
//     /**
//      * Status codes.<br>
//      *<br>
//      * OK: got a response, parsed correctly, obtained some data<br>
//      * CLIENT_OFFLINE: can't connect to the internet<br>
//      * SERVER_ERROR: the server replied anything other than HTTP 200, basically<br>
//      *              for 404 special constant (see @FiveTAPIFetcher)
//      * PARSER_ERROR: the server replied something that can't be parsed, probably it's not the data we're looking for (e.g. "PHP: Fatal Error")<br>
//      * EMPTY_RESULT_SET: the response is valid and indicates there are no stops\routes\"passaggi"\results for your query<br>
//      * NOT_FOUND: response is valid, no parsing errors, but the desired stops/routes wasn't found
//      * QUERY_TOO_SHORT: input more characters and retry.
//      */
//     enum Result {
//         OK, CLIENT_OFFLINE, SERVER_ERROR, SETUP_ERROR,PARSER_ERROR, EMPTY_RESULT_SET, QUERY_TOO_SHORT, SERVER_ERROR_404,
//         CONNECTION_ERROR, NOT_FOUND
//     }
// }
// 
// Path: src/it/reyboz/bustorino/backend/Notifications.java
// public class Notifications {
//     public static final String DEFAULT_CHANNEL_ID ="Default";
// 
//     public static void createDefaultNotificationChannel(Context context) {
//         // Create the NotificationChannel, but only on API 26+ because
//         // the NotificationChannel class is new and not in the support library
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//             CharSequence name = context.getString(R.string.default_notification_channel);
//             String description = context.getString(R.string.default_notification_channel_description);
//             int importance = NotificationManager.IMPORTANCE_DEFAULT;
//             NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID, name, importance);
//             channel.setDescription(description);
//             // Register the channel with the system; you can't change the importance
//             // or other notification behaviors after this
//             NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
//             notificationManager.createNotificationChannel(channel);
//         }
//     }
// 
//     /**
//      * Register a notification channel on Android Oreo and above
//      * @param con a Context
//      * @param name channel name
//      * @param description channel description
//      * @param importance channel importance (from NotificationManager)
//      * @param ID channel ID
//      */
//     public static void createNotificationChannel(Context con, String name, String description, int importance, String ID){
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//             NotificationChannel channel = new NotificationChannel(ID, name, importance);
//             channel.setDescription(description);
//             // Register the channel with the system; you can't change the importance
//             // or other notification behaviors after this
//             NotificationManager notificationManager = con.getSystemService(NotificationManager.class);
//             notificationManager.createNotificationChannel(channel);
//         }
//     }
// }


import java.util.concurrent.atomic.AtomicReference;
import static android.content.Context.MODE_PRIVATE;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.work.*;
import it.reyboz.bustorino.R;
import it.reyboz.bustorino.backend.Fetcher;
import it.reyboz.bustorino.backend.Notifications;
import java.util.concurrent.TimeUnit;
/*
	BusTO - Data components
    Copyright (C) 2021 Fabio Mazza

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.reyboz.bustorino.data;




public class DBUpdateWorker extends Worker{


    public static final String ERROR_CODE_KEY ="Error_Code";
    public static final String ERROR_REASON_KEY = "ERROR_REASON";
    public static final int ERROR_FETCHING_VERSION = 4;
    public static final int ERROR_DOWNLOADING_STOPS = 5;
    public static final int ERROR_DOWNLOADING_LINES = 6;

    public static final String SUCCESS_REASON_KEY = "SUCCESS_REASON";
    public static final int SUCCESS_NO_ACTION_NEEDED = 9;
    public static final int SUCCESS_UPDATE_DONE = 1;

    private final int notifi_ID=62341;

    public static final String FORCED_UPDATE = "FORCED-UPDATE";

    public static final String DEBUG_TAG = "Busto-UpdateWorker";

    private static final long UPDATE_MIN_DELAY= 3*7*24*3600; //3 weeks


    public DBUpdateWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @SuppressLint("RestrictedApi")
    @NonNull
    @Override
    public Result doWork() {
        //register Notification channel
        final Context con = getApplicationContext();
// Path: src/it/reyboz/bustorino/backend/Fetcher.java
// public interface Fetcher {
//     /**
//      * Status codes.<br>
//      *<br>
//      * OK: got a response, parsed correctly, obtained some data<br>
//      * CLIENT_OFFLINE: can't connect to the internet<br>
//      * SERVER_ERROR: the server replied anything other than HTTP 200, basically<br>
//      *              for 404 special constant (see @FiveTAPIFetcher)
//      * PARSER_ERROR: the server replied something that can't be parsed, probably it's not the data we're looking for (e.g. "PHP: Fatal Error")<br>
//      * EMPTY_RESULT_SET: the response is valid and indicates there are no stops\routes\"passaggi"\results for your query<br>
//      * NOT_FOUND: response is valid, no parsing errors, but the desired stops/routes wasn't found
//      * QUERY_TOO_SHORT: input more characters and retry.
//      */
//     enum Result {
//         OK, CLIENT_OFFLINE, SERVER_ERROR, SETUP_ERROR,PARSER_ERROR, EMPTY_RESULT_SET, QUERY_TOO_SHORT, SERVER_ERROR_404,
//         CONNECTION_ERROR, NOT_FOUND
//     }
// }
// 
// Path: src/it/reyboz/bustorino/backend/Notifications.java
// public class Notifications {
//     public static final String DEFAULT_CHANNEL_ID ="Default";
// 
//     public static void createDefaultNotificationChannel(Context context) {
//         // Create the NotificationChannel, but only on API 26+ because
//         // the NotificationChannel class is new and not in the support library
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//             CharSequence name = context.getString(R.string.default_notification_channel);
//             String description = context.getString(R.string.default_notification_channel_description);
//             int importance = NotificationManager.IMPORTANCE_DEFAULT;
//             NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID, name, importance);
//             channel.setDescription(description);
//             // Register the channel with the system; you can't change the importance
//             // or other notification behaviors after this
//             NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
//             notificationManager.createNotificationChannel(channel);
//         }
//     }
// 
//     /**
//      * Register a notification channel on Android Oreo and above
//      * @param con a Context
//      * @param name channel name
//      * @param description channel description
//      * @param importance channel importance (from NotificationManager)
//      * @param ID channel ID
//      */
//     public static void createNotificationChannel(Context con, String name, String description, int importance, String ID){
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//             NotificationChannel channel = new NotificationChannel(ID, name, importance);
//             channel.setDescription(description);
//             // Register the channel with the system; you can't change the importance
//             // or other notification behaviors after this
//             NotificationManager notificationManager = con.getSystemService(NotificationManager.class);
//             notificationManager.createNotificationChannel(channel);
//         }
//     }
// }


// Path: src/it/reyboz/bustorino/data/DBUpdateWorker.java
import java.util.concurrent.atomic.AtomicReference;
import static android.content.Context.MODE_PRIVATE;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.work.*;
import it.reyboz.bustorino.R;
import it.reyboz.bustorino.backend.Fetcher;
import it.reyboz.bustorino.backend.Notifications;
import java.util.concurrent.TimeUnit;

/*
	BusTO - Data components
    Copyright (C) 2021 Fabio Mazza

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.reyboz.bustorino.data;




public class DBUpdateWorker extends Worker{


    public static final String ERROR_CODE_KEY ="Error_Code";
    public static final String ERROR_REASON_KEY = "ERROR_REASON";
    public static final int ERROR_FETCHING_VERSION = 4;
    public static final int ERROR_DOWNLOADING_STOPS = 5;
    public static final int ERROR_DOWNLOADING_LINES = 6;

    public static final String SUCCESS_REASON_KEY = "SUCCESS_REASON";
    public static final int SUCCESS_NO_ACTION_NEEDED = 9;
    public static final int SUCCESS_UPDATE_DONE = 1;

    private final int notifi_ID=62341;

    public static final String FORCED_UPDATE = "FORCED-UPDATE";

    public static final String DEBUG_TAG = "Busto-UpdateWorker";

    private static final long UPDATE_MIN_DELAY= 3*7*24*3600; //3 weeks


    public DBUpdateWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @SuppressLint("RestrictedApi")
    @NonNull
    @Override
    public Result doWork() {
        //register Notification channel
        final Context con = getApplicationContext();
        Notifications.createDefaultNotificationChannel(con);