"use strict";

function TestHelper() {
    var e = require("chai"),
        i = require("./TestHelperPO.js");
    global.expect = e.expect, global.should = e.should(), global.assert = e.assert, e.use(require("chai-as-promised"));
    var s, u = require("./Logger.js"),
        o = protractor.ExpectedConditions,
        l = 1e6;
    return {
        setWaitTime: function(e) {
            l = e, console.log("Assigned wait time to: " + e), u.info("Assigned wait time to: " + e)
        },
        getRandomString: function() {
            for (var e = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz", n = "", t = 0; t < 8; t++) {
                var r = Math.floor(Math.random() * e.length);
                n += e.substring(r, r + 1)
            }
            return u.info("Generated random string: " + n), n
        },
        assertInclude: function(e, n, t) {
            var r;
            try {
                u.info("Asserting if actual: " + e + " is included in expected: " + n + "\n"), assert.include(e, n)
            } catch (e) {
                if (r = e, u.info("Error on assertInclude: " + r), t) return t(r)
            }
            return r
        },
        assertEqual: function(e, n, t) {
            var r;
            try {
                u.info("Asserting if actual: " + e + " is equal to expected: " + n + "\n"), u.info(assert.equal(e, n)), assert.equal(e, n)
            } catch (e) {
                r = e, u.info("Error in assertEqual: " + r + "\n"), t && t(r)
            }
            return r
        },
        assertTrue: function(e, n) {
            var t;
            try {
                u.info("Asserting if actual: " + e + " is equal to True \n"), assert.isTrue(e)
            } catch (e) {
                t = e, u.info("Error in assertTrue: " + t + "\n"), n && n(t)
            }
            return t
        },
        waitForAngular: function() {
            try {
                browser.waitForAngular()
            } catch (e) {
                console.log("Ignoring Error ", e), u.info("Ignoring Error " + e + "\n")
            }
        },
        isElementPresent: function(e, n) {
            if ("string" == typeof e) {
                var t = s.findElement(e, n);
                return browser.wait(o.visibilityOf(t), l).then(function() {
                    return u.info("Found " + n + "!\n"), !0
                }, function() {
                    return !1
                })
            }
            return browser.wait(o.visibilityOf(e), l, "false").then(function() {
                return !0
            }, function() {
                return u.info("Did not find: " + e.locator().value + "!\n"), !1
            })
        },
        isElementNotPresent: function(e, n) {
            if ("string" == typeof e) {
                var t = s.findElement(e, n);
                return browser.wait(o.not(o.visibilityOf(t)), l).then(function() {
                    return u.info("Found " + n + "!\n"), !0
                }, function() {
                    return !1
                })
            }
            return browser.wait(o.not(o.visibilityOf(e)), l, "false").then(function() {
                return !0
            }, function() {
                return u.info("Did not find: " + e.locator().value + "!\n"), !1
            })
        },
        waitForElementToDisappear: function(e, n) {
            if ("string" == typeof e) {
                var t = s.findElement(e, n);
                return browser.wait(o.invisibilityOf(t), l).then(function() {
                    return u.info("Element " + n + "disappeared!\n"), !0
                }, function() {
                    return !1
                })
            }
            return browser.wait(o.invisibilityOf(e), l, "false").then(function() {
                return u.info("Element " + n + "disappeared!\n"), !0
            }, function() {
                return !1
            })
        },
        isElementVisible: function(n, t) {
            if ("string" == typeof n) {
                var r = s.findElement(n, t);
                return browser.wait(r.isPresent(), l).then(function(e) {
                    return !0 === e ? (u.info("Found " + t + "!\n"), browser.wait(r.isDisplayed(), l).then(function(e) {
                        return e ? u.info("is Visible " + t + "!\n") : u.info("not Visible " + t + "!\n"), e
                    })) : (u.info("Not Found " + t + "!\n"), e)
                })
            }
            return browser.wait(n.isPresent(), l).then(function(e) {
                return !0 === e ? (u.info("Found " + n.locator().value + "!\n"), browser.wait(n.isDisplayed(), l).then(function(e) {
                    return e ? u.info("is Visible " + n.locator().value + "!\n") : u.info("not Visible " + n.locator().value + "!\n"), e
                })) : (u.info("Not Found " + n.locator().value + "!\n"), e)
            })
        },
        elementToBeClickable: function(e, n) {
            if ("string" == typeof e) {
                var t = s.findElement(e, n);
                return browser.wait(function() {
                    return t.click().then(function() {
                        return u.info("Clicked on" + n + "!\n"), !0
                    }, function() {
                        return !1
                    })
                }, l)
            }
            return browser.wait(function() {
                return e.click().then(function() {
                    return u.info("Clicked on " + e.locator().value + "!\n"), !0
                }, function() {
                    return !1
                })
            }, l)
        },
        sendKeys: function(n, t, r) {
            return "string" == typeof n ? this.isElementPresent(n, t).then(function() {
                var e = s.findElement(n, t);
                return u.info("Sending Keys to " + t + ": '" + r + "'"), e.sendKeys(r)
            }) : this.isElementPresent(n).then(function() {
                return u.info("Sending Keys to " + n.locator().value + ": '" + t + "'"), n.sendKeys(t)
            })
        },
        getText: function(n, t) {
            return "string" == typeof n ? this.isElementPresent(n, t).then(function() {
                return s.findElement(n, t).getText().then(function(e) {
                    return u.info("Text Value of " + t + ": " + e), e
                })
            }) : this.isElementPresent(n).then(function() {
                return n.getText().then(function(e) {
                    return u.info("Text Value of " + n.locator().value + ": " + e), e
                })
            })
        },
        alertsIsPresent: function() {
            return browser.wait(o.alertIsPresent(), l)
        },
        acceptAlert: function() {
            return browser.switchTo().alert().accept()
        },
        dismissAlert: function() {
            return browser.switchTo().alert().dismiss()
        },
        textToBePresentInElement: function(e, n, t) {
            if ("string" == typeof e) {
                var r = s.findElement(e, n);
                return browser.wait(o.textToBePresentInElement(r, t), l).then(function() {
                    return u.info(t + " is present in " + n + "!\n"), !0
                }, function() {
                    return u.info(t + " is NOT present in " + n + "!\n"), !1
                })
            }
            return browser.wait(o.textToBePresentInElement(e, n), l).then(function() {
                return u.info(n + " is present in " + e.locator().value + "!\n"), !0
            }, function() {
                return u.info(n + " is NOT present in " + e.locator().value + "!\n"), !1
            })
        },
        textToBePresentInElementValue: function(e, n, t) {
            if ("string" == typeof e) {
                var r = s.findElement(e, n);
                return browser.wait(o.textToBePresentInElementValue(r, t), l).then(function() {
                    return u.info(t + " is present in " + n + "'s value!\n"), !0
                }, function() {
                    return u.info(t + " is NOT present in " + n + "'s value!\n"), !1
                })
            }
            return browser.wait(o.textToBePresentInElementValue(e, n), l).then(function() {
                return u.info(n + " is present in " + e.locator().value + "'s value!\n"), !0
            }, function() {
                return u.info(n + " is NOT present in " + e.locator().value + "'s value!\n"), !1
            })
        },
        elementToBeSelected: function(e, n) {
            if ("string" == typeof e) {
                var t = s.findElement(e, n);
                return browser.wait(o.elementToBeSelected(t), l).then(function() {
                    return u.info(n + " is selected!\n"), !0
                }, function() {
                    return u.info(n + " is NOT selected!\n"), !1
                })
            }
            return browser.wait(o.elementToBeSelected(e), l).then(function() {
                return u.info(e.locator().value + " is selected!\n"), !0
            }, function() {
                return u.info(e.locator().value + " is NOT selected!\n"), !1
            })
        },
        titleContains: function(e) {
            return browser.wait(o.titleContains(e), l)
        },
        titleIs: function(e) {
            return browser.wait(o.titleIs(e), l)
        },
        setElementManager: function(e) {
            s = e
        },
        getElementManager: function() {
            return s
        },
       uploadFile: function (filePath) {
                var deferred = protractor.promise.defer();
                var absolutePath = path.resolve(__dirname, filePath);
                var a = $('input[type="file"]');
                fs.stat(absolutePath, function (err, stat) {
                    if (err == null) {
                        u.info("File exists");
                        console.log("File exists");
                        a.sendKeys(absolutePath).then(() => {
                            u.info("File Uploaded");
                            deferred.fulfill(true);
                        })
                    } else if (err.code == "ENOENT") {
                        u.info("File does not exist!");
                        console.error("File does not exist!");
                        deferred.fulfill(false);

                    } else {
                        u.info("Some other error: ", err.code);
                        console.log("Some other error: ", err.code);
                        deferred.fulfill(false);
                    }
                })

                return deferred.promise;
            },
        iFrameSwitch: function(e, n) {
            if ("string" == typeof e) return this.isElementPresent(e, n).then(function() {
                s.browserFindElement(e, n).then(function(e) {
                    browser.switchTo().frame(e).then(function() {
                        u.info("Switched over to " + e + "iFrame")
                    })
                })
            })
        },
        scrollIntoView: function(n, t) {
            return "string" == typeof n ? this.isElementPresent(n, t).then(function() {
                var e = s.findElement(n, t);
                browser.executeScript("arguments[0].scrollIntoView()", e.getWebElement()).then(function() {
                    u.info("Scrolled to " + t + "\n")
                })
            }) : this.isElementPresent(n).then(function() {
                browser.executeScript("arguments[0].scrollIntoView()", n.getWebElement()).then(function() {
                    u.info("Scrolled to " + n.locator().value + "\n")
                })
            })
        },
        dragAndDrop: function(r, i, e, o) {
            return "string" == typeof page ? this.isElementPresent(r, i).then(function() {
                var n = s.findElement(r, i),
                    t = s.findElement(e, o);
                n.getAttribute("draggable").then(function(e) {
                    null != e ? browser.executeScript("./drag.js", n.getWebElement(), t.getWebElement()).then(function() {
                        u.info("Dragged " + i + " to " + o)
                    }) : browser.executeScript("./dragFn.js", n.getWebElement(), t.getWebElement()).then(function() {
                        u.info("Dragged " + i + " to " + o), browser.actions().mouseMove(t).mouseUp().perform()
                    })
                })
            }) : this.isElementPresent(r).then(function() {
                r.getAttribute("draggable").then(function(e) {
                    null != e ? browser.executeScript("./drag.js", r.getWebElement(), i.getWebElement()).then(function() {
                        u.info("Dragged " + r.locator().value + " to " + i.locator().value)
                    }) : browser.executeScript("./dragFn.js", r.getWebElement(), i.getWebElement()).then(function() {
                        u.info("Dragged " + r.locator().value + " to " + i.locator().value), browser.actions().mouseMove(i).mouseUp().perform()
                    })
                })
            })
        },
        dragAndDrop2: function(t, r, i, o) {
            return "string" == typeof t ? this.isElementPresent(t, r).then(function() {
                var e = s.findElement(t, r),
                    n = s.findElement(i, o);
                browser.actions().dragAndDrop(e, n).mouseUp().perform()
            }) : this.isElementPresent(t).then(function() {
                browser.actions().dragAndDrop(t, r).mouseUp().perform()
            })
        },
        getAttribute: function(n, t, r) {
            return "string" == typeof n ? (console.log("inside here 22"), this.isElementPresent(n, t).then(function() {
                return s.findElement(n, t).getAttribute(r).then(function(e) {
                    return u.info(t + "'s attribute " + r + ": " + e), e
                })
            })) : this.isElementPresent(n).then(function() {
                return n.getAttribute(t).then(function(e) {
                    return u.info(n.locator().value + "'s attribute " + t + ": " + e), e
                })
            })
        },
        getCssValue: function(n, t, r) {
            return "string" == typeof n ? this.isElementPresent(n, t).then(function() {
                return s.findElement(n, t).getCssValue(r).then(function(e) {
                    return u.info(t + "'s CSS Value " + r + ": " + e), e
                })
            }) : this.isElementPresent(n).then(function() {
                return n.getCssValue(t).then(function(e) {
                    return u.info(n.locator().value + "'s CSS Value " + t + ": " + e), e
                })
            })
        },
        geSSOLogin: function(e, n) {
            var t, r;
            return t = void 0 !== e ? e : "502641091", r = void 0 !== n ? n : "Aut0mati0n", i.isElementPresent(element(by.id("username"))).then(function() {
                element(by.id("username")).sendKeys(t), element(by.id("password")).sendKeys(r), element(by.id("password")).sendKeys(protractor.Key.ENTER), u.info("Logged in GE SSO Page")
            })
        }
    }
}
module.exports = new TestHelper;