package aniter.utils;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.google.gwt.dev.util.collect.HashMap;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.server.rpc.impl.ServerSerializationStreamReader;
import com.shc.webgl4j.client.WebGL10;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import com.gargoylesoftware.htmlunit.html.*;

public class ViewPortUtilWebGL4J extends JFrame

        implements HyperlinkListener {
    // These are the buttons for iterating through the page list.
    private JButton backButton, forwardButton;

    // Page location text field.
    private JTextField locationTextField;

    // Editor pane for displaying pages.
    private JEditorPane displayEditorPane;

    // Browser's list of pages that have been visited.
    private ArrayList pageList = new ArrayList();

//    private Canvas canvas = Canvas.;
//    // Now set the dimensions of the canvas, and add it to the RootPanel.
//        canvas.setCoordinateSpaceWidth(640);
//        canvas.setCoordinateSpaceHeight(480);
//        RootPanel.get().add(canvas);
//
//        // Create the context, here we are creating a WebGL 1.0 context.
//        WebGL10.createContext(canvas);
//
//        // Set the clear color of the canvas, and clear the color buffer. Here we make it back.
//        glClearColor(0, 0, 0, 1);
//        glClear(GL_COLOR_BUFFER_BIT);

    public static String pString = "";

//        public URL pageURL = new URL(pString.toURI().toURL());

    // Constructor for Mini Web Browser.
    public ViewPortUtilWebGL4J(URL pageURL) {
        // Set application title.
        super("ViewPort");

        // Set window size.
        setSize(640, 480);

        // Handle closing events.
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                actionExit();
            }
        });

        // Set up file menu.
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        JMenuItem fileExitMenuItem = new JMenuItem("Exit",
                KeyEvent.VK_X);
        fileExitMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                actionExit();
            }
        });
        fileMenu.add(fileExitMenuItem);
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);

        // Set up button panel.
        JPanel buttonPanel = new JPanel();
        backButton = new JButton("< Back");
        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                actionBack();
            }
        });
        backButton.setEnabled(false);
        buttonPanel.add(backButton);
        forwardButton = new JButton("Forward >");
        forwardButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                actionForward();
            }
        });
        forwardButton.setEnabled(false);
        buttonPanel.add(forwardButton);
        locationTextField = new JTextField(35);
        locationTextField.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    actionGo();
                }
            }
        });
        buttonPanel.add(locationTextField);
        JButton goButton = new JButton("GO");
        goButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                actionGo();
            }
        });
        buttonPanel.add(goButton);

        // Set up page display.
        displayEditorPane = new JEditorPane();
        displayEditorPane.setContentType("text/html");
        displayEditorPane.setEditable(false);
        displayEditorPane.addHyperlinkListener(this);
        displayEditorPane.getPage();
        try {
            displayEditorPane.setPage(pageURL);
        } catch (IOException e) {
            e.printStackTrace();
        }

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(buttonPanel, BorderLayout.NORTH);
        getContentPane().add(new JScrollPane(displayEditorPane),
                BorderLayout.CENTER);
    }

    // Exit this program.
    private void actionExit() {

    }

    // Go back to the page viewed before the current page.
    private void actionBack() {
        URL currentUrl = displayEditorPane.getPage();

        int pageIndex = pageList.indexOf(currentUrl.toString());
        try {
            showPage(
                    new URL((String) pageList.get(pageIndex - 1)), false);
        } catch (Exception e) {}
    }

    // Go forward to the page viewed after the current page.
    private void actionForward() {
        URL currentUrl = displayEditorPane.getPage();
        int pageIndex = pageList.indexOf(currentUrl.toString());
        try {
            showPage(
                    new URL((String) pageList.get(pageIndex + 1)), false);
        } catch (Exception e) {}
    }

    // Load and show the page specified in the location text field.
    private void actionGo() {
        URL verifiedUrl = verifyUrl(locationTextField.getText());
        if (verifiedUrl != null) {
            showPage(verifiedUrl, true);
        } else {
            showError("Invalid URL");
        }
    }

    // Show dialog box with error message.
    private void showError(String errorMessage) {
        JOptionPane.showMessageDialog(this, errorMessage,
                "Error", JOptionPane.ERROR_MESSAGE);
    }

    // Verify URL format.
    private URL verifyUrl(String url) {
        // Only allow HTTP URLs.
//            if (!url.toLowerCase().contains("html"))
//                return null;

        // Verify format of URL.
        URL verifiedUrl = null;
        try {
            verifiedUrl = new URL(url);
        } catch (Exception e) {
            return null;
        }

        return verifiedUrl;
    }

    /* Show the specified page and add it to
       the page list if specified. */
    public URL showPage(URL pageUrl, boolean addToList) {
        // Show hour glass cursor while crawling is under way.
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        URL newURL = null;
        try {
            // Get URL of page currently being displayed.
            URL currentUrl = displayEditorPane.getPage();

            // Load and display specified page.
            displayEditorPane.setPage(pageUrl);

            // Get URL of new page being displayed.
            URL newUrl = displayEditorPane.getPage();

            // Add page to list if specified.
            if (addToList) {
                int listSize = pageList.size();
                if (listSize > 0) {
                    int pageIndex =
                            pageList.indexOf(currentUrl.toString());
                    if (pageIndex < listSize - 1) {
                        for (int i = listSize - 1; i > pageIndex; i--) {
                            pageList.remove(i);
                        }
                    }
                }
                pageList.add(newUrl.toString());
            }

            // Update location text field with URL of current page.
            locationTextField.setText(newUrl.toString());

            // Update buttons based on the page being displayed.
            updateButtons();
        } catch (Exception e) {
            // Show error messsage.
            showError("Unable to load page");
        } finally {
            // Return to default cursor.
            setCursor(Cursor.getDefaultCursor());
        }

        return newURL;
    }

    /* Update back and forward buttons based on
       the page being displayed. */
    private void updateButtons() {
        if (pageList.size() < 2) {
            backButton.setEnabled(false);
            forwardButton.setEnabled(false);
        } else {
            URL currentUrl = displayEditorPane.getPage();
            int pageIndex = pageList.indexOf(currentUrl.toString());
            backButton.setEnabled(pageIndex > 0);
            forwardButton.setEnabled(
                    pageIndex < (pageList.size() - 1));
        }
    }

    // Handle hyperlink's being clicked.
    public void hyperlinkUpdate(HyperlinkEvent event) {
        HyperlinkEvent.EventType eventType = event.getEventType();
        if (eventType == HyperlinkEvent.EventType.ACTIVATED) {
            if (event instanceof HTMLFrameHyperlinkEvent) {
                HTMLFrameHyperlinkEvent linkEvent =
                        (HTMLFrameHyperlinkEvent) event;
                HTMLDocument document =
                        (HTMLDocument) displayEditorPane.getDocument();
                document.processHTMLFrameHyperlinkEvent(linkEvent);
            } else {
                showPage(event.getURL(), true);
            }
        }
    }

    public static WebClient createBrowser (URL yurURL) {

            WebClient browser = new WebClient(BrowserVersion.CHROME);
        try {
            HtmlPage page = browser.getPage(yurURL);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return browser;
    }

//    public Server(WebClient browser)throws IOException{
//        browser = new WebClient(BrowserVersion.CHROME);
//
//        done = false;
//        isSubmitting = false;
//        user_name = "XC2";
//        pass_word = "donthackme";
//        work_list = new ArrayList<>();
//        users_data = new HashMap<>();
//        application = Executors.newCachedThreadPool();
//        //the buffer can be 100 at max (may be adjusted if needed)
//        buffer_queue = new LinkedBlockingQueue<>(100);
//
//
//        //some browser intitialization to increase efficiency (the cookies part is essential)
//        browser.getOptions().setUseInsecureSSL(true);
//        browser.getOptions().setJavaScriptEnabled(true);
//        browser.getOptions().setCssEnabled(true);
//        browser.getOptions().setThrowExceptionOnScriptError(false);
//        browser.getCookieManager().setCookiesEnabled(true);
//        browser.setAjaxController(new NicelyResynchronizingAjaxController());
//    }

    // Run the viewPort
    public static JFrame main(URL pageURL) {
        ViewPortUtilWebGL4J ViewPort = new ViewPortUtilWebGL4J(pageURL);
        ViewPort.showPage(pageURL, true);
        ViewPort.show();
        return ViewPort;
    }
}


