package io.magnum.antimalware.features;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.os.Environment;

import weka.core.Attribute;
import weka.core.Instance;

/**
 * A singleton that adds extra functionality to the Weka library.
 * @author Brandon Amos
 * @version 2012.08.10
 */
public class WekaHelper {
  private static WekaHelper _instance = new WekaHelper();

  private WekaHelper() {
    // Prevents instantiation of this class
  }

  public static WekaHelper getInstance() {
    return _instance;
  }

  public ArrayList<Attribute> addLists(ArrayList<Attribute>... lists) {
    ArrayList<Attribute> result = new ArrayList<Attribute>();
    for (ArrayList<Attribute> list : lists) {
      result.addAll(list);
    }
    return result;
  }

  /**
   * @param attributes The attributes to be created into a list.
   * @return An ArrayList of numeric Attribute objects.
   */
  public ArrayList<Attribute> createNumAttributeList(String... attributes) {
    ArrayList<Attribute> attributeList = 
      new ArrayList<Attribute>(attributes.length);
    for (String attribute : attributes) {
      attributeList.add(new Attribute(attribute));
    }
    return attributeList;
  }

  /**
   * This fixes the indexes for an ArrayList of Attributes to
   * fix a bug that was encountered when merging Instances.
   * @param attributes The list of Attribute objects
   */
  public void resetAttributes(ArrayList<Attribute> attributes) {
    for (int i = 0; i < attributes.size(); i++) {
      attributes.get(i).setIndex(i);
    }
  }

  /**
   * Helper function for merging instances.
   * @param instances The instances to merge.
   * @return The merged instances.
   */
  public Instance mergeInstances(Instance... instances) {
    Instance result = instances[0];
    for (int i = 1; i < instances.length; i++) {
      result = result.mergeInstance(instances[i]);
    }
    return result;
  }

  /**
   * Helper function for merging instances.
   * @param instanceList The list of instances to merge.
   * @return The merged instances.
   */
  public Instance mergeInstances(ArrayList<Instance> instanceList) {
    Instance result = instanceList.get(0);
    for (int i = 1; i < instanceList.size(); i++) {
      result = result.mergeInstance(instanceList.get(i));
    }
    return result;
  }


  /**
   * @param dataSet The dataset to an ARFF file.
   */
  public void writeArff(String dataSet) {
    writeStringToFile("profilingInformation.arff", dataSet);
  }

  public void writeClassification(String classification) {
    writeStringToFile("classification", classification);
  }

  public String getClassification() {
    String classification = "";

    File file = new File(getMagDir(), "classification");

    try {
      BufferedReader br =
        new BufferedReader(new FileReader(file));
      classification = br.readLine();
      br.close();
    } catch (IOException e) {
      // If the file can't be read, set it to positive
      writeClassification("positive");
    }

    return classification;
  }

  public void setInstance(Instance instance, ArrayList<Attribute> attributes, double... values) {
    for (int i = 0; i < values.length; i++) {
      instance.setValue(attributes.get(i), values[i]);
    }
  }

  private void writeStringToFile(String fileName, String text) {
    FileOutputStream fos;
    try {
      File file = new File(getMagDir(), fileName);
      fos = new FileOutputStream(file);
      fos.write(text.getBytes());
      fos.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public File getMagDir() {
    File sdCard = Environment.getExternalStorageDirectory();
    File magDir = new File(
        sdCard.getAbsolutePath() + "/magnum/");
    magDir.mkdirs();
    return magDir;
  }


  /* 
  // http://weka.wikispaces.com/Programmatic+Use
  public static String example() throws Exception {
  List<String> fvBattIsCharging = new ArrayList<String>(2);
  fvBattIsCharging.add("true");
  fvBattIsCharging.add("false");
  Attribute battIsCharging = 
  new Attribute("battIsCharging", fvBattIsCharging);
  fvAttributes.add(battIsCharging);

  Attribute battVoltage = new Attribute("battVoltage");
  fvAttributes.add(battVoltage);

  Attribute battCurrent = new Attribute("battCurrent");
  fvAttributes.add(battCurrent);

  Attribute battTemp = new Attribute("battTemp");
  fvAttributes.add(battTemp);

  Attribute battLevelChange = new Attribute("battLevelChange");
  fvAttributes.add(battLevelChange);

  Attribute battLevel = new Attribute("battLevel");
  fvAttributes.add(battLevel);

// Classification
List<String> fvClassVal = new ArrayList<String>(2);
fvClassVal.add("positive");
fvClassVal.add("negative");
Attribute classAttribute = new Attribute("classification", fvClassVal);

fvAttributes.add(classAttribute);

// Create a training set
Instances isTrainingSet = new Instances("relation", fvAttributes, 0);      
isTrainingSet.setClassIndex(fvAttributes.size()-1);

// Create the instance
Instance iExample = new DenseInstance(fvAttributes.size());
iExample.setValue(fvAttributes.get(0), "true");     
iExample.setValue(fvAttributes.get(1), 1);    
iExample.setValue(fvAttributes.get(2), 2);    
iExample.setValue(fvAttributes.get(3), 3);    
iExample.setValue(fvAttributes.get(4), 4); 
iExample.setValue(fvAttributes.get(5), 5);
iExample.setValue(fvAttributes.get(6), "positive");

// Add the instance
isTrainingSet.add(iExample);
Classifier cModel = (Classifier) new NaiveBayes();  
cModel.buildClassifier(isTrainingSet);

// Test the model
Evaluation eTest = new Evaluation(isTrainingSet);
eTest.evaluateModel(cModel, isTrainingSet);

// Print the result
String strSummary = eTest.toSummaryString();
Log.i(TAG, strSummary);

// Get the confusion matrix
double[][] cmMatrix = eTest.confusionMatrix();
for(int row_i=0; row_i<cmMatrix.length; row_i++){
for(int col_i=0; col_i<cmMatrix.length; col_i++){
Log.i(TAG, "" + cmMatrix[row_i][col_i]);
Log.i(TAG, "|");
}
Log.i(TAG, "");
}

return isTrainingSet.toString();
  }
*/
}

@@ -2597,22 +2597,9 @@
 ofil
-ingInformation
+e
 .arf

Change ARFF name.