/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package com.runicgateway.app.ui.admin

import com.runicgateway.app.R
import com.runicgateway.app.data.api.fake.FakeAdminApi
import com.runicgateway.app.data.repository.AdminRepository
import com.runicgateway.app.util.MainDispatcherRule
import com.runicgateway.app.util.errorUnit
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test

class AdminModerationViewModelTest {

    @get:Rule val mainDispatcher = MainDispatcherRule()

    private val api = FakeAdminApi()
    private fun viewModel() = AdminModerationViewModel(AdminRepository(api))

    @Test fun kickWithNoTargetShowsTargetRequired() {
        val vm = viewModel()
        vm.kick(account = "", serial = "")
        assertEquals(R.string.admin_mod_target_required, vm.state.value.feedback!!.messageRes)
    }

    @Test fun banForwardsFieldsAndSucceeds() {
        val vm = viewModel()
        vm.ban(account = "gwen", serial = "", durationSec = 3600, reason = "afk")
        assertEquals("gwen", api.lastBan!!.account)
        assertNull(api.lastBan!!.serial) // blank -> null
        assertEquals(3600L, api.lastBan!!.durationSec)
        assertEquals("afk", api.lastBan!!.reason)
        val fb = vm.state.value.feedback!!
        assertTrue(fb.ok)
        assertEquals(R.string.admin_mod_banned, fb.messageRes)
        assertFalse(vm.state.value.busy)
    }

    @Test fun shardOfflineMapsTo503Copy() {
        api.unitResponse = errorUnit(503)
        val vm = viewModel()
        vm.kick(account = "x", serial = "")
        assertEquals(R.string.admin_mod_shard_offline, vm.state.value.feedback!!.messageRes)
    }

    @Test fun broadcastRejectsBlankText() {
        val vm = viewModel()
        vm.broadcast(text = "  ", hue = null)
        assertEquals(R.string.admin_mod_text_required, vm.state.value.feedback!!.messageRes)
    }

    @Test fun unbanForbiddenMapsTo403Copy() {
        api.unitResponse = errorUnit(403)
        val vm = viewModel()
        vm.unban("gwen")
        assertEquals(R.string.admin_forbidden, vm.state.value.feedback!!.messageRes)
    }
}
