import java.util.ArrayList;
import java.util.List;

public class TSSRow {
	private List<TSSClause> clauses = new ArrayList<TSSClause>();
	private String chapter;

	private String tablePart1 = " <tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>\r\n"
			+ "  <td width=146 rowspan={{ROWSPANDOUBLE}} valign=top style='width:109.35pt;border:solid windowtext 1.0pt;\r\n"
			+ "  border-bottom:solid windowtext 1.0pt;mso-border-top-alt:solid windowtext .75pt;mso-border-left-alt:\r\n"
			+ "  solid windowtext .5pt;mso-border-right-alt:solid windowtext .5pt;padding:\r\n"
			+ "  0cm 3.5pt 0cm 1.4pt'>\r\n"
			+ "  <p class=TAL style='line-height:115%;page-break-after:auto'><span lang=EN-GB>{{CHAPTER}}</span></p>\r\n"
			+ "  </td>\r\n"
			+ "  <td width=260 rowspan={{ROWSPANHALF}} valign=top style='width:195.0pt;border:solid windowtext 1.0pt;\r\n"
			+ "  border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:\r\n"
			+ "  solid windowtext .75pt;mso-border-left-alt:solid windowtext .5pt;padding:\r\n"
			+ "  0cm 3.5pt 0cm 1.4pt'>\r\n"
			+ "  <p class=TAL style='line-height:115%;page-break-after:auto'><span lang=EN-GB>{{CLAUSE}}</span></p>\r\n"
			+ "  </td>\r\n" + "";

	private String tablePart2 = " </tr>";
	private String tablePart3 = " <tr style='mso-yfti-irow:1';mso-yfti-lastrow:yes>\r\n";
	private String tablePart5 = " <tr style='mso-yfti-irow:3';mso-yfti-lastrow:yes>\r\n" + 
			"  <td width=260 rowspan={{ROWSPANHALF}} valign=top style='width:195.0pt;border-top:none;\r\n" + 
			"  border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;\r\n" + 
			"  mso-border-top-alt:solid windowtext .75pt;mso-border-left-alt:solid windowtext .5pt;\r\n" + 
			"  mso-border-alt:solid windowtext .75pt;mso-border-left-alt:solid windowtext .5pt;\r\n" + 
			"  padding:0cm 3.5pt 0cm 1.4pt'>\r\n" + 
			"  <p class=TAL style='line-height:115%;page-break-after:auto'><span lang=EN-GB>{{CLAUSE}}</span></p>\r\n" + 
			"  </td>\r\n";
	
	private String tdPart1 = "  <td width=227 valign=top style='width:170.0pt;border:solid windowtext 1.0pt;\r\n"
			+ "  border-left:none;border-bottom:solid windowtext 1.0pt;mso-border-left-alt:solid windowtext .75pt;mso-border-alt:\r\n"
			+ "  solid windowtext .75pt;padding:0cm 3.5pt 0cm 1.4pt'>\r\n"
			+ "  <p class=TAL style='line-height:115%;page-break-after:auto'><span lang=EN-GB>{{TESTCASE}}</span></p>\r\n"
			+ "  </td>\r\n" + "";

	public List<TSSClause> getClauses() {
		return clauses;
	}

	public void addClause(TSSClause clause) {
		clauses.add(clause);
	}

	public void setClauses(List<TSSClause> clauses) {
		this.clauses = clauses;
	}

	public String getChapter() {
		return chapter;
	}

	public void setChapter(String chapter) {
		this.chapter = chapter;
	}

	@Override
	public String toString() {
		return "TSSRow [\nclauses=" + clauses + ", \nchapter=" + chapter + "]";
	}

	public String toHtml() {
		StringBuffer result = new StringBuffer();

		int totalTestCaseCount = 0;
		for (int i = 0; i < clauses.size(); i++) {
			totalTestCaseCount += clauses.get(i).getTestCases().size();
		}
		
		for (int i = 0; i < clauses.size(); i++) {
			int testCasesCount = clauses.get(i).getTestCases().size();

			for (int j = 0; j < clauses.get(i).getTestCases().size(); j++) {
				String testCase = clauses.get(i).getTestCases().get(j);
				if ((i==0) && (j==0)) {
					String tPart1 = tablePart1.replaceAll("\\{\\{ROWSPANDOUBLE\\}\\}",
							Integer.toString(totalTestCaseCount)).replaceAll(
							"\\{\\{ROWSPANHALF\\}\\}",
							Integer.toString(testCasesCount)).replaceAll("\\{\\{CHAPTER\\}\\}", getChapter()).replaceAll("\\{\\{CLAUSE\\}\\}", clauses.get(i).getClauseName());
					result.append(tPart1);
					String tdPart = tdPart1.replaceAll("\\{\\{TESTCASE\\}\\}", testCase);
					result.append(tdPart);
					result.append(tablePart2);
					
				} else if ((i > 0) && (j==0)) {
					String tPart2 = tablePart5.replaceAll("\\{\\{CLAUSE\\}\\}", clauses.get(i).getClauseName()).replaceAll(
							"\\{\\{ROWSPANHALF\\}\\}",
							Integer.toString(testCasesCount)).replaceAll("\\{\\{TESTCASE\\}\\}", testCase);
					result.append(tPart2);
					String tdPart = tdPart1.replaceAll("\\{\\{TESTCASE\\}\\}", testCase);
					result.append(tdPart);
					result.append(tablePart2);
				} else if (j > 0) {
					result.append(tablePart3);
					String tdPart = tdPart1.replaceAll("\\{\\{TESTCASE\\}\\}", testCase);
					result.append(tdPart);
					result.append(tablePart2);
				}
				
			}

			result.append(tablePart2);
		}

		return result.toString();
	}

}
