/*
 * 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.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

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.results.SQResult;
import com.strategyquant.lib.results.SQResultsGroup;
import com.strategyquant.lib.settings.SQConst;
import com.strategyquant.lib.settings.SQCoreConst;
 
public class SetNote extends DatabankAction {
	private JDialog dialog;
	ArrayList<SQResultsGroup> strategyResults;
	
	public SetNote() {
		setName(L.t("Set note"));
		
		initDialog();
	}
	
	private void initDialog() {
		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		JRadioButton rdbtnNoteFromFile = new JRadioButton(L.t("From file"));
		rdbtnNoteFromFile.setActionCommand("file");
		rdbtnNoteFromFile.setBounds(17, 28, 73, 23);
		rdbtnNoteFromFile.setSelected(true);
		rdbtnNoteFromFile.setFont(new Font("Arial Unicode MS", Font.PLAIN, 11));
		panel.add(rdbtnNoteFromFile);
		
		JRadioButton rdbtnUseSymbolAs = new JRadioButton(L.t("Use symbol as note"));
		rdbtnUseSymbolAs.setActionCommand("symbol");
		rdbtnUseSymbolAs.setSelected(true);
		rdbtnUseSymbolAs.setFont(new Font("Arial Unicode MS", Font.PLAIN, 11));
		rdbtnUseSymbolAs.setBounds(17, 53, 134, 23);
		panel.add(rdbtnUseSymbolAs);
		
		JRadioButton rdbtnUseTimeframeAs = new JRadioButton(L.t("Use timeframe as note"));
		rdbtnUseTimeframeAs.setActionCommand("timeframe");
		rdbtnUseTimeframeAs.setSelected(true);
		rdbtnUseTimeframeAs.setFont(new Font("Arial Unicode MS", Font.PLAIN, 11));
		rdbtnUseTimeframeAs.setBounds(17, 79, 166, 23);
		panel.add(rdbtnUseTimeframeAs);
		
		JRadioButton rdbtnUseSymboltimeframeAs = new JRadioButton(L.t("Use symbol+timeframe as note"));
		rdbtnUseSymboltimeframeAs.setActionCommand("symbolTimeframe");
		rdbtnUseSymboltimeframeAs.setSelected(true);
		rdbtnUseSymboltimeframeAs.setFont(new Font("Arial Unicode MS", Font.PLAIN, 11));
		rdbtnUseSymboltimeframeAs.setBounds(17, 106, 195, 23);
		panel.add(rdbtnUseSymboltimeframeAs);
		
		JRadioButton rdbtnCustom = new JRadioButton(L.t("Custom"));
		rdbtnCustom.setActionCommand("custom");
		rdbtnCustom.setSelected(true);
		rdbtnCustom.setFont(new Font("Arial Unicode MS", Font.PLAIN, 11));
		rdbtnCustom.setBounds(17, 132, 73, 23);
		panel.add(rdbtnCustom);
		
		ButtonGroup rdbtnGroup = new ButtonGroup();
		rdbtnGroup.add(rdbtnNoteFromFile);
		rdbtnGroup.add(rdbtnUseSymbolAs);
		rdbtnGroup.add(rdbtnUseTimeframeAs);
		rdbtnGroup.add(rdbtnUseSymboltimeframeAs);
		rdbtnGroup.add(rdbtnCustom);
		
		JTextField customNoteTextField = new JTextField();
		customNoteTextField.setBounds(97, 134, 177, 20);
		panel.add(customNoteTextField);
		customNoteTextField.setColumns(10);
		
		JButton btnOk = new JButton(L.t("OK"));
		btnOk.setFont(new Font("Arial Unicode MS", Font.PLAIN, 11));
		btnOk.setBounds(194, 178, 80, 23);
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setNote(rdbtnGroup.getSelection().getActionCommand(), customNoteTextField.getText());
				
				dialog.dispose();
			}
		});
		panel.add(btnOk);
		
		dialog = new JDialog(MainApp.getFrame(), L.t("Set note"), true);
		dialog.setBackground(Color.WHITE);
		dialog.setResizable(false);
		dialog.setPreferredSize(new Dimension(300, 250));
		dialog.setMaximumSize(new Dimension(300, 250));
		dialog.setContentPane(panel);
		dialog.pack();
		
	}

	private void setNote(String type, String text) {
		for(SQResultsGroup rg : strategyResults) {
			String symbol = null;
			String timeframe = null;
			
			for(String s : rg.getResultKeysList()) {
				SQResult result = rg.getResult(s);
				
				if(s.equals(SQConst.SYMBOL_PORTFOLIO)) {
					continue;
				}
				
				symbol = s;
				
				int tf = result.getSettings().getInt("Timeframe", SQConst.TF_INT_UNKNOWN);
				timeframe = SQConst.translateTFToString(tf); 
				break;
			}
					
			switch(type) {
				case "file": return;
				case "symbol":
					
					rg.set("Note", symbol);
					break;
					
				case "timeframe":
					rg.set("Note", timeframe);
					break;
					
				case "symbolTimeframe":
					rg.set("Note", symbol+"_"+timeframe);
					break;
					
				default:
					rg.set("Note", text); //custom
			}
		}
	}
	
	@Override
	public void execute(Databank databank) {
		this.strategyResults = databank.getSelectedResults();
		
		if(strategyResults.isEmpty()) {
			JOptionPane.showMessageDialog(MainApp.getFrame(), 
					L.t("Please select at least one report using checkbox."),
					L.t("Info"),
	    		    JOptionPane.INFORMATION_MESSAGE);	

			return;
		}
		
		dialog.setLocationRelativeTo(MainApp.getFrame());
		dialog.setVisible(true);
	}

	@Override
	public String getProduct() {
		return SQCoreConst.PRODUCT_EAA;
	}
	
	@Override
	public int getPreferredPosition() {
		return 100;
	}
}