package jforex.guitests;

import javax.swing.*;

import com.dukascopy.api.*;
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TickCountingComponent implements IStrategy {
    private IUserInterface userInterface;
    private IContext context;

    private final String tabName = "TickCounter";

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

        JPanel tab = userInterface.getBottomTab(tabName);
        
        addComponentsToPane(tab);
        
        Buy();    // works fine
    }

    private void addComponentsToPane(Container pane) {

        pane.setLayout(null);

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

        pane.add(btn);

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Buy();    // throws Exception @ engine.submitOrder();
            }
        });

        //customized size and placement of the button
        Insets insets = pane.getInsets();
        btn.setBounds(25 + insets.left, 5 + insets.top, 100, 25);
    }

    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 {
    }

    private void Buy()
    {
        try{ context.getEngine().submitOrder("longEntry", Instrument.EURUSD, IEngine.OrderCommand.BUY, 0.001);}
        catch (JFException e)
        {
            context.getConsole().getOut().println("   -> JFException @ engine.submitOrder();");

            e.printStackTrace();
        }
    }
}