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 {

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

        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 {
                Log.d("SignupActivity", "Not overriding URL, URL is: " + url);
                return false;
            }

        }

    }
}

@@ -354,16 +354,48 @@
 vity %7B%0A%0A
+    private boolean otherPage;%0A%0A
     @Ove
@@ -560,16 +560,43 @@
 arted%22);
+%0A        otherPage = false;
 %0A%0A      
@@ -1921,16 +1921,24 @@
        %7D
+%0A       
  else %7B%0A
@@ -2008,32 +2008,66 @@
 L is: %22 + url);%0A
+                otherPage = true;%0A
                 
@@ -2090,25 +2090,24 @@
           %7D%0A
-%0A
         %7D%0A%0A 
@@ -2100,21 +2100,416 @@
 %7D%0A        %7D%0A
+    %7D%0A%0A    @Override%0A    public void onBackPressed() %7B%0A        if (otherPage == true) %7B%0A            Intent intent = new Intent(this, SignupActivity.class);%0A            startActivity(intent);%0A        %7D%0A        else %7B%0A            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);%0A            intent.putExtra(%22Redirected%22, true);%0A            startActivity(intent);%0A        %7D
 %0A    %7D%0A%7D%0A

Handle back button presses