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

import java.util.ArrayList;

import javax.swing.JOptionPane;

import com.strategyquant.core.databanks.DatabankResults;
import com.strategyquant.lib.app.MainApp;
import com.strategyquant.lib.databank.Databank;
import com.strategyquant.lib.databank.DatabankAction;
import com.strategyquant.lib.language.L;
import com.strategyquant.lib.plugins.SQGlobalEventMsg;
import com.strategyquant.lib.plugins.SQGlobalEvents;
import com.strategyquant.lib.results.SQResultsGroup;
import com.strategyquant.lib.settings.SQCoreConst;

public class Delete extends DatabankAction {

	public Delete() {
		setName(L.t("Delete"));
	}
	
	@Override
	public void execute(Databank databank) {
		int count = databank.getNumberOfSelectedResults();
		
		if(count==0) {
        	JOptionPane.showMessageDialog(MainApp.getFrame(),
        			L.t("You have to select some result in order to delete them!"),
        			L.t("Delete error"),
        		    JOptionPane.ERROR_MESSAGE);
        	return;			
		}
		
        int ret = JOptionPane.showConfirmDialog(
                MainApp.getFrame(),
                L.t("Do you really want to remove the selected results from the table?")+"\n"+
                L.t("They will be lost if they are not saved."),
                L.t("Confirmation"),
                JOptionPane.YES_NO_OPTION);
                   
        if(ret == JOptionPane.YES_OPTION) {
        	DatabankResults deletedResults = new DatabankResults(databank.getName());
            String strategyName;
        	
        	ArrayList<SQResultsGroup> results = databank.getSelectedResults();
                	
        	for(SQResultsGroup result : results) {
        		strategyName = result.getString("StrategyName");
        		
        		if(databank.delete(result)) {
        			deletedResults.addResultName(strategyName);
        		}
        	}
        	
        	SQGlobalEvents.getInstance().callChangeEvent(SQGlobalEventMsg.DATABANK_RESULTS_DELETED, deletedResults);
        }
	}
	
	@Override
	public String getProduct() {
		return SQCoreConst.PRODUCT_ANY;
	}
	
	@Override
	public int getPreferredPosition() {
		return 10;
	}
}