package jforex.simpletests;

import com.dukascopy.api.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

@SuppressWarnings("unused")

@RequiresFullAccess
public class TestInvokeAndWait implements IStrategy {
    private IContext context;
    private IConsole console;

    private final String tabName = "TestInvokeAndWait";

    private ArrayList<String> keysList;

    @Override
    public void onStart(final IContext context) throws JFException {
        this.context = context;
        IHistory history = context.getHistory();
        this.console = context.getConsole();

        console.getOut().println("1");
        keysList = new ArrayList<>();

        console.getOut().println("1");
        final JPanel panel = context.getUserInterface().getBottomTab(tabName);
        panel.setLayout(new GridLayout(1, 1));

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    console.getOut().println("2");
                    JButton button = new JButton("Say >>hello<<");
                    button.addActionListener(
                            new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    console.getOut().println("Hello World!!!");
                                }
                            }
                    );
                    panel.add(button);

                    String fName = "aaChosenSMAsEURUSD.xml";
                    String dName = "JForex";

                    console.getOut().println("context.isFullAccessGranted() = " + context.isFullAccessGranted());

                    File f = new File("/" + dName + "/" + fName);
                    console.getOut().println("f = " + f);
                    console.getOut().println("f.exists() = " + f.exists());
                    console.getOut().println("f.canRead() = " + f.canRead());
                    console.getOut().println("f.canWrite() = " + f.canWrite());
                    console.getOut().println("f.getAbsolutePath() = " + f.getAbsolutePath());
                    console.getOut().println("f.getPath() = " + f.getPath());
                    try {
                        FileInputStream fis = new FileInputStream(f);
                        console.getOut().println("fis = " + fis);
                        BufferedInputStream bis = new BufferedInputStream(fis);
                        console.getOut().println("bis = " + bis);
                        XMLDecoder xml = new XMLDecoder(bis);
                        console.getOut().println("xml = " + xml);
                        xml.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (InterruptedException | InvocationTargetException e) {
            e.printStackTrace();
        }

        context.stop();
    }

    @Override
    public void onAccount(IAccount account) throws JFException {}

    @Override
    public void onMessage(final IMessage message) throws JFException {}

    @Override
    public void onStop() throws JFException {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    for (String s : keysList) {
                        for (IChart chart : context.getCharts()) {
                            if (chart.get(s) != null) { chart.remove(s); }
                        }
                    }
                }
            });
        } catch (InterruptedException | InvocationTargetException e) {
            e.printStackTrace();
        }
        context.getUserInterface().removeBottomTab(tabName);
    }

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

    @Override
    public void onBar(final Instrument instrument, final Period period, final IBar askBar, final IBar bidBar) {}
}
