package com.infoclinika.pdx.controller;

import com.infoclinika.pdx.domain.experiment.expression.ExpressionType;
import com.infoclinika.pdx.projection.GeneMappingResponse;
import com.infoclinika.pdx.service.FileParser;
import com.infoclinika.pdx.service.ParsedFile;
import com.infoclinika.pdx.service.ProcessedFile;
import com.infoclinika.pdx.service.exception.ApplicationException;
import com.infoclinika.pdx.service.experiment.ExpressionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Handles requests to upload expression data.
 */
@RestController
@RequestMapping("/api/upload")
public class ExpressionUploadController {
    private static final String FILE_PARAM = "file";
    private static final String TYPE_PARAM = "type";
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final FileParser fileParser;
    private final ExpressionService expressionService;

    /**
     * Constructor to provide required services.
     */
    @Autowired
    public ExpressionUploadController(FileParser fileParser, ExpressionService service) {
        this.fileParser = fileParser;
        this.expressionService = service;
    }

    /**
     * Handles upload of expression data file.
     *
     * @param type type of sample
     * @param file file to parse
     * @return Result of gene-synonyms mapping.
     */
    @PostMapping("/expression")
    @ResponseStatus(HttpStatus.OK)
    public GeneMappingResponse save(@RequestParam(TYPE_PARAM) ExpressionType type,
                                    @RequestParam(FILE_PARAM) MultipartFile file) throws ApplicationException {
        logger.debug("Request to /expression");

        final ParsedFile parsedFile = fileParser.parseFile(file);
        return expressionService.save(type, parsedFile);
    }

    /**
     * Handles upload of large expression data file.
     *
     * @param type type of sample
     * @param file file to parse
     * @return Result of gene-synonyms mapping.
     */
    @PostMapping("/expression/large")
    @ResponseStatus(HttpStatus.OK)
    public GeneMappingResponse saveBatch(@RequestParam(TYPE_PARAM) ExpressionType type,
                                         @RequestParam(FILE_PARAM) MultipartFile file) throws ApplicationException {
        logger.debug("Request to /expression/new");

        final ProcessedFile processedFile = fileParser.parseChunks(file);
        return expressionService.save(type, processedFile);
    }
}
