package eu.darken.sdmse.systemcleaner.core.filter.stock

import androidx.compose.ui.graphics.vector.ImageVector
import dagger.Binds
import dagger.Module
import dagger.Reusable
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dagger.multibindings.IntoSet
import eu.darken.sdmse.systemcleaner.R
import eu.darken.sdmse.common.areas.DataArea
import eu.darken.sdmse.common.ca.CaString
import eu.darken.sdmse.common.ca.toCaString
import eu.darken.sdmse.common.compose.icons.OsMac
import eu.darken.sdmse.common.compose.icons.SdmIcons
import eu.darken.sdmse.common.datastore.value
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
import eu.darken.sdmse.common.files.APathLookup
import eu.darken.sdmse.common.files.GatewaySwitch
import eu.darken.sdmse.common.files.segs
import eu.darken.sdmse.common.sieve.NameCriterium
import eu.darken.sdmse.common.sieve.SegmentCriterium
import eu.darken.sdmse.common.sieve.TypeCriterium
import eu.darken.sdmse.systemcleaner.core.SystemCleanerSettings
import eu.darken.sdmse.systemcleaner.core.filter.BaseSystemCleanerFilter
import eu.darken.sdmse.systemcleaner.core.filter.SystemCleanerFilter
import eu.darken.sdmse.systemcleaner.core.filter.toDeletion
import eu.darken.sdmse.systemcleaner.core.sieve.SystemCrawlerSieve
import javax.inject.Inject
import javax.inject.Provider

class MacFilesFilter @Inject constructor(
    private val sieveFactory: SystemCrawlerSieve.Factory,
    private val gatewaySwitch: GatewaySwitch,
) : BaseSystemCleanerFilter() {

    override val icon: ImageVector = SdmIcons.OsMac

    override suspend fun getLabel(): CaString = R.string.systemcleaner_filter_macfiles_label.toCaString()

    override suspend fun getDescription(): CaString = R.string.systemcleaner_filter_macfiles_summary.toCaString()

    override suspend fun targetAreas(): Set<DataArea.Type> = setOf(
        DataArea.Type.SDCARD,
        DataArea.Type.PUBLIC_MEDIA,
        DataArea.Type.PORTABLE,
    )

    private lateinit var fileSieve: SystemCrawlerSieve
    private lateinit var directorySieve: SystemCrawlerSieve

    override suspend fun initialize() {
        val fileConfig = SystemCrawlerSieve.Config(
            targetTypes = setOf(TypeCriterium.FILE),
            areaTypes = targetAreas(),
            nameCriteria = setOf(
                NameCriterium("._", mode = NameCriterium.Mode.Start()),
                NameCriterium(".DS_Store", mode = NameCriterium.Mode.Equal()),
            ),
        )
        val directoryConfig = SystemCrawlerSieve.Config(
            targetTypes = setOf(TypeCriterium.DIRECTORY),
            areaTypes = setOf(
                DataArea.Type.SDCARD,
                DataArea.Type.PORTABLE,
            ),
            pfpCriteria = setOf(
                SegmentCriterium(segs(".Trashes"), mode = SegmentCriterium.Mode.Equal()),
                SegmentCriterium(segs(".spotlight"), mode = SegmentCriterium.Mode.Equal()),
                SegmentCriterium(segs(".Spotlight-V100"), mode = SegmentCriterium.Mode.Equal()),
                SegmentCriterium(segs(".fseventsd"), mode = SegmentCriterium.Mode.Equal()),
                SegmentCriterium(segs(".TemporaryItems"), mode = SegmentCriterium.Mode.Equal()),
            ),
        )
        fileSieve = sieveFactory.create(fileConfig)
        directorySieve = sieveFactory.create(directoryConfig)
        log(TAG) { "initialized() with $fileConfig and $directoryConfig" }
    }


    override suspend fun match(item: APathLookup<*>): SystemCleanerFilter.Match? {
        return fileSieve.match(item).toDeletion() ?: directorySieve.match(item).toDeletion()
    }

    override suspend fun process(
        matches: Collection<SystemCleanerFilter.Match>
    ): Collection<SystemCleanerFilter.Processed> {
        return matches.filterIsInstance<SystemCleanerFilter.Match.Deletion>().deleteAll(gatewaySwitch)
    }

    override fun toString(): String = "${this::class.simpleName}(${hashCode()})"

    @Reusable
    class Factory @Inject constructor(
        private val settings: SystemCleanerSettings,
        private val filterProvider: Provider<MacFilesFilter>
    ) : SystemCleanerFilter.Factory {
        override suspend fun isEnabled(): Boolean = settings.filterMacFilesEnabled.value()
        override suspend fun create(): SystemCleanerFilter = filterProvider.get()
    }

    @InstallIn(SingletonComponent::class)
    @Module
    abstract class DIM {
        @Binds @IntoSet abstract fun mod(mod: Factory): SystemCleanerFilter.Factory
    }

    companion object {
        private val TAG = logTag("SystemCleaner", "Filter", "MacFiles")
    }
}
