import phantom from 'phantom';

export default class Browser {
    constructor() {
        this.page = undefined;
        this.phantom = undefined;
    }

    createPhantom() {
        return new Promise((resolve, reject) => {
            console.info('creating phantom');

            phantom.create(phantom => {
                if (phantom) {
                    this.phantom = phantom;
                    console.log('phantom created.');
                    resolve(this.phantom);
                } else {
                    reject('error creating phantom');
                }
            }, {
                dnodeOpts: {
                    weak: false,
                },
            });
        });
    }

    createPage() {
        return new Promise((resolve, reject) => {
            console.info('creating page');

            this.phantom.createPage(page => {
                if (page) {
                    this.page = page;
                    resolve(this.page);
                } else {
                    reject('error creating page');
                }
            });
        });
    }

    open({url}) {
        return new Promise((resolve, reject) => {
            console.info('loading %s', url);

            this.page.open(url, status => {
                setTimeout(resolve, 2000);
            });
        });
    }

    screenshot({filename}) {
        return new Promise((resolve, reject) => {
            if (this.page) {
                console.info('screenshot: %s', filename);

                this.page.render(filename, resolve);
            } else {
                reject('error when taking screenshot, page is null');
            }
        });
    }

    exit() {
        return new Promise(resolve => {
            console.info('exiting');
            this.phantom.exit();
            resolve();
        });
    }

    submit({file}) {
        return new Promise(resolve => {
            console.log('submiting file');

            this.page.evaluate((file) => {
                editor.getSession().setValue(file);
                $('.send-submit').click();
            }, () => {
                setTimeout(resolve, 2000);
            }, file);
        });
    }

    login({email, password}) {
        return new Promise(resolve => {
            console.log('submiting email and password');

            this.page.evaluate((config) => {
                document.getElementById('UserEmail').value = config.email;
                document.getElementById('UserPassword').value = config.password;
                document.forms[0].submit();
            },
            () => {
                setTimeout(resolve, 2000);
            },
            {
                email,
                password,
            });
        });
    }

}

@@ -224,55 +224,8 @@
 %3E %7B%0A
-            console.info('creating phantom');%0A%0A
     
@@ -256,24 +256,24 @@
 hantom =%3E %7B%0A
+
             
@@ -339,61 +339,8 @@
 om;%0A
-                    console.log('phantom created.');%0A
     
@@ -681,52 +681,8 @@
 %3E %7B%0A
-            console.info('creating page');%0A%0A
     
@@ -743,32 +743,84 @@
     if (page) %7B%0A
+                    page.set('onError', () =%3E %7B%7D);%0A%0A
                 
@@ -841,16 +841,17 @@
 = page;%0A
+%0A
         
@@ -1060,77 +1060,21 @@
 ise(
-(
 resolve
-, reject)
  =%3E %7B%0A
-            console.info('loading %25s', url);%0A%0A
     
@@ -1105,14 +1105,10 @@
 rl, 
-status
+()
  =%3E 
@@ -1299,67 +1299,8 @@
 ) %7B%0A
-                console.info('screenshot: %25s', filename);%0A%0A
     
@@ -1529,45 +1529,8 @@
 %3E %7B%0A
-            console.info('exiting');%0A
     
@@ -1665,52 +1665,8 @@
 %3E %7B%0A
-            console.log('submiting file');%0A%0A
     
@@ -1933,24 +1933,24 @@
 assword%7D) %7B%0A
+
         retu
@@ -1981,66 +1981,8 @@
 %3E %7B%0A
-            console.log('submiting email and password');%0A%0A
     

remove phantom stdout