package com.strategyquant.extend.Functions;

import com.strategyquant.lib.settings.SQConst;

public class TestInfo {

	public double expectedValue;
	public String fileName;
	public String resultKey;
	public String direction;
	public String plType;
	public String sampleType;
	public String key;

	public TestInfo(int rowIndex, String rowData) throws Exception {
		String[] data = null;
		data = rowData.split(";");

		if(data.length != 7){
			throw new Exception("Incorrect format - there aren't 7 columns on line "+rowIndex+". Line: "+rowData);
		}
		
		fixData(data);
		
		this.fileName = data[0];
		this.resultKey = data[1];
		this.direction = recognizeDirection(rowIndex, data[2]);
		this.plType = recognizePLType(rowIndex, data[3]);
		this.sampleType = recognizeSampleType(rowIndex, data[4]);
		this.key = data[5];
		
		try {
			this.expectedValue = Double.parseDouble(data[6]);
		} catch(Exception e) {
			throw new Exception("Cannot recognize expected value on line "+rowIndex+". Unrecognized value: '"+data[6]+"', it must be a number!");
		}
	}

	private void fixData(String[] data) {
		for(int i=0; i<data.length; i++) {
			data[i] = data[i].trim().replace(" ", ""); 
		}
	}

	private String recognizeDirection(int rowIndex, String string) throws Exception {
		String upperString = string.toUpperCase();
		if(upperString.contains("BOTH") || upperString.contains("ALL")) {
			return SQConst.DIRECTION_ALL;
		} else if(upperString.contains("LONG")) {
			return SQConst.DIRECTION_LONG;
		} else if(upperString.contains("SHORT")) {
			return SQConst.DIRECTION_SHORT;
		}

		throw new Exception("Cannot recognize direction on line "+rowIndex+". Unrecognized direction: '"+string+"', allowed values are: Both, Long, Short");
	}

	private String recognizePLType(int rowIndex, String string) throws Exception {
		String upperString = string.toUpperCase();
		if(upperString.contains("$") || upperString.contains("MONEY")) {
			return SQConst.PL_IN_MONEY;
		} else if(upperString.contains("%") || upperString.contains("PCT") || upperString.contains("PERCENT")) {
			return SQConst.PL_IN_PCT;
		} else if(upperString.contains("PIP") || upperString.contains("PIPS")) {
			return SQConst.PL_IN_TICKS;
		}

		throw new Exception("Cannot recognize PL Type on line "+rowIndex+". Unrecognized PL Type: '"+string+"', allowed values are: $, %, pips");
	}

	private String recognizeSampleType(int rowIndex, String string) throws Exception {
		String upperString = string.toUpperCase();
		if(upperString.contains("ALL")) {
			return SQConst.KEY_SAMPLE_ALL;
		} else if(upperString.contains("IS") || upperString.contains("INSAMPLE")) {
			return SQConst.KEY_SAMPLE_IS;
		} else if(upperString.contains("OOS") || upperString.contains("OUTOFSAMPLE")) {
			return SQConst.KEY_SAMPLE_IS;
		}

		throw new Exception("Cannot recognize Sample Type on line "+rowIndex+". Unrecognized Sample Type: '"+string+"', allowed values are: All, IS, OOS");
	}

	public String getCompleteKey() {
		return "[Direction: "+direction+", PL Type: "+plType+", Sample Type: "+sampleType+"]   "+key;
	}

	public String getStatsCombination() {
		return "[Direction: "+direction+", PL Type: "+plType+", Sample Type: "+sampleType+"]";
	}


}
