package com.app.simplitend.locationupdates;

import static com.app.simplitend.patientgeofencing.PatientLocationUpdatesReceiver.LOCATION_EXTRA_KEY;
import static com.app.simplitend.patientgeofencing.PatientLocationUpdatesReceiver.LOCATION_REQUEST_TAG;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.IBinder;
import android.util.Log;

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

import com.app.simplitend.R;
import com.app.simplitend.chats.SocketHelper;
import com.app.simplitend.patientgeofencing.PatientLocationUpdatesReceiver;
import com.google.android.gms.location.LocationServices;

public class LocationService extends Service implements LocationClient.DefaultLocationUpdates {
    
    public static final String ACTION_START_LOCATION_UPDATES = "com.simplitent.action_start_lu";
    public static final String ACTION_STOP_LOCATION_UPDATES = "com.simplitent.action.stop_lu";
    public static final String LOCATION_NOTIFICATION_CHANNEL_ID = "location";

    public static final String LOCATION_UPDATE_MIN_INTERVAL = "min_interval";

    public static final int LOCATION_INTERVAL_BASE_TIME = 25 * 1000;

    private static final int LOCATION_UPDATES_NOTIFICATION_ID = 112;

    private DefaultLocationClient locationClient;

    @Override
    public void onCreate() {
        super.onCreate();
        locationClient = new DefaultLocationClient(this,
                LocationServices.getFusedLocationProviderClient(this));

        SocketHelper.getInstance().establishConnection(null);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null && intent.getAction() != null){
            switch (intent.getAction()){
                case ACTION_START_LOCATION_UPDATES:
                    int minInterval = intent.getIntExtra(LOCATION_UPDATE_MIN_INTERVAL, 0);
                    if (minInterval != 0){
                        removeLocationUpdates();
                        startLocationUpdates(minInterval);
                    }
                    break;
                case ACTION_STOP_LOCATION_UPDATES:
                    stopLocationUpdates();
                    break;
            }
        }
        return START_REDELIVER_INTENT;
    }

    private void stopLocationUpdates() {
        stopForeground(true);
        stopSelf();
        removeLocationUpdates();
        SocketHelper.getInstance().closeConnection();
    }

    private void startLocationUpdates(int minInterval){
        SocketHelper.getInstance().establishConnection(null);

        Notification notification = new NotificationCompat.Builder(this, LOCATION_NOTIFICATION_CHANNEL_ID)
                .setContentTitle("Your phone is connected to your caregiver")
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setOngoing(true)
                .build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        try {
            locationClient.getLocationUpdates(minInterval, this);
        } catch (LocationClient.LocationException e) {
            // do nothing
        }

        startForeground(LOCATION_UPDATES_NOTIFICATION_ID, notification);
        notificationManager.notify(LOCATION_UPDATES_NOTIFICATION_ID, notification);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(LOCATION_REQUEST_TAG, "onDestroy: SERVICE DESTROYED");
        // stop the location updates
        stopForeground(true);
        stopSelf();
        removeLocationUpdates();
        SocketHelper.getInstance().closeConnection();
    }
    
    public void removeLocationUpdates(){
        if (locationClient != null){
            locationClient.removeLocationUpdates();
        }
    }

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

    @Override
    public void locationUpdates(Location location) {
        Intent intent = new Intent(this, PatientLocationUpdatesReceiver.class);
        intent.putExtra(LOCATION_EXTRA_KEY, location);
        sendBroadcast(intent);
    }
}
