package com.aplusinformatique.aplusmanager.Services;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.aplusinformatique.aplusmanager.Activitys.Activity_Lancement;
import com.aplusinformatique.aplusmanager.R;

public class VerificationBarcodeScanner extends Service {

    private static final String TAG = "distrimobilelog_VerifBCS";
    private final int ID_NOTIFICATION = 1021;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(TAG, "Vérification barcode scanner");

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createNotificationChannel(notificationManager) : "";
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
        Notification notification = notificationBuilder.setOngoing(true)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle("Vérification Barcode Scanner")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setCategory(NotificationCompat.CATEGORY_SERVICE)
                .build();

        startForeground(ID_NOTIFICATION, notification);

        final PackageManager pm = getPackageManager();

        try {

            pm.getPackageInfo("com.google.zxing.client.android", 0);
            sendResponse(1);

        } catch (PackageManager.NameNotFoundException e1) {

            sendResponse(0);
            Intent replyIntent = new Intent(getApplicationContext(), Activity_Lancement.class);
            replyIntent.putExtra("barcodescanner", true);
            replyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(replyIntent);

        }

        FinService();
    }

    private void sendResponse(int success) {
        Intent replyIntent = new Intent();
        replyIntent.setAction("com.aplusinformatique.aplusmanager.verificationbarcodescannerreponse");
        replyIntent.putExtra("success", success);
        sendBroadcast(replyIntent);
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private String createNotificationChannel(NotificationManager notificationManager) {
        String channelId = TAG;
        String channelName = "Vérification Barcode Scanner";
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW);
        // omitted the LED color
        channel.setImportance(NotificationManager.IMPORTANCE_LOW);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        notificationManager.createNotificationChannel(channel);
        return channelId;
    }

    private void FinService() {
        stopForeground(true);
        stopSelf();
    }
}