content: package logic;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  
  import org.openqa.selenium.By;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.firefox.FirefoxBinary;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.openqa.selenium.firefox.FirefoxProfile;
  import org.openqa.selenium.support.ui.ExpectedConditions;
  import org.openqa.selenium.support.ui.WebDriverWait;
  
  import core.DataCache;
  import core.TwitterData;
  import core.UINamesData;
  import randomfactory.HTTPRequestFactory;
  
  public class TwitterTask {
    public static final String GECKO_DRIVER_PATH = "PATH_TO_GECKODRIVER_(geckodriver.exe)";
    public static String DEFAULT_USER_PASSWORD = "wantedpassword9999";
    public static final String TOR_ROOT_PATH = "PATH_TO_TOR_BROWSER_FOLDER";
    public static final String TOR_EXE_PATH = TOR_ROOT_PATH + "Tor Browser\\Browser\\firefox.exe";
    public static final String TOR_PROFILE_PATH = TOR_ROOT_PATH + "Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
  
    public static void perform() {
      System.setProperty("webdriver.gecko.driver", GECKO_DRIVER_PATH);
  
      // Tor Browser configuration
      File torProfileDir = new File(TOR_PROFILE_PATH);
      FirefoxBinary binary = new FirefoxBinary(new File(TOR_EXE_PATH));
      FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
      torProfile.setPreference("webdriver.load.strategy", "unstable");
      //
  
      int randomNum = 0 + (int)(Math.random() * UINamesData.QUERIES_BY_REGION.size());
      DataCache.newUserCreated(HTTPRequestFactory.performRequestInBackground(UINamesData.QUERIES_BY_REGION.get(randomNum)));
  
      try {
        binary.startProfile(torProfile, torProfileDir, "");
      } catch (IOException e) {
        e.printStackTrace();
      }
  
      FirefoxProfile profile = new FirefoxProfile();
      profile.setPreference("network.proxy.type", 1);
      profile.setPreference("network.proxy.socks", "127.0.0.1");
      profile.setPreference("network.proxy.socks_port", 9150);
  
      FirefoxDriver driver = new FirefoxDriver(profile);
      System.setProperty("webdriver.gecko.driver", GECKO_DRIVER_PATH);
      driver.get(TwitterData.TWITTER_REG_URL);
  
      WebDriverWait wait = new WebDriverWait(driver, 10);
      WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("full-name")));
  
      driver.findElement(By.id("full-name")).sendKeys(DataCache.lastResponseObject.getName() + " " + DataCache.lastResponseObject.getSurname());
      driver.findElement(By.id("email")).sendKeys(DataCache.lastResponseObject.getEmail());
      try {
        Thread.sleep(5000);
      } catch (InterruptedException e1) {
        e1.printStackTrace();
      }
      driver.findElement(By.id("password")).sendKeys(DEFAULT_USER_PASSWORD);
  
      driver.findElement(By.id("submit_button")).click();
  
      try {
        Thread.sleep(5000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
  
      driver.get(TwitterData.TWITTER_STEP2_REG_URL);
  
      WebElement element2 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
  
      randomNum = 1 + (int)(Math.random() * 50);
      driver.findElement(By.id("username")).sendKeys(DataCache.lastResponseObject.getName().toLowerCase() + DataCache.lastResponseObject.getSurname().toLowerCase() + randomNum);
  
      driver.findElement(By.id("submit_button")).click();
  
      try {
        Thread.sleep(5000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
  
      driver.get(TwitterData.TWITTER_BASE_URL + TwitterData.TWITTER_USER_TO_FOLLOW);
  
      WebElement element3 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(@class, 'follow-button')]/li[1]/a")));
  
      driver.findElement(By.xpath("//li[contains(@class, 'follow-button')]/li[1]/a")).click();
  
      try {
        Thread.sleep(5000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
  
      //Tidy up now
      killBrowser();
    }
    
    private static void killBrowser() {
      Runtime rt = Runtime.getRuntime();
  
      try {
        rt.exec("taskkill /F /IM firefox.exe");
        while (processIsRunning("firefox.exe")) {
          Thread.sleep(100);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  
    private static boolean processIsRunning(String process) {
      boolean processIsRunning = false;
      String line;
      try {
        Process proc = Runtime.getRuntime().exec("wmic.exe");
        BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
        oStream.write("process where name='" + process + "'");
        oStream.flush();
        oStream.close();
        while ((line = input.readLine()) != null) {
          if (line.toLowerCase().contains("caption")) {
            processIsRunning = true;
            break;
          }
        }
        input.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return processIsRunning;
    }
  }
  