package theorycrafter.ui.fiteditor

import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertAny
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.filter
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.runDesktopComposeUiTest
import theorycrafter.TheorycrafterContext.eveData
import theorycrafter.TheorycrafterContext.fits
import theorycrafter.TheorycrafterTest
import theorycrafter.onSubtreeRootedAt
import theorycrafter.setApplicationContent
import kotlin.test.Test
import kotlin.test.assertTrue


/**
 * Tests remote effects
 */
@OptIn(ExperimentalTestApi::class)
class RemoteEffectsTest: TheorycrafterTest() {


    /**
     * Tests adding a hostile and a friendly remote module effect (in the same rack), and then removing them.
     */
    @Test
    fun addAndRemoveHostileAndFriendlyRemoteModuleEffect() = runDesktopComposeUiTest {
        val fit = newFit(shipName = "Caracal")

        setApplicationContent {
            FitEditor(fit)
        }

        val nosferatuType = eveData.moduleType("Medium Energy Nosferatu I")
        val capTransmitterType = eveData.moduleType("Medium Remote Capacitor Transmitter I")
        val nosferatu = fits.modifyAndSave {
            fit.addModuleEffect(nosferatuType)!!
        }
        val capTransmitter = fits.modifyAndSave {
            fit.addModuleEffect(capTransmitterType)!!
        }

        onSubtreeRootedAt(Nodes.FitEditor.hostileModuleEffectRow(0))
            .assertAny(hasText(nosferatuType.name))
        onSubtreeRootedAt(Nodes.FitEditor.friendlyModuleEffectRow(0))
            .assertAny(hasText(capTransmitterType.name))

        fits.modifyAndSave {
            fit.removeModuleEffect(nosferatu)
        }
        onSubtreeRootedAt(Nodes.FitEditor.hostileModuleEffectRow(0))
            .filter(hasText(nosferatuType.name))
            .assertCountEquals(0)

        fits.modifyAndSave {
            fit.removeModuleEffect(capTransmitter)
        }
        onSubtreeRootedAt(Nodes.FitEditor.friendlyModuleEffectRow(0))
            .filter(hasText(capTransmitterType.name))
            .assertCountEquals(0)

        assertTrue(fit.remoteEffects.allModule.isEmpty(), message = "Remote modules not cleared")
    }


}