package fr.free.nrw.commons.auth;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import fr.free.nrw.commons.R;

public class SignupActivity extends Activity {

    private boolean otherPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("SignupActivity", "Signup Activity started");
        otherPage = false;

        WebView webView = new WebView(this);
        setContentView(webView);

        webView.setWebViewClient(new MyWebViewClient());
        //myWebView.loadUrl("https://commons.wikimedia.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page");
        //Mobile page, looks better than the above
        webView.loadUrl("https://commons.m.wikimedia.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page&returntoquery=welcome%3Dyes");

        //After Create Account button is pressed within WebView, it brings user to https://commons.m.wikimedia.org/w/index.php?title=Main_Page&welcome=yes. So can we just override that URL?
        //Do we NEED to enable JS? Validation seems to work fine here
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.equals("https://commons.m.wikimedia.org/w/index.php?title=Main_Page&welcome=yes")) {
                //Signup success, so load LoginActivity again
                Log.d("SignupActivity", "Overriding URL" + url);
                Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                intent.putExtra("Redirected", true);
                startActivity(intent);
                return true;
            } else {
                //If user clicks any other links in the webview
                Log.d("SignupActivity", "Not overriding URL, URL is: " + url);
                otherPage = true;
                return false;
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (otherPage == true) {
            //If we are in any page except the main signup page, back button should take us back to signup page
            Intent intent = new Intent(this, SignupActivity.class);
            startActivity(intent);
        }
        else {
            //If we are in signup page, back button should take us back to LoginActivity
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            intent.putExtra("Redirected", true);
            startActivity(intent);
        }
    }
}

@@ -206,16 +206,51 @@
 sponse;%0A
+import android.webkit.WebSettings;%0A
 import a
@@ -272,16 +272,16 @@
 ebView;%0A
-
 import a
@@ -772,579 +772,378 @@
     
-//myWebView.loadUrl(%22https://commons.wikimedia.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page%22);%0A        //Mobile page, looks better than the above%0A        webView.loadUrl(%22https://commons.m.wikimedia.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page&returntoquery=welcome%253Dyes%22);%0A%0A        //After Create Account button is pressed within WebView, it brings user to https://commons.m.wikimedia.org/w/index.php?title=Main_Page&welcome=yes. So can we just override that URL?%0A        //Do we NEED to enable JS? Validation seems to work fine here
+WebSettings webSettings = webView.getSettings();%0A        //Needed to refresh Captcha. Might introduce XSS vulnerabilities, but we can trust Wikimedia's site... right?%0A        webSettings.setJavaScriptEnabled(true);%0A        %0A        webView.loadUrl(%22https://commons.m.wikimedia.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page&returntoquery=welcome%253Dyes%22);
 %0A   

Enable JS for captcha