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.FeedVersion;
import com.conveyal.r5.transit.TransportNetwork;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * Created by landon on 10/11/16.
 */
public class ReadTransportNetworkJob extends MonitorableJob {
    public FeedVersion feedVersion;
    public TransportNetwork result;
    public Status status;

    public ReadTransportNetworkJob (FeedVersion feedVersion, String owner) {
        super(owner, "Reading in 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("Reading network");
        File is = null;
        is = new File(DataManager.config.get("application").get("data").get("gtfs").asText() + "/"  + feedVersion.feedSourceId + "/" + feedVersion.id + "_network.dat");
        try {
            feedVersion.transportNetwork = TransportNetwork.read(is);
            // check to see if distance tables are built yet... should be removed once better caching strategy is implemeneted.
            if (feedVersion.transportNetwork.transitLayer.stopToVertexDistanceTables == null) {
                feedVersion.transportNetwork.transitLayer.buildDistanceTables(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        synchronized (status) {
            status.message = "Transport network read 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;
//        }
//    }

}

@@ -256,16 +256,73 @@
 Network;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -589,16 +589,110 @@
 leJob %7B%0A
+    private static final Logger LOG = LoggerFactory.getLogger(ReadTransportNetworkJob.class);%0A
     publ
@@ -1199,26 +1199,16 @@
     
-System.out.println
+LOG.info
 (%22Re
@@ -1239,23 +1239,16 @@
  File is
- = null
 ;%0A      
@@ -1258,161 +1258,44 @@
 s = 
-new File(DataManager.config.get(%22application%22).get(%22data%22).get(%22gtfs%22).asText() + %22/%22  + feedVersion.feedSourceId + %22/%22 + feedVersion.id + %22_n
+feedVersion.getTransportN
 etwork
-.dat%22
+Path(
 );%0A 

add logger