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");

        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.loadUrl("javascript:" +"var x = document.getElementById('email').value = 'blah';");
                view.evaluateJavascript("document.getElementById('Email').value = 'blah';",null);

//                view.loadUrl("javascript:" +"var x = document.getElementById('email').value = '" + "blah" + "';");
//                view.loadUrl("javascript:var y = document.getElementById('pass').value = " + password + ";");
            }
        });

    }

    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;

    }

}

@@ -1026,16 +1026,18 @@
 l(url);%0A
+//
         
@@ -1159,16 +1159,68 @@
 ifier%22);
+%0A        webview.loadUrl(%22http://www.facebook.com%22);
 %0A%0A      
@@ -1777,26 +1777,24 @@
 password);%0A%0A
-//
             
@@ -1806,40 +1806,28 @@
 iew.
-loadUrl(%22javascript:%22 +%22var x = 
+evaluateJavascript(%22
 docu
@@ -1833,36 +1833,39 @@
 ument.getElement
+s
 By
-Id
+Name
 ('email').value 
@@ -1849,32 +1849,35 @@
 sByName('email')
+%5B0%5D
 .value = 'blah';
@@ -1877,16 +1877,21 @@
 'blah';%22
+,null
 );%0A     
@@ -1949,21 +1949,26 @@
 ment
+s
 By
-Id('Email')
+Name('pass')%5B0%5D
 .val
@@ -1965,36 +1965,41 @@
 s')%5B0%5D.value = '
-blah
+111111111
 ';%22,null);%0A%0A//  
@@ -2021,40 +2021,28 @@
 iew.
-loadUrl(%22javascript:%22 +%22var x = 
+evaluateJavascript(%22
 docu
@@ -2083,140 +2083,24 @@
  = '
-%22 + %22blah%22 + %22';%22);%0A//                view.loadUrl(%22javascript:var y = document.getElementById('pass').value = %22 + password + %22;%22
+blah';%22,null
 );%0A
+%0A
     

Implement automatic password insertion in web view (Facebook only)