package dev.dinauer.maven.maven.core.release;

import dev.dinauer.maven.maven.core.*;
import dev.dinauer.maven.maven.core.context.ReleaseContext;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.apache.commons.lang3.NotImplementedException;

@ApplicationScoped
public class ReleaseService
{
    @Inject
    ReleaseJarService releaseJarService;

    @Inject
    ReleasePomService releasePomService;

    @Transactional
    public void upload(ReleaseContext releaseContext, byte[] body)
    {
        switch (releaseContext.extensions().ext())
        {
            case JAR ->
            {
                if (releaseContext.extensions().hashExt().isEmpty())
                {
                    releaseJarService.store(releaseContext, body);
                }
            }
            case POM ->
            {
                if (releaseContext.extensions().hashExt().isEmpty())
                {
                    releasePomService.store(releaseContext, body);
                }
            }
        }
    }

    public Response serve(ReleaseContext releaseContext)
    {
        switch (releaseContext.extensions().ext())
        {
            case JAR ->
            {
                if (releaseContext.extensions().hashExt().isEmpty())
                {
                    return Response.status(200).type(MediaType.APPLICATION_OCTET_STREAM).entity(releaseJarService.find(releaseContext)).build();
                }
                else
                {
                    return Response.status(200).type(MediaType.TEXT_PLAIN).entity(releaseJarService.findHash(releaseContext)).build();
                }
            }
            case POM ->
            {
                if (releaseContext.extensions().hashExt().isEmpty())
                {
                    return Response.status(200).type(MediaType.APPLICATION_XML).entity(releasePomService.find(releaseContext)).build();
                }
                else
                {
                    return Response.status(200).type(MediaType.TEXT_PLAIN).entity(releasePomService.findHash(releaseContext)).build();
                }
            }
        }
        throw new BadRequestException();
    }
}
