package com.bitbreeds.webrtc.signaling;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.camel.CamelContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Copyright (c) 27/06/16, Jonas Waage
 * <p>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * <p>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
public class BrowserTestFirefoxLossy {

    WebDriver driver;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Before
    public void setup() {
        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();
    }

    @After
    public void tearDown() {
        driver.quit();
    }


    @Test
    public void testAllMessages() throws Exception {
        System.setProperty("com.bitbreeds.keystore", "./src/test/resources/ws2.jks");
        System.setProperty("com.bitbreeds.keystore.alias", "websocket");
        System.setProperty("com.bitbreeds.keystore.pass", "websocket");

        CamelContext ctx = SimpleSignalingExample.camelContextLossy(5,5);
        ctx.start();

        File fl = new File(".././web/transfer-loss.html");

        String url = "file://" + fl.getAbsolutePath();
        System.out.println(url);
        driver.get(url);

        (new WebDriverWait(driver, 30)).until(
                (ExpectedCondition<Boolean>) d -> {
                    assert d != null;
                    String data  = d.findElement(By.id("all-received")).getText();
                    logger.info("Data {}",data);
                    if(data.contains("echo-msg-")) {
                        String out = data.substring(9, data.length());
                        return Integer.valueOf(out) > 75;
                    }
                    else {
                        return false;
                    }
                }
        );

        ctx.stop();
    }






}

@@ -2054,16 +2054,24 @@
 Messages
+Finished
 () throw
@@ -2084,16 +2084,16 @@
 ption %7B%0A
-
         
@@ -2461,13 +2461,8 @@
 sfer
--loss
 .htm
@@ -2620,9 +2620,9 @@
 er, 
-3
+2
 0)).
@@ -2742,22 +2742,14 @@
     
-String data  =
+return
  d.f
@@ -2795,349 +2795,42 @@
 xt()
-;%0A                    logger.info(%22Data %7B%7D%22,data);%0A                    if(data.contains(%22echo-msg-%22)) %7B%0A                        String out = data.substring(9, data.length());%0A                        return Integer.valueOf(out) %3E 75;%0A                    %7D%0A                    else %7B%0A                        return false;%0A                    %7D
+.equalsIgnoreCase(%22ALL RECEIVED%22);
 %0A   
@@ -2876,22 +2876,22 @@
 stop();%0A
+
     %7D%0A%0A%0A
 %0A%0A%0A%0A%7D%0A
@@ -2884,14 +2884,10 @@
     %7D%0A%0A%0A
-%0A%0A%0A%0A
 %7D%0A

Add packet loss test for FF