package co.ootf.garmin;

import javastrava.api.v3.auth.AuthorisationService;
import javastrava.api.v3.auth.impl.retrofit.AuthorisationServiceImpl;
import javastrava.api.v3.auth.model.Token;
import javastrava.api.v3.model.StravaActivity;
import javastrava.api.v3.model.StravaUploadResponse;
import javastrava.api.v3.service.Strava;
import javastrava.api.v3.service.exception.BadRequestException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.Arrays;

public class GarminUploadApp {

    private static final Log LOG = LogFactory.getLog(GarminUploadApp.class);
    public static final boolean PRIVATE_DEBUG = true;
    public static final String DATA_TYPE = "fit";

    private static PropertiesService propertiesService = new PropertiesService();

    public static void main(String[] args) {
        File activitiesFolder = null;
        String deviceName = null;
        if (args.length > 0) {
            String path = args[0];
            if (StringUtils.isEmpty(path)) {
                LOG.error("No path specified");
                System.exit(-1);
            }
            File devicePathFile = new File(path);
            deviceName = devicePathFile.getName();
            if (!path.endsWith("/")) {
                path = path + "/";
            }
            activitiesFolder = new File(path + propertiesService.getProperties().getProperty(PropertiesService.DEVICE_ACTIVITIES + deviceName));
        }


        LOG.info(deviceName);

        Token token = null;

        int clientId = Integer.parseInt(propertiesService.getProperties().getProperty(PropertiesService.API_CLIENTID));
        String secret = propertiesService.getProperties().getProperty(PropertiesService.API_SECRET);
        String code = propertiesService.getProperties().getProperty(PropertiesService.API_CODE);

        try {

            AuthorisationService service = new AuthorisationServiceImpl();
            token = service.tokenExchange(clientId, secret, code);
        } catch (BadRequestException e) {
            LOG.error("Please register a Strava Application at \"https://www.strava.com/settings/api\"", e);
            System.exit(-1);
        }

        Strava strava = new Strava(token);

        for (File file : activitiesFolder.listFiles()) {

            final StravaUploadResponse uploadResponse = strava.upload(null, null, null, PRIVATE_DEBUG, false, DATA_TYPE, null, file);

            LOG.info("*** UPLOAD RESPONSE: " + uploadResponse.getStatus());

        }
//        StravaActivity activity = strava.getActivity(351013934);
//
//        LOG.info(activity.getDistance());
//        LOG.info(activity.getElapsedTime());
//        LOG.info(activity.getName());
//        LOG.info(strava.getAthlete(activity.getAthlete().getId()).getFirstname());
//        LOG.info(activity.getGear().getName());

    }

}

@@ -2302,16 +2302,42 @@
 token);%0A
+        int fileCount = 0;
 %0A       
@@ -2383,24 +2383,122 @@
 Files()) %7B%0A%0A
+            LOG.info(%22Attempting to upload file #%22 + fileCount++ + %22 for device %22 + deviceName);%0A%0A
             
@@ -2636,20 +2636,21 @@
     LOG.
-info
+debug
 (%22*** UP
@@ -2697,25 +2697,24 @@
 tus());%0A
-%0A
         
 %7D%0A//    
@@ -2709,306 +2709,219 @@
     
-%7D%0A//        StravaActivity activity = strava.getActivity(351013934);%0A//%0A//        LOG.info(activity.getDistance());%0A//        LOG.info(activity.getElapsedTime());%0A//        LOG.info(activity.getName());%0A//        LOG.info(strava.getAthlete(activity.getAthlete().getId()).getFirstname()
+    LOG.debug(%22*** UPLOAD ERROR: %22 + uploadResponse.getError());%0A%0A            if (uploadResponse.getError() == null) %7B%0A                LOG.info(%22Upload appears to have been successful, deleting file.%22
 );%0A
-//
         
 LOG.
@@ -2920,47 +2920,55 @@
     
-LOG.info(activity.getGear().getName());
+        file.delete();%0A            %7D%0A%0A        %7D
 %0A%0A  

Delete file on error free.