/*
 * 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.DatabankColumns;

import javax.swing.JOptionPane;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.strategyquant.lib.app.MainApp;
import com.strategyquant.lib.databank.Databank;
import com.strategyquant.lib.databank.DatabankColumn;
import com.strategyquant.lib.databank.DatabanksRegistry;
import com.strategyquant.lib.language.L;
import com.strategyquant.lib.results.SQResultsGroup;

public class StrategyName extends DatabankColumn {
	public static final Logger Log = LoggerFactory.getLogger("StrategyName");
	
	public StrategyName() {
		super();
		
		setName(L.t("Strategy Name"));	
		setTooltip(L.t("Strategy Name"));
		setWidth(170);
		setEditable(true);
	}
	
	@Override
	public Object getValue(SQResultsGroup strategyResults, String dataType, String direction, String sampleType, String plType) throws Exception {
		return strategyResults.getString("StrategyName", "?");
	}
	
	@Override
	public String displayString(SQResultsGroup strategyResults, String dataType, String direction, String sampleType, String plType) throws Exception {
		return (String)getValue(strategyResults, dataType, direction, sampleType, plType);
	}
	
	@Override
	public boolean setValue(SQResultsGroup strategyResults, Object value, String dataType, String databankName) {		
		String strategyName = value.toString();
		
		try {
			Databank databank = DatabanksRegistry.get(databankName);
			if(!databank.isStrategyNameUnique(strategyName, strategyResults)) {
	        	
				JOptionPane.showMessageDialog(MainApp.getFrame(),
	        			String.format("Strategy with the name '%s' already exists in the databank.", strategyName)+"\n" +
	        		    "Please enter a unique strategy name.",
	        			"Duplicate Strategy name",
	        		    JOptionPane.ERROR_MESSAGE);
	        	
	        	return false;			
			} 
		} catch (Exception e) {
			Log.error("Exc.", e);
		}
		
		strategyResults.set("StrategyName", strategyName);
		return true;
	}
	
	@Override
	public String exportString(SQResultsGroup strategyResults, String dataType, String direction, String sampleType, String plType) throws Exception {
		return "\""+strategyResults.getString("StrategyName", "?")+"\"";
	}
}