package eu.artemis.shield.overlay.semanticknowledge.impl;

import it.trs.rd.pshield.data.response.CompositionData;
import it.trs.rd.pshield.data.response.CompositionResponse;
import it.trs.rd.pshield.engine.PShieldApplicationContex;
import it.trs.rd.pshield.engine.PShieldEngineException;
import it.trs.rd.pshield.engine.PShieldService;

import java.util.LinkedList;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

import eu.artemis.shield.overlay.semanticknowledge.ISemanticKnowledge;

public class SemanticKnowledge implements ISemanticKnowledge {
  
  public SemanticKnowledge(){
    
  }
  
  public List semanticComposition(List list
  {
    
    System.out.println("1");
    
    PShieldApplicationContex ctx;
    try 
    {
      System.out.println(System.getProperty("user.dir"));
      ctx = PShieldApplicationContex.getContex();
      System.out.println("2");
       
      PShieldService service = ctx.getPShieldService();
      
      System.out.println("3");
      
      for (int i = 0; i < list.size(); i++)
      {
        try {
          service.addElement(readFileAsString((String)list.get(i)));
          System.out.println(i);
        catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      
      System.out.println("4");
      
      /*HashMap services = new HashMap();
      
      for (int i = 1; i<10; i++ ){
        String alfa = url("resources/data/data_"+i+"_Pilota.owl");
        String beta = spdLevel("resources/data/data_"+i+"_Pilota.owl");
        services.put(alfa, beta);
      }*/
      
      CompositionResponse response = null;
      try {
        response = service.getComposition();
      catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      for (CompositionData composition : response.getCompositions()) {

           System.out.println("------");

          for (String elem : composition.getFuncionalities()) {

              System.out.println(elem);

          }

      }
      
      return response.getCompositions();
    
    catch (PShieldEngineException e1
    {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    return null;
  }

    private static String readFileAsString(String filePaththrows java.io.IOException {
  StringBuffer fileData = new StringBuffer(1000);
  BufferedReader reader = new BufferedReader(new FileReader(filePath));
  char[] buf = new char[1024];
  int numRead = 0;
  while ((numRead = reader.read(buf)) != -1) {
      String readData = String.valueOf(buf, 0, numRead);
      fileData.append(readData);
      buf = new char[1024];
  }
  reader.close();
  return fileData.toString();
    }
    
    /**
     * This function return the spdLevel associated to that owl file 
     @param path
     @return spdLevel
     @throws IOException
     */
   /* private static String spdLevel(String path) throws IOException{
      String file = "";
      BufferedReader in = new BufferedReader(new FileReader(path));
      String control = "        <SPDStatus rdf:datatype=\"&xsd;int\">";
      while((file = in.readLine())!=null){
        if (file.startsWith(control)){
          file = file.substring(control.length(), control.length()+2);
          String a = file.substring(1);
          if (a.equals("<")){
            file = file.substring(0,1);
          }
          System.out.println("Found" + file);
          return file;        
        }
        else {
        System.out.println("Not Found");
        }
    }
      
      return file;
    }*/
    
    /**
     * This function returns the url associated to that owl file
     @param path
     @return url
     @throws IOException
     */
  /*  private static String url(String path) throws IOException{
      String file = "";
      BufferedReader in = new BufferedReader(new FileReader(path));
      String control = "<rdf:RDF xmlns=\"";
      while((file = in.readLine())!=null){
        if (file.startsWith(control)){
          file = file.substring(control.length(), file.length()-1);
          System.out.println("Found" + file);
          return file;
        }
        else {
        System.out.println("Not Found");
        }
    }
      
      return file;
    }*/
}