package org.pharmgkb.pharmcat.reporter.model;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.StringUtils;


/**
 * @author Lester Carter
 * @author Ryan Whaley
 */
public class PharmcatException {
  private static final int sf_rowLength = 9;

  public PharmcatException(@Nonnull String row) throws RuntimeException {
    String[] fields = row.split("\\t");

    if (fields.length < sf_rowLength) {
      throw new RuntimeException("Row not of expected length "+sf_rowLength);
    }

    m_name = fields[0];
    m_exceptionType = fields[8];
    m_message = fields[9];
    m_version = fields[10];

    m_matches = new MatchLogic();
    m_matches.setGene(fields[1]);
    m_matches.setHapsCalled(parseList(fields[2]));
    m_matches.setHapsMissing(parseList(fields[3]));
    m_matches.setVariantsMissing(parseList(fields[4]));
    m_matches.setDips(parseList(fields[5]));
    m_matches.setDrugs(parseList(fields[6]));
  }

  @Expose
  @SerializedName("rule_name")
  private String m_name;
  @Expose
  @SerializedName("version")
  private String m_version;
  @Expose
  @SerializedName("matches")
  private MatchLogic m_matches;
  @Expose
  @SerializedName("exception_type")
  private String m_exceptionType;
  @Expose
  @SerializedName("message")
  private String m_message;

  public String getName() {
    return m_name;
  }

  public void setName(String rule_name) {
    m_name = rule_name;
  }

  public String getVersion() {
    return m_version;
  }

  public void setVersion(String version) {
    m_version = version;
  }

  public MatchLogic getMatches() {
    return m_matches;
  }

  public void setMatches(MatchLogic matches) {
    m_matches = matches;
  }

  public String getExceptionType() {
    return m_exceptionType;
  }

  public void setExceptionType(String exceptionType) {
    m_exceptionType = exceptionType;
  }

  public String getMessage() {
    return m_message;
  }

  public void setMessage(String message) {
    m_message = message;
  }

  @Override
  public String toString() {
    return getName();
  }

  private static List<String> parseList(String value) {
    if (StringUtils.isBlank(value)) {
      return new ArrayList<>();
    }

    return ImmutableList.copyOf(Splitter.on(",").trimResults().split(value));
  }
}

@@ -484,17 +484,18 @@
 ength = 
-9
+11
 ;%0A%0A  pub
@@ -756,96 +756,8 @@
 %5B0%5D;
-%0A    m_exceptionType = fields%5B8%5D;%0A    m_message = fields%5B9%5D;%0A    m_version = fields%5B10%5D;
 %0A%0A  
@@ -1022,24 +1022,24 @@
 ields%5B5%5D));%0A
-
     m_matche
@@ -1071,16 +1071,105 @@
 ds%5B6%5D));
+%0A%0A    m_exceptionType = fields%5B8%5D;%0A    m_message = fields%5B9%5D;%0A    m_version = fields%5B10%5D;
 %0A  %7D%0A%0A  

Update exception row length.