package jforex;

import java.util.*;
import com.dukascopy.api.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.dukascopy.api.IEngine.OrderCommand;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public class russell implements IStrategy {
    private final String tabName = "CompraVende";
    private int contOrdenes = 0;
    private Set<Instrument> pares;
    private Tarea compra;
    private Tarea vende;
    private Future<Object> future;
    
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    
    public void onStart(IContext context) throws JFException {
        final IContext contexto = context;
        
        this.engine  = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        
//      Pares que se van a usar...
        pares = new HashSet<Instrument>();
        
        pares.add(Instrument.GBPUSD);
        pares.add(Instrument.EURGBP);       
        pares.add(Instrument.GBPCHF);
        pares.add(Instrument.CHFJPY);
        pares.add(Instrument.AUDJPY);
        pares.add(Instrument.EURJPY);
        pares.add(Instrument.USDCHF);
        pares.add(Instrument.AUDUSD);       
        pares.add(Instrument.USDJPY);
        pares.add(Instrument.EURUSD);
        pares.add(Instrument.EURCHF);
        pares.add(Instrument.GBPJPY);
                                
        context.setSubscribedInstruments(pares, true);  
//
        JPanel tabOrdenes = userInterface.getBottomTab(tabName);   //Crea una pestaña nueva...
        final JButton btnBuy  = new JButton("¡Compra!");           //Añade dos botones a la pestaña para comprar o vender...
        final JButton btnSell = new JButton("¡Vende!");
        tabOrdenes.add(btnBuy);
        tabOrdenes.add(btnSell);
        compra = new Tarea(OrderCommand.BUY);                     //Lo que se ejecuta al pulsar comprar
        vende  = new Tarea(OrderCommand.SELL);                    //Lo que se ejecuta al pulsar vender   

//      Rutina que ejecuta cada botón al ser pulsado
        btnBuy.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            try{
                future = contexto.executeTask(compra);
             } catch (Exception e1) {
                e1.printStackTrace();
                console.getOut().println(String.format("Error: %s", e1));
            }
        }
        });
        btnSell.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {      
            try{
                future = contexto.executeTask(vende);
             } catch (Exception e2) {
                e2.printStackTrace();
                console.getOut().println(String.format("Error: %s", e2));
            }
            }
        });
//        
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
        userInterface.removeBottomTab(tabName);
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
    
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

////
    private class Tarea implements Callable<Object>{       
        private OrderCommand orderCommand;
        
        public Tarea(OrderCommand command){
             this.orderCommand = command;
        }
        public Object call() throws Exception {
            try {
               console.getOut().println(pares.size());
               for(Instrument ins: pares)
                  engine.submitOrder(ins.getPrimaryCurrency().toString().substring(0,1) + ins.getSecondaryCurrency().toString().substring(0,1)+contOrdenes++,  //Etiqueta
                                     ins,                                                                                                                      //Par
                                     this.orderCommand,                                                                                                        //Tipo orden
                                     0.01                                                                                                                      //Lotes
                                      );
            } catch (JFException e) {
               console.getErr().println(e.getMessage());
               return null;
            }
            return null;
        }        
    }
////
}