package org.wikipedia.onboarding;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import org.wikipedia.R;
import org.wikipedia.WikipediaApp;
import org.wikipedia.analytics.LoginFunnel;
import org.wikipedia.analytics.OnboardingFunnel;
import org.wikipedia.login.LoginActivity;
import org.wikipedia.settings.PrefKeys;
import org.wikipedia.util.L10nUtils;

import java.util.Locale;

/**
 * Onboarding screen, which shows up the first time this app is started unless the user is already logged in.
 * From there you can create an account, login, or skip to the main activity.
 * It is assumed that this activity is started by the main activity,
 * so that when create account, login or skip are done we can just call finish.
 */
public class OnboardingActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(WikipediaApp.getInstance().getCurrentTheme());
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_onboarding);

        final OnboardingFunnel funnel = new OnboardingFunnel((WikipediaApp) getApplicationContext());

        // Only send the onboarding start log event if the activity is created for the first time
        if (savedInstanceState == null) {
            funnel.logStart();
        }

        ImageView wikipediaWordMarkImage = (ImageView) findViewById(R.id.onboarding_wp_wordmark_img);
        TextView wikipediaWordMarkText = (TextView) findViewById(R.id.onboarding_wp_wordmark_text_fallback);
        View createAccountButton = findViewById(R.id.onboarding_create_account_button);
        View loginButton = findViewById(R.id.onboarding_login_button);
        View skipLink = findViewById(R.id.onboarding_skip_link);

        if (L10nUtils.canLangUseImageForWikipediaWordmark(this)) {
            wikipediaWordMarkText.setVisibility(View.GONE);
        } else {
            wikipediaWordMarkImage.setVisibility(View.GONE);
            wikipediaWordMarkText.setText(Html.fromHtml(getString(R.string.wp_stylized)));
            if ("iw".equals(Locale.getDefault().getLanguage())) {
                final float dp = WikipediaApp.getInstance().getScreenDensity();
                final int padAdjust = 10;
                // move wordmark a bit to the right so it lines up better with the slogan
                wikipediaWordMarkText.setPadding((int) (padAdjust * dp), 0, 0, 0);
            }
        }

        createAccountButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                funnel.logCreateAccount();
                startLoginActivity(true); // launch login but go straight to create account activity
            }
        });

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                funnel.logLogin();
                startLoginActivity(false);
            }
        });

        skipLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                funnel.logSkip();
                done();
            }
        });
    }

    private void startLoginActivity(boolean createAccount) {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra(LoginActivity.LOGIN_REQUEST_SOURCE, LoginFunnel.SOURCE_ONBOARDING);
        intent.putExtra(LoginActivity.ACTION_CREATE_ACCOUNT, createAccount);
        startActivity(intent);
        done();
    }

    private void done() {
        markAllAboard();
        finish();
    }

    private void markAllAboard() {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.edit().putBoolean(PrefKeys.getOnboard(), true).apply();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("onboardingShowing", true);
    }
}

@@ -3255,16 +3255,53 @@
 (false);
+ // just take the user the login form
 %0A       
@@ -3513,41 +3513,231 @@
 e();
-%0A            %7D%0A        %7D);%0A    %7D%0A
+ // take the user directly out of the funnel%0A            %7D%0A        %7D);%0A    %7D%0A%0A    /**%0A     * Starts LoginActivity.%0A     * @param createAccount true if the account creation form should be shown first, false otherwise%0A     */
 %0A   
@@ -4088,79 +4088,297 @@
     
-private void done() %7B%0A        markAllAboard();%0A        finish();%0A    %7D%0A
+/**%0A     * Prepares the activity for finishing and ensuring onboarding is not shown again.%0A     */%0A    private void done() %7B%0A        markAllAboard();%0A        finish();%0A    %7D%0A%0A    /**%0A     * Adds a key to the SharedPreferences of the app to ensure that onboarding never shows again.%0A     */
 %0A   
@@ -4682,32 +4682,220 @@
 tate(outState);%0A
+        // Put item in bundle so start events are not fired purely because of activity recreation%0A        // This makes the event logging data more closely match user intent and behaviour%0A
         outState

add some documentation to OnboardingActivity