package me.soundlocker.soundlocker;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.ArrayList;

/**
 * Created by SamW on 11/2/2015.
 */
public class WebViewer extends Activity{
    private ArrayList<String> webEndings;
    private String password = "";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webEndings = new ArrayList<String>();
        populateWebEndings(webEndings);
        Intent intent = getIntent();
        String website = intent.getStringExtra("website");
        password = intent.getStringExtra("password");

        String url = makeUrl(website);
        WebView webview = new WebView(this);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);

//        webview.loadUrl(url);
//        webview.loadUrl("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier");
        webview.loadUrl("http://www.facebook.com");

        setContentView(webview);

        //prevents certain sites from defaulting to browser
        webview.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                System.out.println("TESTING");
                System.out.println("TESTING");
                System.out.println("TESTING");
                System.out.println("TESTING");
                System.out.println("TESTING");
                System.out.println("TESTING");
                System.out.println(password);

                view.evaluateJavascript("document.getElementsByName('email')[0].value = 'blah';",null);
                view.evaluateJavascript("document.getElementsByName('pass')[0].value = '111111111';",null);

//                view.evaluateJavascript("document.getElementById('email').value = 'blah';",null);

            }
        });

    }

    private void populateWebEndings(ArrayList<String> aL) {
        aL.add(".org");
        aL.add(".com");
        aL.add(".net");
        aL.add(".gov");
        aL.add(".edu");
    }

    private String makeUrl (String siteName){
        if (!siteName.contains("http://")){
            siteName = "http://" + siteName;
        }



        for(int i=0; i<webEndings.size(); i++){
            if(siteName.contains(webEndings.get(i))){
                return siteName;
            }
        }
        String appendedWebSite = siteName+".com";
        return appendedWebSite;

    }

}

@@ -253,49 +253,8 @@
 t;%0A%0A
-/**%0A * Created by SamW on 11/2/2015.%0A */%0A
 publ
@@ -957,174 +957,8 @@
 );%0A%0A
-//        webview.loadUrl(url);%0A//        webview.loadUrl(%22https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier%22);%0A
     
@@ -1249,337 +1249,8 @@
 ) %7B%0A
-                System.out.println(%22TESTING%22);%0A                System.out.println(%22TESTING%22);%0A                System.out.println(%22TESTING%22);%0A                System.out.println(%22TESTING%22);%0A                System.out.println(%22TESTING%22);%0A                System.out.println(%22TESTING%22);%0A                System.out.println(password);%0A%0A
     
@@ -1314,21 +1314,20 @@
 ByName('
-email
+pass
 ')%5B0%5D.va
@@ -1337,221 +1337,20 @@
  = '
-blah';%22,null);%0A                view.evaluateJavascript(%22document.getElementsByName('pass')%5B0%5D.value = '111111111';%22,null);%0A%0A//                view.evaluateJavascript(%22document.getElementById('email').value = 'blah
+%22+password+%22
 ';%22,
@@ -1356,17 +1356,16 @@
 ,null);%0A
-%0A
         

Remove unneeded code and insert generated PW to Facebook