package com.casic.dcms.service;

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

import androidx.annotation.Nullable;

import com.amap.api.location.AMapLocation;
import com.casic.dcms.ui.MainActivity;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.LocationHelper;
import com.casic.dcms.utils.NotificationHelper;
import com.casic.dcms.utils.callback.ILocationListener;

public class UploadLocationService extends Service {

    private static final String TAG = "UploadLocationService";

    @Override
    public void onCreate() {
        super.onCreate();
        NotificationHelper notificationHelper = NotificationHelper.getInstance(this);
        Notification notification = notificationHelper.createKeepAlive();
        startForeground(Integer.MAX_VALUE, notification);
        LocationHelper.obtainCurrentLocationByGD(this, new ILocationListener() {
            @Override
            public void onLocationGet(Location location) {

            }

            @Override
            public void onAMapLocationGet(AMapLocation aMapLocation) {
                Log.d(TAG, "onAMapLocationGet: UploadLocationService");
                Message message = Message.obtain();
                message.what = Constant.UPLOAD_LOCATION;
                message.obj = aMapLocation;
                MainActivity.sendMessage(message);
            }
        }, false);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 如果Service被终止，当资源允许情况下，重启service
        return START_STICKY;
    }

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