package io.github.shirainbown.sharelink;

import static run.halo.app.extension.index.IndexAttributeFactory.simpleAttribute;

import org.springframework.stereotype.Component;
import run.halo.app.extension.Scheme;
import run.halo.app.extension.SchemeManager;
import run.halo.app.extension.index.IndexSpec;
import run.halo.app.plugin.BasePlugin;
import run.halo.app.plugin.PluginContext;
import io.github.shirainbown.sharelink.model.DownloadRecord;
import io.github.shirainbown.sharelink.model.DownloadResource;
import io.github.shirainbown.sharelink.model.LinkVisitRecord;
import io.github.shirainbown.sharelink.model.ProtectedLink;
import io.github.shirainbown.sharelink.model.VerifiedDownloader;

@Component
public class SharelinkPlugin extends BasePlugin {

    private final SchemeManager schemeManager;

    public SharelinkPlugin(PluginContext pluginContext, SchemeManager schemeManager) {
        super(pluginContext);
        this.schemeManager = schemeManager;
    }

    @Override
    public void start() {
        schemeManager.register(DownloadResource.class, indexSpecs -> indexSpecs.add(
            new IndexSpec()
                .setName("spec.slug")
                .setUnique(true)
                .setIndexFunc(simpleAttribute(DownloadResource.class,
                    resource -> resource.getSpec() == null ? null : resource.getSpec().getSlug()))));
        schemeManager.register(DownloadRecord.class, indexSpecs -> {
            indexSpecs.add(new IndexSpec()
                .setName("spec.resourceSlug")
                .setIndexFunc(simpleAttribute(DownloadRecord.class,
                    record -> record.getSpec() == null ? null : record.getSpec().getResourceSlug())));
            indexSpecs.add(new IndexSpec()
                .setName("spec.downloadedAt")
                .setIndexFunc(simpleAttribute(DownloadRecord.class,
                    record -> record.getSpec() == null || record.getSpec().getDownloadedAt() == null
                        ? null : record.getSpec().getDownloadedAt().toString())));
        });
        schemeManager.register(VerifiedDownloader.class);
        schemeManager.register(ProtectedLink.class, indexSpecs -> indexSpecs.add(
            new IndexSpec()
                .setName("spec.slug")
                .setUnique(true)
                .setIndexFunc(simpleAttribute(ProtectedLink.class,
                    link -> link.getSpec() == null ? null : link.getSpec().getSlug()))));
        schemeManager.register(LinkVisitRecord.class, indexSpecs -> {
            indexSpecs.add(new IndexSpec()
                .setName("spec.linkSlug")
                .setIndexFunc(simpleAttribute(LinkVisitRecord.class,
                    record -> record.getSpec() == null ? null
                        : record.getSpec().getLinkSlug())));
            indexSpecs.add(new IndexSpec()
                .setName("spec.visitedAt")
                .setIndexFunc(simpleAttribute(LinkVisitRecord.class,
                    record -> record.getSpec() == null
                        || record.getSpec().getVisitedAt() == null
                        ? null : record.getSpec().getVisitedAt().toString())));
        });
    }

    @Override
    public void stop() {
        schemeManager.unregister(Scheme.buildFromType(DownloadResource.class));
        schemeManager.unregister(Scheme.buildFromType(DownloadRecord.class));
        schemeManager.unregister(Scheme.buildFromType(VerifiedDownloader.class));
        schemeManager.unregister(Scheme.buildFromType(ProtectedLink.class));
        schemeManager.unregister(Scheme.buildFromType(LinkVisitRecord.class));
    }
}
