/*
 * Copyright (c) 2015, StrategyQuant - All rights reserved.
 *
 * Code in this file was made in a good faith that it is correct and does what it should. 
 * If you found a bug in this code OR you have an improvement suggestion OR you want to include 
 * your own code snippet into our standard library please contact us at:
 * http://tasks.strategyquant.com/projects/snippets/
 *
 * This code can be used only within StrategyQuant products. 
 * Every owner of valid (free, trial or commercial) license of any StrategyQuant product 
 * is allowed to freely use, copy, modify or make derivative work of this code without limitations,
 * to be used in all StrategyQuant products and share his/her modifications or derivative work 
 * with the StrategyQuant community.
 *  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES 
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *  
 */
package com.strategyquant.extend.OverviewTab;

import com.strategyquant.lib.language.L;
import com.strategyquant.lib.results.SQResult;
import com.strategyquant.lib.results.SQResultsGroup;
import com.strategyquant.lib.results.SQStats;
import com.strategyquant.lib.results.StatsConst;
import com.strategyquant.lib.results.StatsTypeCombination;
import com.strategyquant.lib.settings.SQConst;
import com.strategyquant.lib.snippets.OverviewTemplate;

public class TSOverview extends OverviewTemplate {
	
	public TSOverview() {
		setName(L.t("TS Overview"));
		setScreenshotName("ts_screenshot.jpg");
		setHtmlTemplateName("ts_template.htm");
	}
	
	@Override
	public String drawValues(SQResultsGroup strategyResult, String symbol, StatsTypeCombination combination) throws Exception {
		template = loadTemplate(); 
		replaceValues(strategyResult, symbol, combination);
		
		return template;
	}

	public void replaceValues(SQResultsGroup strategyResult, String symbol, StatsTypeCombination combination) {
		SQStats statsAll = null;
		SQStats statsLong = null;
		SQStats statsShort = null;
		
		SQStats statsAllPct = null;
		SQStats statsLongPct = null;
		SQStats statsShortPct = null;
		
		SQResult result = null;
		
		if(strategyResult!=null) {
	       	result = strategyResult.getResult(symbol);
	   	   	
	       	if(result!=null) {
	       		statsAll = result.getStats(SQConst.DIRECTION_ALL, combination.getPLType(), SQConst.KEY_SAMPLE_ALL);
	       		statsLong = result.getStats(SQConst.DIRECTION_LONG, combination.getPLType(), SQConst.KEY_SAMPLE_ALL);
	       		statsShort = result.getStats(SQConst.DIRECTION_SHORT, combination.getPLType(), SQConst.KEY_SAMPLE_ALL);
	       		
	       		statsAllPct = result.getStats(SQConst.DIRECTION_ALL, SQConst.PL_IN_PCT, SQConst.KEY_SAMPLE_ALL);
	       		statsLongPct = result.getStats(SQConst.DIRECTION_LONG, SQConst.PL_IN_PCT, SQConst.KEY_SAMPLE_ALL);
	       		statsShortPct = result.getStats(SQConst.DIRECTION_SHORT, SQConst.PL_IN_PCT, SQConst.KEY_SAMPLE_ALL);
	   	   	}
		}
		
		replaceLabel("1", "Total Net Profit");
		replaceValue("1_All", statsAll==null ? NA : d2WithPlType(statsAll.getDouble(StatsConst.NET_PROFIT), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(StatsConst.NET_PROFIT)>0 ? "positiveNum" : "negativeNum");
		replaceValue("1_Long", statsLong==null ? NA : d2WithPlType(statsLong.getDouble(StatsConst.NET_PROFIT), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(StatsConst.NET_PROFIT)>0 ? "positiveNum" : "negativeNum");
		replaceValue("1_Short", statsShort==null ? NA : d2WithPlType(statsShort.getDouble(StatsConst.NET_PROFIT), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(StatsConst.NET_PROFIT)>0 ? "positiveNum" : "negativeNum");
		
		replaceLabel("2", "Gross Profit");
		replaceValue("2_All", statsAll==null ? NA : d2WithPlType(statsAll.getDouble(StatsConst.GROSS_PROFIT), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(StatsConst.GROSS_PROFIT)>0 ? "positiveNum" : "negativeNum");
		replaceValue("2_Long", statsLong==null ? NA : d2WithPlType(statsLong.getDouble(StatsConst.GROSS_PROFIT), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(StatsConst.GROSS_PROFIT)>0 ? "positiveNum" : "negativeNum");
		replaceValue("2_Short", statsShort==null ? NA : d2WithPlType(statsShort.getDouble(StatsConst.GROSS_PROFIT), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(StatsConst.GROSS_PROFIT)>0 ? "positiveNum" : "negativeNum");
		
		replaceLabel("3", "Gross Loss");
		replaceValue("3_All", statsAll==null ? NA : d2WithPlType(statsAll.getDouble(StatsConst.GROSS_LOSS), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(StatsConst.GROSS_LOSS)>0 ? "positiveNum" : "negativeNum");
		replaceValue("3_Long", statsLong==null ? NA : d2WithPlType(statsLong.getDouble(StatsConst.GROSS_LOSS), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(StatsConst.GROSS_LOSS)>0 ? "positiveNum" : "negativeNum");
		replaceValue("3_Short", statsShort==null ? NA : d2WithPlType(statsShort.getDouble(StatsConst.GROSS_LOSS), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(StatsConst.GROSS_LOSS)>0 ? "positiveNum" : "negativeNum");
		
		replaceLabel("4", "Profit Factor");
		replaceValue("4_All", statsAll==null ? NA  : d2(statsAll.getDouble(StatsConst.PROFIT_FACTOR)));
		replaceValue("4_Long", statsLong==null ? NA  : d2(statsLong.getDouble(StatsConst.PROFIT_FACTOR)));
		replaceValue("4_Short", statsShort==null ? NA  : d2(statsShort.getDouble(StatsConst.PROFIT_FACTOR)));
		
		replaceLabel("5", "Total Number of trades");
		replaceValue("5_All", statsAll==null ? NA  : ""+statsAll.getInt(StatsConst.NUMBER_OF_TRADES));
		replaceValue("5_Long", statsLong==null ? NA  : ""+statsLong.getInt(StatsConst.NUMBER_OF_TRADES));
		replaceValue("5_Short", statsShort==null ? NA  : ""+statsShort.getInt(StatsConst.NUMBER_OF_TRADES));

		replaceLabel("6", "Percent Profitable");
		replaceValue("6_All", statsAllPct==null ? NA  : d2WithPlType(statsAllPct.getDouble(StatsConst.WINNING_PCT), SQConst.PL_IN_PCT));
		replaceValue("6_Long", statsLongPct==null ? NA  : d2WithPlType(statsLongPct.getDouble(StatsConst.WINNING_PCT), SQConst.PL_IN_PCT));
		replaceValue("6_Short", statsShortPct==null ? NA  : d2WithPlType(statsShortPct.getDouble(StatsConst.WINNING_PCT), SQConst.PL_IN_PCT));
		
		replaceLabel("7", "Winning Trades");
		replaceValue("7_All", statsAll==null ? NA  : ""+statsAll.getInt(StatsConst.NUMBER_OF_PROFITS));
		replaceValue("7_Long", statsLong==null ? NA  : ""+statsLong.getInt(StatsConst.NUMBER_OF_PROFITS));
		replaceValue("7_Short", statsShort==null ? NA  : ""+statsShort.getInt(StatsConst.NUMBER_OF_PROFITS));
		
		replaceLabel("8", "Losing Trades");
		replaceValue("8_All", statsAll==null ? NA  : ""+statsAll.getInt(StatsConst.NUMBER_OF_LOSSES));
		replaceValue("8_Long", statsLong==null ? NA  : ""+statsLong.getInt(StatsConst.NUMBER_OF_LOSSES));
		replaceValue("8_Short", statsShort==null ? NA  : ""+statsShort.getInt(StatsConst.NUMBER_OF_LOSSES));
		
		replaceLabel("9", "Avg. Trade Net Profit");
		replaceValue("9_All", statsAll==null ? NA  : d2WithPlType(statsAll.getDouble(StatsConst.AVG_TRADE), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(StatsConst.AVG_TRADE)>0 ? "positiveNum" : "negativeNum");
		replaceValue("9_Long", statsLong==null ? NA  : d2WithPlType(statsLong.getDouble(StatsConst.AVG_TRADE), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(StatsConst.AVG_TRADE)>0 ? "positiveNum" : "negativeNum");
		replaceValue("9_Short", statsShort==null ? NA  : d2WithPlType(statsShort.getDouble(StatsConst.AVG_TRADE), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(StatsConst.AVG_TRADE)>0 ? "positiveNum" : "negativeNum");

		replaceLabel("10", "Avg. Winning Trade");
		replaceValue("10_All", statsAll==null ? NA  : d2WithPlType(statsAll.getDouble(StatsConst.AVG_WIN), combination.getPLType()));
		replaceValue("10_Long", statsLong==null ? NA  : d2WithPlType(statsLong.getDouble(StatsConst.AVG_WIN), combination.getPLType()));
		replaceValue("10_Short", statsShort==null ? NA  : d2WithPlType(statsShort.getDouble(StatsConst.AVG_WIN), combination.getPLType()));
		
		replaceLabel("11", "Payout Ratio (Avg Win/Loss)");
		replaceValue("11_All", statsAll==null ? NA  : d2(statsAll.getDouble(StatsConst.PAYOUT_RATIO)));
		replaceValue("11_Long", statsLong==null ? NA  : d2(statsLong.getDouble(StatsConst.PAYOUT_RATIO)));
		replaceValue("11_Short", statsShort==null ? NA  : d2(statsShort.getDouble(StatsConst.PAYOUT_RATIO)));
		
		replaceLabel("12", "Avg. Losing Trade");
		replaceValue("12_All", statsAll==null ? NA  : d2WithPlType(statsAll.getDouble(StatsConst.AVG_LOSS), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(StatsConst.AVG_LOSS)>0 ? "positiveNum" : "negativeNum");
		replaceValue("12_Long", statsLong==null ? NA  : d2WithPlType(statsLong.getDouble(StatsConst.AVG_LOSS), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(StatsConst.AVG_LOSS)>0 ? "positiveNum" : "negativeNum");
		replaceValue("12_Short", statsShort==null ? NA  : d2WithPlType(statsShort.getDouble(StatsConst.AVG_LOSS), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(StatsConst.AVG_LOSS)>0 ? "positiveNum" : "negativeNum");
				
		replaceLabel("13", "Avg. Profit by Day");
		replaceValue("13_All", statsAll==null ? NA  : d2WithPlType(statsAll.getDouble(StatsConst.AVG_PROFIT_BY_DAY), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(StatsConst.AVG_PROFIT_BY_DAY)>0 ? "positiveNum" : "negativeNum");
		replaceValue("13_Long", statsLong==null ? NA  : d2WithPlType(statsLong.getDouble(StatsConst.AVG_PROFIT_BY_DAY), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(StatsConst.AVG_PROFIT_BY_DAY)>0 ? "positiveNum" : "negativeNum");
		replaceValue("13_Short", statsShort==null ? NA  : d2WithPlType(statsShort.getDouble(StatsConst.AVG_PROFIT_BY_DAY), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(StatsConst.AVG_PROFIT_BY_DAY)>0 ? "positiveNum" : "negativeNum");
		
		replaceLabel("14", "Avg. Profit by Month");
		replaceValue("14_All", statsAll==null ? NA  : d2WithPlType(statsAll.getDouble(StatsConst.AVG_PROFIT_BY_MONTH), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(StatsConst.AVG_PROFIT_BY_MONTH)>0 ? "positiveNum" : "negativeNum");
		replaceValue("14_Long", statsLong==null ? NA  : d2WithPlType(statsLong.getDouble(StatsConst.AVG_PROFIT_BY_MONTH), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(StatsConst.AVG_PROFIT_BY_MONTH)>0 ? "positiveNum" : "negativeNum");
		replaceValue("14_Short", statsShort==null ? NA  : d2WithPlType(statsShort.getDouble(StatsConst.AVG_PROFIT_BY_MONTH), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(StatsConst.AVG_PROFIT_BY_MONTH)>0 ? "positiveNum" : "negativeNum");
		
		replaceLabel("15", "Avg. Profit by Year");
		replaceValue("15_All", statsAll==null ? NA  : d2WithPlType(statsAll.getDouble(StatsConst.AVG_PROFIT_BY_YEAR), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(StatsConst.AVG_PROFIT_BY_YEAR)>0 ? "positiveNum" : "negativeNum");
		replaceValue("15_Long", statsLong==null ? NA  : d2WithPlType(statsLong.getDouble(StatsConst.AVG_PROFIT_BY_YEAR), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(StatsConst.AVG_PROFIT_BY_YEAR)>0 ? "positiveNum" : "negativeNum");
		replaceValue("15_Short", statsShort==null ? NA  : d2WithPlType(statsShort.getDouble(StatsConst.AVG_PROFIT_BY_YEAR), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(StatsConst.AVG_PROFIT_BY_YEAR)>0 ? "positiveNum" : "negativeNum");
		
		replaceLabel("16", "Largest Winning Trade");
		replaceValue("16_All", statsAll==null ? NA  : d2WithPlType(statsAll.getDouble(StatsConst.MAX_PROFIT), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(StatsConst.MAX_PROFIT)>0 ? "positiveNum" : "negativeNum");
		replaceValue("16_Long", statsLong==null ? NA  : d2WithPlType(statsLong.getDouble(StatsConst.MAX_PROFIT), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(StatsConst.MAX_PROFIT)>0 ? "positiveNum" : "negativeNum");
		replaceValue("16_Short", statsShort==null ? NA  : d2WithPlType(statsShort.getDouble(StatsConst.MAX_PROFIT), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(StatsConst.MAX_PROFIT)>0 ? "positiveNum" : "negativeNum");
	
		replaceLabel("17", "Largest Losing Trade");
		replaceValue("17_All", statsAll==null ? NA  : d2WithPlType(statsAll.getDouble(StatsConst.MAX_LOSS), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(StatsConst.MAX_LOSS)>0 ? "positiveNum" : "negativeNum");
		replaceValue("17_Long", statsLong==null ? NA  : d2WithPlType(statsLong.getDouble(StatsConst.MAX_LOSS), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(StatsConst.MAX_LOSS)>0 ? "positiveNum" : "negativeNum");
		replaceValue("17_Short", statsShort==null ? NA  : d2WithPlType(statsShort.getDouble(StatsConst.MAX_LOSS), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(StatsConst.MAX_LOSS)>0 ? "positiveNum" : "negativeNum");
		
		replaceLabel("18", "Avg Consecutive Winning Trades");
		replaceValue("18_All", statsAll==null ? NA  : d2(statsAll.getDouble(StatsConst.AVG_CONSEC_WIN)));
		replaceValue("18_Long", statsLong==null ? NA  : d2(statsLong.getDouble(StatsConst.AVG_CONSEC_WIN)));
		replaceValue("18_Short", statsShort==null ? NA  : d2(statsShort.getDouble(StatsConst.AVG_CONSEC_WIN)));
		
		replaceLabel("19", "Avg Consecutive Losing Trades");
		replaceValue("19_All", statsAll==null ? NA  : d2(statsAll.getDouble(StatsConst.AVG_CONSEC_LOSS)));
		replaceValue("19_Long", statsLong==null ? NA  : d2(statsLong.getDouble(StatsConst.AVG_CONSEC_LOSS)));
		replaceValue("19_Short", statsShort==null ? NA  : d2(statsShort.getDouble(StatsConst.AVG_CONSEC_LOSS)));
		
		replaceLabel("20", "Max. Consecutive Winning Trades");
		replaceValue("20_All", statsAll==null ? NA  : ""+statsAll.getInt(StatsConst.MAX_CONSEC_WINS));
		replaceValue("20_Long", statsLong==null ? NA  : ""+statsLong.getInt(StatsConst.MAX_CONSEC_WINS));
		replaceValue("20_Short", statsShort==null ? NA  : ""+statsShort.getInt(StatsConst.MAX_CONSEC_WINS));
		
		replaceLabel("21", "Max. Consecutive Losing Trades");
		replaceValue("21_All", statsAll==null ? NA  : ""+statsAll.getInt(StatsConst.MAX_CONSEC_LOSS));
		replaceValue("21_Long", statsLong==null ? NA  : ""+statsLong.getInt(StatsConst.MAX_CONSEC_LOSS));
		replaceValue("21_Short", statsShort==null ? NA  : ""+statsShort.getInt(StatsConst.MAX_CONSEC_LOSS));

		replaceLabel("22", "Avg. Bars in Total Trades");
		replaceValue("22_All", statsAll==null ? NA  : d2(statsAll.getDouble(StatsConst.AVG_BARS_TRADE)));
		replaceValue("22_Long", statsLong==null ? NA  : d2(statsLong.getDouble(StatsConst.AVG_BARS_TRADE)));
		replaceValue("22_Short", statsShort==null ? NA  : d2(statsShort.getDouble(StatsConst.AVG_BARS_TRADE)));
		
		replaceLabel("23", "Avg. Bars in Winning Trades");
		replaceValue("23_All", statsAll==null ? NA  : d2(statsAll.getDouble(StatsConst.AVG_BARS_WIN)));
		replaceValue("23_Long", statsLong==null ? NA  : d2(statsLong.getDouble(StatsConst.AVG_BARS_WIN)));
		replaceValue("23_Short", statsShort==null ? NA  : d2(statsShort.getDouble(StatsConst.AVG_BARS_WIN)));
		
		replaceLabel("24", "Avg. Bars in Losing Trades");
		replaceValue("24_All", statsAll==null ? NA  : d2(statsAll.getDouble(StatsConst.AVG_BARS_LOSS)));
		replaceValue("24_Long", statsLong==null ? NA  : d2(statsLong.getDouble(StatsConst.AVG_BARS_LOSS)));
		replaceValue("24_Short", statsShort==null ? NA  : d2(statsShort.getDouble(StatsConst.AVG_BARS_LOSS)));
		
		String dd_key = combination.getPLType().equals(SQConst.PL_IN_PCT) ? StatsConst.PCT_DRAWDOWN : (combination.getPLType().equals(SQConst.PL_IN_TICKS) ? StatsConst.PIPS_DRAWDOWN : StatsConst.DRAWDOWN);
		
		replaceLabel("25", "Drawdown");
		replaceValue("25_All", statsAll==null ? NA  : d2WithPlType(statsAll.getDouble(dd_key), combination.getPLType()), statsAll==null ? null : statsAll.getDouble(dd_key)>0 ? "positiveNum" : "negativeNum");
		replaceValue("25_Long", statsLong==null ? NA  : d2WithPlType(statsLong.getDouble(dd_key), combination.getPLType()), statsLong==null ? null : statsLong.getDouble(dd_key)>0 ? "positiveNum" : "negativeNum");
		replaceValue("25_Short", statsShort==null ? NA  : d2WithPlType(statsShort.getDouble(dd_key), combination.getPLType()), statsShort==null ? null : statsShort.getDouble(dd_key)>0 ? "positiveNum" : "negativeNum");
		
		replaceLabel("26", "% Drawdown");
		replaceValue("26_All", statsAll==null ? NA  : d2WithPlType(statsAll.getDouble(StatsConst.PCT_DRAWDOWN), SQConst.PL_IN_PCT), statsAll==null ? null : statsAll.getDouble(StatsConst.PCT_DRAWDOWN)>0 ? "positiveNum" : "negativeNum");
		replaceValue("26_Long", statsLong==null ? NA  : d2WithPlType(statsLong.getDouble(StatsConst.PCT_DRAWDOWN), SQConst.PL_IN_PCT), statsLong==null ? null : statsLong.getDouble(StatsConst.PCT_DRAWDOWN)>0 ? "positiveNum" : "negativeNum");
		replaceValue("26_Short", statsShort==null ? NA  : d2WithPlType(statsShort.getDouble(StatsConst.PCT_DRAWDOWN), SQConst.PL_IN_PCT), statsShort==null ? null : statsShort.getDouble(StatsConst.PCT_DRAWDOWN)>0 ? "positiveNum" : "negativeNum");
	}

}
