package bou.amine.apps.readerforselfossv2.repository

import bou.amine.apps.readerforselfossv2.dao.ITEM
import bou.amine.apps.readerforselfossv2.dao.ReaderForSelfossDB
import bou.amine.apps.readerforselfossv2.dao.SOURCE
import bou.amine.apps.readerforselfossv2.dao.TAG
import bou.amine.apps.readerforselfossv2.model.SelfossModel
import bou.amine.apps.readerforselfossv2.rest.SelfossApi
import bou.amine.apps.readerforselfossv2.service.AppSettingsService
import bou.amine.apps.readerforselfossv2.utils.ItemType
import com.github.ln_12.library.ConnectivityStatus
import io.mockk.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.runBlocking
import kotlin.test.*

class RepositoryTest() {
    private val connectivityStatus = mockk<ConnectivityStatus>()
    private val db = mockk<ReaderForSelfossDB>()
    private val appSettingsService = mockk<AppSettingsService>()
    private val api = mockk<SelfossApi>()

    private val NUMBER_ARTICLES = 100
    private val NUMBER_UNREAD = 50
    private val NUMBER_STARRED = 20

    @BeforeTest
    fun setup() {
        clearAllMocks()
        every { appSettingsService.getApiVersion() } returns 4
        every { appSettingsService.getBaseUrl() } returns "https://test.com/selfoss/"
        every { appSettingsService.isItemCachingEnabled() } returns false
        every { appSettingsService.isUpdateSourcesEnabled() } returns true

        every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(true)

        coEvery { api.version() } returns SelfossModel.StatusAndData(success = true, data = SelfossModel.ApiVersion("2.19-ba1e8e3", "4.0.0"))
        coEvery { api.stats() } returns  SelfossModel.StatusAndData(success = true, data = SelfossModel.Stats(NUMBER_ARTICLES, NUMBER_UNREAD, NUMBER_STARRED))

        every { db.itemsQueries.items().executeAsList() } returns generateTestDBItems()
        every { db.tagsQueries.deleteAllTags() } returns Unit
        every { db.tagsQueries.transaction(any(), any()) } returns Unit
        every { db.tagsQueries.insertTag(any()) } returns Unit
    }

    @Test
    fun `Instantiate repository`() {
        val repository = Repository(api, appSettingsService, connectivityStatus, db)
    }

    @Test
    fun `Instantiate repository without api version`() {
        every { appSettingsService.getApiVersion() } returns -1
        every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
    }

    @Test
    fun `Instantiate repository with negative stats`() {
        coEvery { api.stats() } returns  SelfossModel.StatusAndData(success = true, data = SelfossModel.Stats(-100, -50, -20))

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
    }

    @Test
    fun `Get api 4 date with api 1 version stored`() {
        every { appSettingsService.getApiVersion() } returns 1
        coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.StatusAndData(success = true, data = generateTestApiItem())
        every { appSettingsService.updateApiVersion(any()) } returns Unit

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        runBlocking {
            repository.getNewerItems()
        }

        assertSame(repository.items.size, 1)
        verify(exactly = 1) { appSettingsService.updateApiVersion(4) }
    }

    @Test
    fun `Get api 1 date with api 4 version stored`() {
        every { appSettingsService.getApiVersion() } returns 4
        coEvery { api.version() } returns SelfossModel.StatusAndData(success = false, null)
        val itemParameters = FakeItemParameters()
        itemParameters.datetime = "2021-04-23 11:45:32"
        coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.StatusAndData(success = true, data = generateTestApiItem(itemParameters))

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        runBlocking {
            repository.getNewerItems()
        }

        assertSame(1, repository.items.size)
    }

    @Test
    fun `Get newer items`() {
        coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.StatusAndData(success = true, data = generateTestApiItem())

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        runBlocking {
        repository.getNewerItems()
        }

        assertSame(repository.items.size, 1)
        coVerify(exactly = 1) { api.getItems("unread", 0, null, null, null, null, any()) }
        verify(exactly = 0) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Get all newer items`() {
        coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.StatusAndData(success = true, data = generateTestApiItem())

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        repository.displayedItems = ItemType.ALL
        runBlocking {
            repository.getNewerItems()
        }

        assertSame(repository.items.size, 1)
        coVerify(exactly = 1) { api.getItems("all", 0, null, null, null, null, any()) }
        verify(exactly = 0) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Get newer starred items`() {
        coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.StatusAndData(success = true, data = generateTestApiItem())

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        repository.displayedItems = ItemType.STARRED
        runBlocking {
            repository.getNewerItems()
        }

        assertSame(repository.items.size, 1)
        coVerify(exactly = 1) { api.getItems("starred", 0, null, null, null, null, any()) }
        verify(exactly = 0) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Get newer items without connectivity`() {
        every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
        every { appSettingsService.isItemCachingEnabled() } returns true

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        runBlocking {
            repository.getNewerItems()
        }

        assertSame(repository.items.size, 1)
        coVerify(exactly = 0) { api.getItems("unread", 0, null, null, null, null, any()) }
        verify(atLeast = 1) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Get older items`() {
        coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.StatusAndData(success = true, data = generateTestApiItem())

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        repository.items = ArrayList(generateTestApiItem())
        runBlocking {
            repository.getOlderItems()
        }

        assertSame(repository.items.size, 2)
        coVerify(exactly = 1) { api.getItems("unread", 1, null, null, null, null, any()) }
        verify(exactly = 0) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Get all older items`() {
        coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.StatusAndData(success = true, data = generateTestApiItem())

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        repository.items = ArrayList(generateTestApiItem())
        repository.displayedItems = ItemType.ALL
        runBlocking {
            repository.getOlderItems()
        }

        assertSame(repository.items.size, 2)
        coVerify(exactly = 1) { api.getItems("all", 1, null, null, null, null, any()) }
        verify(exactly = 0) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Get older starred items`() {
        coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.StatusAndData(success = true, data = generateTestApiItem())

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        repository.displayedItems = ItemType.STARRED
        repository.items = ArrayList(generateTestApiItem())
        runBlocking {
            repository.getOlderItems()
        }

        assertSame(repository.items.size, 2)
        coVerify(exactly = 1) { api.getItems("starred", 1, null, null, null, null, any()) }
        verify(exactly = 0) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Reload badges`() {
        var success = false

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        runBlocking {
            success = repository.reloadBadges()
        }

        assertSame(true, success)
        assertSame(NUMBER_ARTICLES, repository.badgeAll)
        assertSame(NUMBER_UNREAD, repository.badgeUnread)
        assertSame(NUMBER_STARRED, repository.badgeStarred)
        coVerify(atLeast = 1) { api.stats() }
        verify(exactly = 0) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Reload badges without response`() {
        coEvery { api.stats() } returns SelfossModel.StatusAndData(success = false, data = null)

        var success = false

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        runBlocking {
            success = repository.reloadBadges()
        }

        assertSame(false, success)
        assertSame(0, repository.badgeAll)
        assertSame(0, repository.badgeUnread)
        assertSame(0, repository.badgeStarred)
        coVerify(atLeast = 1) { api.stats() }
        verify(exactly = 0) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Reload badges with items caching`() {
        every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
        every { appSettingsService.isItemCachingEnabled() } returns true

        val itemParameter = FakeItemParameters()
        itemParameter.starred = true
        itemParameter.unread = false

        var success = false

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        repository.items = ArrayList(generateTestApiItem(itemParameter))
        runBlocking {
            success = repository.reloadBadges()
        }

        assertSame(true, success)
        assertSame(1, repository.badgeAll)
        assertSame(0, repository.badgeUnread)
        assertSame(1, repository.badgeStarred)
        coVerify(exactly = 0) { api.stats() }
        verify(atLeast = 1) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Reload badges without items caching`() {
        every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
        every { appSettingsService.isItemCachingEnabled() } returns false

        val itemParameter = FakeItemParameters()
        itemParameter.starred = true
        itemParameter.unread = false

        var success = false

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        repository.items = ArrayList(generateTestApiItem(itemParameter))
        runBlocking {
            success = repository.reloadBadges()
        }

        assertSame(false, success)
        assertSame(0, repository.badgeAll)
        assertSame(0, repository.badgeUnread)
        assertSame(0, repository.badgeStarred)
        coVerify(exactly = 0) { api.stats() }
        verify(exactly = 0) { db.itemsQueries.items().executeAsList()}
    }

    @Test
    fun `Get tags`() {
        val tags = listOf(SelfossModel.Tag("test", "red", 6),
            SelfossModel.Tag("second", "yellow", 0))

        coEvery { api.tags() } returns SelfossModel.StatusAndData(success = true, data = tags)

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var testTags: List<SelfossModel.Tag>? = null
        runBlocking {
            testTags = repository.getTags()
        }

        assertSame(tags, testTags)
        verify(exactly = 0) { db.tagsQueries.tags().executeAsList() }
    }

    @Test
    fun `Get tags with sources update disabled`() {
        val tags = listOf(SelfossModel.Tag("test", "red", 6),
            SelfossModel.Tag("second", "yellow", 0))
        val tagsDB = listOf(TAG("test_DB", "red", 6),
            TAG("second_DB", "yellow", 0))

        coEvery { api.tags() } returns SelfossModel.StatusAndData(success = true, data = tags)
        coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB
        every { appSettingsService.isUpdateSourcesEnabled() } returns false

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var testTags: List<SelfossModel.Tag>? = null
        runBlocking {
            testTags = repository.getTags()
        }

        assertNotSame(tags, testTags)
        assertSame(tagsDB.first().name, testTags?.first()?.tag)
        coVerify(exactly = 0) { api.tags() }
        verify(atLeast = 1) { db.tagsQueries.tags().executeAsList() }
    }

    @Test
    fun `Get tags with sources update and db disabled`() {
        val tags = listOf(SelfossModel.Tag("test", "red", 6),
            SelfossModel.Tag("second", "yellow", 0))
        val tagsDB = listOf(TAG("test_DB", "red", 6),
            TAG("second_DB", "yellow", 0))

        coEvery { api.tags() } returns SelfossModel.StatusAndData(success = true, data = tags)
        coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB
        every { appSettingsService.isUpdateSourcesEnabled() } returns false
        every { appSettingsService.isItemCachingEnabled() } returns false

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var testTags: List<SelfossModel.Tag>? = null
        runBlocking {
            testTags = repository.getTags()
        }

        assertSame(null, testTags)
        coVerify(exactly = 0) { api.tags() }
        verify(exactly = 0) { db.tagsQueries.tags().executeAsList() }
    }

    @Test
    fun `Get tags without connection`() {
        val tags = listOf(SelfossModel.Tag("test", "red", 6),
            SelfossModel.Tag("second", "yellow", 0))
        val tagsDB = listOf(TAG("test_DB", "red", 6),
            TAG("second_DB", "yellow", 0))

        coEvery { api.tags() } returns SelfossModel.StatusAndData(success = true, data = tags)
        coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB
        every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var testTags: List<SelfossModel.Tag>? = null
        runBlocking {
            testTags = repository.getTags()
        }

        assertNotSame(tags, testTags)
        assertSame(tagsDB.first().name, testTags?.first()?.tag)
        coVerify(exactly = 0) { api.tags() }
        verify(atLeast = 1) { db.tagsQueries.tags().executeAsList() }
    }

    @Test
    fun `Get tags without connection and items caching disabled`() {
        val tags = listOf(SelfossModel.Tag("test", "red", 6),
            SelfossModel.Tag("second", "yellow", 0))
        val tagsDB = listOf(TAG("test_DB", "red", 6),
            TAG("second_DB", "yellow", 0))

        coEvery { api.tags() } returns SelfossModel.StatusAndData(success = true, data = tags)
        coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB
        every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
        every { appSettingsService.isItemCachingEnabled() } returns false

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var testTags: List<SelfossModel.Tag>? = null
        runBlocking {
            testTags = repository.getTags()
        }

        assertSame(null, testTags)
        coVerify(exactly = 0) { api.tags() }
        verify(exactly = 0) { db.tagsQueries.tags().executeAsList() }
    }

    @Test
    fun `get sources`() {
        val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),
            SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png"))

        coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var testSources: List<SelfossModel.Source>? = null
        runBlocking {
            testSources = repository.getSources()
        }

        assertSame(sources, testSources)
        coVerify(exactly = 1) { api.sources() }
        verify(exactly = 0) { db.sourcesQueries.sources().executeAsList() }
    }

    @Test
    fun `get sources without connection`() {
        val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),
            SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png"))
        val sourcesDB = listOf<SOURCE>(SOURCE("1", "First source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),
            SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png"))

        every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
        coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
        every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var testSources: List<SelfossModel.Source>? = null
        runBlocking {
            testSources = repository.getSources()
        }

        assertContentEquals(sources, testSources)
        coVerify(exactly = 0) { api.sources() }
        verify(atLeast = 1) { db.sourcesQueries.sources().executeAsList() }
    }

    @Test
    fun `get sources without connection and items caching disabled`() {
        val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),
            SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png"))
        val sourcesDB = listOf<SOURCE>(SOURCE("1", "First source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),
            SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png"))

        every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
        every { appSettingsService.isItemCachingEnabled() } returns false
        coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
        every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var testSources: List<SelfossModel.Source>? = null
        runBlocking {
            testSources = repository.getSources()
        }

        assertSame(null, testSources)
        coVerify(exactly = 0) { api.sources() }
        verify(exactly = 0) { db.sourcesQueries.sources().executeAsList() }
    }

    @Test
    fun `get sources with sources update disabled`() {
        val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),
            SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png"))
        val sourcesDB = listOf<SOURCE>(SOURCE("1", "First source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),
            SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png"))

        every { appSettingsService.isUpdateSourcesEnabled() } returns false
        coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
        every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var testSources: List<SelfossModel.Source>? = null
        runBlocking {
            testSources = repository.getSources()
        }

        assertContentEquals(sources, testSources)
        coVerify(exactly = 0) { api.sources() }
        verify(atLeast = 1) { db.sourcesQueries.sources().executeAsList() }
    }

    @Test
    fun `get sources with sources update and items caching disabled`() {
        val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),
            SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png"))
        val sourcesDB = listOf<SOURCE>(SOURCE("1", "First source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),
            SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png"))

        every { appSettingsService.isUpdateSourcesEnabled() } returns false
        every { appSettingsService.isItemCachingEnabled() } returns false
        coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
        every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var testSources: List<SelfossModel.Source>? = null
        runBlocking {
            testSources = repository.getSources()
        }

        assertSame(null, testSources)
        coVerify(exactly = 0) { api.sources() }
        verify(exactly = 0) { db.sourcesQueries.sources().executeAsList() }
    }

    @Test
    fun `create source`() {
        coEvery { api.createSourceForVersion(any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.SuccessResponse(true)

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var response = false
        runBlocking {
            response = repository.createSource("test", "https://test.com/feed", "spouts\\rss\\fulltextrss", "Test, New", "")
        }

        coVerify(exactly = 1) { api.createSourceForVersion(any(), any(), any(), any(), any(), any()) }
        assertSame(true, response)
    }

    @Test
    fun `create source but response fails`() {
        coEvery { api.createSourceForVersion(any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.SuccessResponse(false)

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var response = false
        runBlocking {
            response = repository.createSource("test", "https://test.com/feed", "spouts\\rss\\fulltextrss", "Test, New", "")
        }

        coVerify(exactly = 1) { api.createSourceForVersion(any(), any(), any(), any(), any(), any()) }
        assertSame(false, response)
    }

    @Test
    fun `create source without connection`() {
        coEvery { api.createSourceForVersion(any(), any(), any(), any(), any(), any()) } returns
                SelfossModel.SuccessResponse(true)
        every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)

        val repository = Repository(api, appSettingsService, connectivityStatus, db)
        var response = false
        runBlocking {
            response = repository.createSource("test", "https://test.com/feed", "spouts\\rss\\fulltextrss", "Test, New", "")
        }

        coVerify(exactly = 0) { api.createSourceForVersion(any(), any(), any(), any(), any(), any()) }
        assertSame(false, response)
    }
}

fun generateTestDBItems(item : FakeItemParameters = FakeItemParameters()) : List<ITEM> {
    return listOf(ITEM(
    id = item.id,
    datetime = item.datetime,
    title = item.title,
    content = item.content,
    unread = item.unread,
    starred = item.starred,
    thumbnail = item.thumbnail,
    icon = item.icon,
    link = item.link,
    sourcetitle = item.sourcetitle,
    tags = item.tags
))}

fun generateTestApiItem(item : FakeItemParameters = FakeItemParameters()) : List<SelfossModel.Item> {
    return listOf(
        SelfossModel.Item(
            id = item.id.toInt(),
            datetime = item.datetime,
            title = item.title,
            content = item.content,
            unread = item.unread,
            starred = item.starred,
            thumbnail = item.thumbnail,
            icon = item.icon,
            link = item.link,
            sourcetitle = item.sourcetitle,
            tags = item.tags.split(',')
        )
    )
}

class FakeItemParameters() {
    val id = "20"
    var datetime = "2022-09-09T03:32:01-04:00"
    val title = "Etica della ricerca sotto i riflettori."
    val content = "<p><strong>Luigi Campanella, già Presidente SCI</strong></p>\n<p>L’etica della scienza è di certo ambito di cui continuiamo a scoprire nuovi aspetti e risvolti.</p>\n<p>L’ultimo è quello delle intelligenze artificiali capaci di creare opere complesse basate su immagini e parole memorizzate con il rischio di fake news e di contenuti disturbanti.</p>\n<p>Per evitare che ciò accada si sta procedendo filtrando secondo criteri di autocensura i dati da cui l’intelligenza artificiale parte.</p>\n<p>Comincia ad intravedersi un futuro prossimo di competizione fra autori umani ed artificiali nel quale sarà importante, quando i loro prodotti saranno indistinguibili, dichiararne l’origine.</p>\n<p>Come si comprende, si conferma che gli aspetti etici dell’innovazione e della ricerca si diversificato sempre di più.</p>\n<p>La biologia molecolare e la genetica già in passato hanno posto all’attenzione comune aspetti di etica della scienza che hanno indotto a nuove riflessioni circa i limiti delle ricerche.</p>\n<p>L’argomento, sempre attuale, torna sulle prime pagine a seguito della pubblicazione di una ricerca della Università di Cambridge che ha sviluppato una struttura cellulare di un topo con un cuore che batte regolarmente.</p>\n<img src=\"https://ilblogdellasci.files.wordpress.com/2022/09/image002-1.png?w=481\" alt=\"\" width=\"697\" height=\"430\" /><img src=\"https://ilblogdellasci.files.wordpress.com/2022/09/image003-1.png?w=906\" alt=\"\" /><p>Magdalena Zernicka-Goetz</p>\n<img src=\"https://ilblogdellasci.files.wordpress.com/2022/09/image004.jpg?w=474\" alt=\"\" width=\"622\" height=\"465\" /><p>Gianluca Amadei</p>\n<p>Del gruppo fa parte anche uno scienziato italiano Gianluca Amadei,che dinnanzi alle obiezioni di natura etica sulla realizzazione della vita artificiale si è affrettato a sostenere che non è creare nuove vite il fine primario della ricerca, ma quello di salvare quelle esistenti, di dare contributi essenziali alla medicina citando il caso del fallimento tuttora non interpretato di alcune gravidanze e di superare la sperimentazione animale, così contribuendo positivamente alla soluzione di un altro dilemma etico.</p>\n<p>L’embrione sintetico ha ovviamente come primo traguardo il contributo ai trapianti oggi drammaticamente carenti nell’offerta rispetto alla domanda, con attese fino a 4 anni per i trapianti di cuore ed a 2 anni per quelli di fegato. Il lavoro dovrebbe adesso continuare presso l’Ateneo di Padova per creare nuovi organi e nuovi farmaci.</p>"
    var unread = true
    var starred = true
    val thumbnail = null
    val icon = "ba79e238383ce83c23a169929c8906ef.png"
    val link = "https://ilblogdellasci.wordpress.com/2022/09/09/etica-della-ricerca-sotto-i-riflettori/"
    val sourcetitle = "La Chimica e la Società"
    val tags = "Chimica, Testing"
}