package com.conveyal.datatools.manager.jobs;

import com.conveyal.datatools.common.status.MonitorableJob;
import com.conveyal.datatools.manager.DataManager;
import com.conveyal.datatools.manager.models.FeedSource;
import com.conveyal.datatools.manager.models.FeedVersion;
import com.conveyal.r5.point_to_point.builder.TNBuilderConfig;
import com.conveyal.r5.transit.TransportNetwork;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import static com.conveyal.datatools.manager.models.Deployment.getOsmExtract;

/**
 * Created by landon on 4/30/16.
 */
public class BuildTransportNetworkJob extends MonitorableJob {

    public FeedVersion feedVersion;
    public TransportNetwork result;
    public Status status;

    public BuildTransportNetworkJob (FeedVersion feedVersion, String owner) {
        super(owner, "Building Transport Network for " + feedVersion.getFeedSource().name, JobType.BUILD_TRANSPORT_NETWORK);
        this.feedVersion = feedVersion;
        this.result = null;
        this.status = new Status();
        status.message = "Waiting to begin job...";
    }

    @Override
    public void run() {
        System.out.println("Building network");
        try {
            feedVersion.buildTransportNetwork(eventBus);
        } catch (Exception e) {
            e.printStackTrace();
            synchronized (status) {
                status.message = "Transport network failed!";
                status.percentComplete = 100;
                status.error = true;
                status.completed = true;
            }

        }
        synchronized (status) {
            status.message = "Transport network built successfully!";
            status.percentComplete = 100;
            status.completed = true;
        }
        jobFinished();
    }

    @Override
    public Status getStatus() {
        synchronized (status) {
            return status.clone();
        }
    }

    @Override
    public void handleStatusEvent(Map statusMap) {
        try {
            synchronized (status) {
                status.message = (String) statusMap.get("message");
                status.percentComplete = (double) statusMap.get("percentComplete");
                status.error = (boolean) statusMap.get("error");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @Override
//    public void handleStatusEvent(StatusEvent statusEvent) {
//        synchronized (status) {
//            status.message = statusEvent.message;
//            status.percentComplete = statusEvent.percentComplete
//            status.error = statusEvent.error;
//        }
//    }

}

@@ -841,21 +841,22 @@
 n;%0A    p
-ublic
+rivate
  Transpo
@@ -1382,52 +1382,446 @@
     
-feedVersion.buildTransportNetwork(eventBus);
+if (feedVersion.validationResult != null) %7B%0A                feedVersion.buildTransportNetwork(eventBus);%0A            %7D%0A            else %7B%0A                synchronized (status) %7B%0A                    status.message = %22Transport network skipped because of bad validation.%22;%0A                    status.percentComplete = 100;%0A                    status.error = true;%0A                    status.completed = true;%0A                %7D%0A            %7D
 %0A   
@@ -2114,25 +2114,24 @@
           %7D%0A
-%0A
         %7D%0A  
@@ -2120,32 +2120,65 @@
     %7D%0A        %7D%0A
+        if (!status.error) %7B%0A    
         synchron
@@ -2185,32 +2185,36 @@
 ized (status) %7B%0A
+    
             stat
@@ -2271,32 +2271,36 @@
 !%22;%0A            
+    
 status.percentCo
@@ -2305,32 +2305,36 @@
 Complete = 100;%0A
+    
             stat
@@ -2346,32 +2346,46 @@
 mpleted = true;%0A
+            %7D%0A
         %7D%0A      

fix up status update