package fr.free.nrw.commons;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.anything;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.assertion.ViewAssertions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;

import fr.free.nrw.commons.settings.SettingsActivity;

import java.util.Map;

import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class SettingsActivityTest {
    private SharedPreferences prefs;
    private Map<String,?> prefValues;

    @Rule
    public ActivityTestRule<SettingsActivity> activityRule =
            new ActivityTestRule<SettingsActivity>(SettingsActivity.class,
                    false /* Initial touch mode */, true /*  launch activity */) {

                @Override
                protected void afterActivityLaunched() {
                    // save preferences
                    prefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
                    prefValues = prefs.getAll();
                }

                @Override
                protected void afterActivityFinished() {
                    // restore preferences
                    SharedPreferences.Editor editor = prefs.edit();
                    for (Map.Entry<String,?> entry: prefValues.entrySet()) {
                        String key = entry.getKey();
                        Object val = entry.getValue();
                        if (val instanceof String) {
                            editor.putString(key, (String)val);
                        } else if (val instanceof Boolean) {
                            editor.putBoolean(key, (Boolean)val);
                        } else if (val instanceof Integer) {
                            editor.putInt(key, (Integer)val);
                        } else {
                            throw new RuntimeException("type not implemented: " + entry);
                        }
                    }
                    editor.apply();
                }
            };

    @Test
    public void oneLicenseIsChecked() {
        // click "License" (the first item)
        Espresso.onData(anything())
                .inAdapterView(findPreferenceList())
                .atPosition(0)
                .perform(ViewActions.click());

        // test the selected item
        Espresso.onView(ViewMatchers.isChecked())
                .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    }

    @Test
    public void afterClickingCcby4ItWillStay() {
        // click "License" (the first item)
        Espresso.onData(anything())
                .inAdapterView(findPreferenceList())
                .atPosition(0)
                .perform(ViewActions.click());

        // click "CC BY-4.0"
        Espresso.onView(
                ViewMatchers.withText(R.string.license_name_cc_by_4_0)
        ).perform(ViewActions.click());

        // click "License" (the first item)
        Espresso.onData(anything())
                .inAdapterView(findPreferenceList())
                .atPosition(0)
                .perform(ViewActions.click());

        // test the value remains "CC BY-4.0"
        Espresso.onView(ViewMatchers.isChecked())
                .check(ViewAssertions.matches(
                        ViewMatchers.withText(R.string.license_name_cc_by_4_0)
                ));
    }

    private static Matcher<View> findPreferenceList() {
        return allOf(
                ViewMatchers.isDescendantOfA(ViewMatchers.withId(android.R.id.content)),
                ViewMatchers.withId(android.R.id.list),
                ViewMatchers.hasFocus()
        );
    }
}

@@ -3205,22 +3205,28 @@
  click %22
-CC BY-
+Attribution 
 4.0%22%0A   
@@ -3305,35 +3305,36 @@
 ense_name_cc_by_
-4_0
+four
 )%0A        ).perf
@@ -3611,14 +3611,20 @@
 ns %22
-CC BY-
+Attribution 
 4.0%22
@@ -3799,11 +3799,12 @@
 _by_
-4_0
+four
 )%0A  

Update SettingsActivityTest to correctly pass test on LicenseSpinner