package bou.amine.apps.readerforselfossv2.android

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isChecked
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isEnabled
import androidx.test.espresso.matcher.ViewMatchers.isNotEnabled
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.CoreMatchers.not
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4::class)
@LargeTest
class SettingsActivityOfflineTest {

    @get:Rule
    val activityRule = ActivityScenarioRule(LoginActivity::class.java)

    lateinit var context: Context

    @Before
    fun init() {
        activityRule.scenario.onActivity { activity ->
            context = activity.window.context
        }
        loginAndInitHome()
        openActionBarOverflowOrOptionsMenu(
            ApplicationProvider.getApplicationContext()
        )
        onView(withText(R.string.title_activity_settings)).perform(click())
        onView(withText(R.string.pref_header_offline)).perform(click())
    }

    @Test
    fun testOffline() {
        onView(withSettingsCheckboxWidget(R.string.pref_switch_periodic_refresh)).check(
            matches(
                allOf(
                    isDisplayed(), not(isChecked())
                )
            )
        )
        onView(withSettingsCheckboxWidget(R.string.pref_switch_items_caching)).check(
            matches(
                allOf(
                    isDisplayed(), not(isChecked())
                )
            )
        )
        onView(withSettingsCheckboxFrame(R.string.pref_switch_items_caching)).check(
            matches(
                isEnabled()
            )
        )
        onView(withText(R.string.pref_periodic_refresh_minutes_title)).check(
            matches(
                allOf(isNotEnabled(), isDisplayed())
            )
        )

        onView(withSettingsCheckboxWidget(R.string.pref_switch_refresh_when_charging)).check(
            matches(
                allOf(
                    isDisplayed(), not(isChecked())
                )
            )
        )
        onView(withSettingsCheckboxFrame(R.string.pref_switch_refresh_when_charging)).check(
            matches(
                isNotEnabled()
            )
        )
        onView(withSettingsCheckboxWidget(R.string.pref_switch_notify_new_items)).check(
            matches(
                allOf(
                    isDisplayed(), not(isChecked())
                )
            )
        )
        onView(withSettingsCheckboxFrame(R.string.pref_switch_notify_new_items)).check(
            matches(
                isNotEnabled()
            )
        )
        onView(withSettingsCheckboxWidget(R.string.pref_switch_update_sources)).check(
            matches(
                allOf(
                    isDisplayed(), isChecked()
                )
            )
        )
    }

    @Test
    fun testOfflineActions() {
        onView(withText(R.string.pref_switch_items_caching_off)).check(matches(isDisplayed()))
        onView(withText(R.string.pref_switch_items_caching)).perform(click())
        onView(withText(R.string.pref_switch_items_caching_on)).check(matches(isDisplayed()))
        onView(withSettingsCheckboxFrame(R.string.pref_switch_items_caching)).check(
            matches(
                isEnabled()
            )
        )
        onView(withText(R.string.pref_periodic_refresh_minutes_title)).check(
            matches(
                isNotEnabled()
            )
        )
        onView(withSettingsCheckboxFrame(R.string.pref_switch_refresh_when_charging)).check(
            matches(
                isNotEnabled()
            )
        )
        onView(withSettingsCheckboxFrame(R.string.pref_switch_notify_new_items)).check(
            matches(
                isNotEnabled()
            )
        )

        onView(withText(R.string.pref_switch_periodic_refresh_off)).check(
            matches(
                isDisplayed()
            )
        )
        onView(withSettingsCheckboxWidget(R.string.pref_switch_periodic_refresh)).perform(click())
        onView(withText(R.string.pref_switch_periodic_refresh_on)).check(
            matches(
                isDisplayed()
            )
        )
        onView(withSettingsCheckboxFrame(R.string.pref_periodic_refresh_minutes_title)).check(
            matches(
                isEnabled()
            )
        )
        onView(withSettingsCheckboxFrame(R.string.pref_switch_refresh_when_charging)).check(
            matches(
                isEnabled()
            )
        )
        onView(withSettingsCheckboxFrame(R.string.pref_switch_notify_new_items)).check(
            matches(
                isEnabled()
            )
        )
        changeAndCancelSetting("360", "123") {
            onView(withText(R.string.pref_periodic_refresh_minutes_title)).perform(click())
        }
        changeAndSaveSetting("360", "123") {
            onView(withText(R.string.pref_periodic_refresh_minutes_title)).perform(click())
        }
        onView(withSettingsCheckboxFrame(R.string.pref_switch_refresh_when_charging)).perform(click())
        onView(withSettingsCheckboxFrame(R.string.pref_switch_notify_new_items)).perform(click())
        onView(withSettingsCheckboxWidget(R.string.pref_switch_update_sources)).perform(click())
    }
}