//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2015, University of Osnabrueck                                                                         //
//   All rights reserved.                                                                                               //
//                                                                                                                      //
//   Redistribution and use in source and binary forms, with or without modification, are permitted provided that the   //
//   following conditions are met:                                                                                      //
//                                                                                                                      //
//   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following//
//       disclaimer.                                                                                                    //
//                                                                                                                      //
//   2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the       //
//       following disclaimer in the documentation and/or other materials provided with the distribution.               //
//                                                                                                                      //
//   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, //
//   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE  //
//   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, //
//   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR    //
//   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  //
//   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE   //
//   USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                           //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "ExtractEdges.hpp"

#include "Launcher.hpp"
#include <iomanip>

#include <time.h>

#include "easylogging++.h"
#include "Globals.hpp"
#include "Launcher.hpp"

_INITIALIZE_EASYLOGGINGPP

using namespace launcher;

int main (int argc, char* argv[]) {
    
    char buildingsPolygons[BUFFLEN];
    char terrainPolygons[BUFFLEN];
    char buildingsEdges[BUFFLEN];
    char terrainEdges[BUFFLEN];
    
    if (argc == 5){
    
        strncpy(buildingsPolygons,argv[1],BUFFLEN);
        strncpy(terrainPolygons,argv[2],BUFFLEN);
        strncpy(buildingsEdges,argv[3],BUFFLEN);
        strncpy(terrainEdges,argv[4],BUFFLEN);
        
    }else{
        std::cout << "wrong number of arguments, requires exactly 4" << std::endl;
        exit(42);
    }
    
    std::time_t starttime, currenttime;
    double runtime;
    
    std::string identifier = getIdentifier(buildingsPolygons);
    easyloggingpp::Configurations conf;
    conf.setAll(easyloggingpp::ConfigurationType::ToFile, "true");
    conf.setAll(easyloggingpp::ConfigurationType::ToStandardOutput, "false");
    conf.setAll(easyloggingpp::ConfigurationType::Filename, "tmp/"+identifier+"/logs/"+identifier+"_edge.log");
    conf.setAll(easyloggingpp::ConfigurationType::Format, "%datetime %level %log");
    if(!DEBUG) conf.set(easyloggingpp::Level::Debug, easyloggingpp::ConfigurationType::Enabled, "false");
    conf.set(easyloggingpp::Level::Warning, easyloggingpp::ConfigurationType::ToStandardOutput, "true");
    conf.set(easyloggingpp::Level::Warning, easyloggingpp::ConfigurationType::Format, "%datetime %level %log [%func]");
    easyloggingpp::Loggers::reconfigureLogger("default", conf);
    easyloggingpp::Loggers::reconfigureAllLoggers(conf);
    
    //load polygons and edges
    LOG(INFO) << "Parsing files:";
    LOG(INFO) << buildingsPolygons;
    std::vector<std::vector<double> > polygons =parseFile(buildingsPolygons);
    LOG(INFO) << terrainPolygons;
    std::vector<std::vector<double> > polygons2 =parseFile(terrainPolygons);
    LOG(INFO) << "Buildingpolygons parsed: " << polygons.size();
    LOG(INFO) << "Terrainpolygons parsed: " << polygons2.size();
    for (int i=0; i < polygons2.size(); i++) {
        polygons.push_back(polygons2[i]);
    }
    
    //extract edges
    std::time(&starttime);
    LOG(INFO) << "Extracting edges...";
    std::vector<std::vector<double> > edges, edges2;
    edges  = extractEdges(polygons);
    edges2 = extractEdges(polygons2);
    std::time(&currenttime);
    runtime = currenttime - starttime;
    LOG(INFO) << "Extracted edges in " << runtime << " seconds.";
    LOG(INFO) << "Buildingedges found: " << edges.size();
    LOG(INFO) << "Terrainedges found: " << edges2.size();
    
    LOG(INFO) << "Saving edges.";
    saveEdges(buildingsEdges, edges);
    saveEdges(terrainEdges, edges2);
    
    LOG(INFO) << "Finished...";
        
     
    return EXIT_SUCCESS;
}