package jforex.guitests;

import javax.swing.*;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;

public class TickCountingComponent implements IStrategy {

    private IUserInterface userInterface;
    private IContext context;
    private final String tabName = "TickCounter";
    boolean buy = false;
    private IEngine engine;
    private IHistory history;

    public void onStart(IContext context) throws JFException {
        this.userInterface = context.getUserInterface();
        this.context = context;
        this.engine = engine;
        this.history = context.getHistory();

        JPanel tab = userInterface.getBottomTab(tabName);

        addComponentsToPane(tab);
    }

    private void addComponentsToPane(Container pane) {

        pane.setLayout(null);

        final JButton btn = new JButton("BUY");

        pane.add(btn);

        btn.addActionListener(new BuyAction(context, Instrument.EURUSD));

        //customized size and placement of the button
        Insets insets = pane.getInsets();
        btn.setBounds(25 + insets.left, 5 + insets.top, 100, 25);
    }
    
    class BuyAction implements ActionListener, Callable<Object> {
        
        final IContext context;
        Instrument instrument;
        
        BuyAction(IContext context, Instrument instrument) {
            this.context = context;
            this.instrument = instrument;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                context.executeTask(this);
            } catch (Exception ex) {
                context.getConsole().getErr().println(Thread.currentThread().getName() + " " + ex);
            }
        }
          
        @Override
        public Object call() throws Exception {
            return context.getEngine().submitOrder("longEntry", instrument, IEngine.OrderCommand.BUY, 0.001);
        }
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }

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

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

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


}