package com.aip.gateway.api.service.impl;

import com.aip.gateway.api.aip.model.SetFileInfo;
import com.aip.gateway.api.aip.utils.AipFileUtils;
import com.aip.gateway.api.model.app.CheckAip;
import com.aip.gateway.api.model.request.RequestStream;
import com.aip.gateway.api.model.request.RequestStreamAllSet;
import com.aip.gateway.api.model.request.RequestStreamDel;
import com.aip.gateway.api.model.request.RequestStreamSet;
import com.aip.gateway.api.model.response.ResponseInfo;
import com.aip.gateway.api.model.response.ResponseStream;
import com.aip.gateway.api.repository.AipFileRepository;
import com.aip.gateway.api.repository.AppRepository;
import com.aip.gateway.api.service.IApiStreamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Base64;

@Slf4j
@Service
public class ApiStreamService extends AbstractAipService implements IApiStreamService {

    public ApiStreamService(AppRepository repo, AipFileRepository aipFile) {
        super(repo, aipFile);
    }

    @Override
    public ResponseInfo getInfo(RequestStream req) {
        try {
            String tempOrgFileName = getRequestFileName(req.getApiGuid() + AipFileUtils.getExtension(req.getDispFileName()));
            String actualFileName = getActualFileName(req.getApiGuid() + AipFileUtils.getExtension(req.getDispFileName()));
            String newFileExt = getSupportedFileType(req.getDispFileName());
            if (newFileExt == null) {
                return new ResponseInfo(102, "지원하지 않는 파일형식 입니다.");
            }

            byte[] fileDataArr = Base64.getDecoder().decode(req.getFileData());
            if (!AipFileUtils.streamToFile(fileDataArr, tempOrgFileName)) {
                return new ResponseInfo(110, "작업 중에 파일 시스템 오류가 발생했습니다.");
            }
            try {
                return responseGetFileInfo(this.aipFile.getManager().getFileInfo(tempOrgFileName, actualFileName), req.getDispFileName());
            }
            finally {
                AipFileUtils.delete(tempOrgFileName);
            }
            //AipFileStream fileStream = new AipFileStream(req.getFileData());
            //return responseGetFileInfo(this.aipFile.getManager().getFileInfo(fileStream, actualFileName), req.getDispFileName());
        }
        catch (Exception e) {
            return responseInfoException(e);
        }
    }

    @Override
    public ResponseStream setLabel(RequestStreamSet req) {
        CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), "", true);
        if (check.getErrorCode() != 0) {
            return new ResponseStream(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
        }
        try {
            byte[] fileDataArr = Base64.getDecoder().decode(req.getFileData());
            if (!AipFileUtils.streamToFile(fileDataArr, check.getReqFileName())) {
                return responseStreamFail(req.getDispFileName());
            }

            try {
                SetFileInfo result = this.aipFile.getManager().setLabel(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getAipGuid(), "", req.getComment());
                return responseSetStream(result, req.getDispFileName(), check.getActualFileName());
            }
            finally {
                AipFileUtils.delete(check.getReqFileName());
                AipFileUtils.delete(check.getActualFileName());
            }
        }
        catch (Exception e) {
            return responseStreamException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseStream delLabel(RequestStreamDel req) {
        CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), "", true);
        if (check.getErrorCode() != 0) {
            return new ResponseStream(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
        }
        try {
            byte[] fileDataArr = Base64.getDecoder().decode(req.getFileData());
            if (!AipFileUtils.streamToFile(fileDataArr, check.getReqFileName())) {
                return responseStreamFail(req.getDispFileName());
            }

            try {
                SetFileInfo result = this.aipFile.getManager().deleteLabel(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getComment(), false);
                return responseSetStream(result, req.getDispFileName(), check.getActualFileName());
            }
            finally {
                AipFileUtils.delete(check.getReqFileName());
                AipFileUtils.delete(check.getActualFileName());
            }
        }
        catch (Exception e) {
            return responseStreamException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseStream setProtection(RequestStreamSet req) {
        CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), "", true);
        if (check.getErrorCode() != 0) {
            return new ResponseStream(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
        }
        try {
            byte[] fileDataArr = Base64.getDecoder().decode(req.getFileData());
            if (!AipFileUtils.streamToFile(fileDataArr, check.getReqFileName())) {
                return responseStreamFail(req.getDispFileName());
            }

            try {
                SetFileInfo result = this.aipFile.getManager().setProtection(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getAipGuid(), req.getComment());
                return responseSetStream(result, req.getDispFileName(), check.getActualFileName());
            }
            finally {
                AipFileUtils.delete(check.getReqFileName());
                AipFileUtils.delete(check.getActualFileName());
            }
        }
        catch (Exception e) {
            return responseStreamException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseStream delProtection(RequestStreamDel req) {
        CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), "", true);
        if (check.getErrorCode() != 0) {
            return new ResponseStream(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
        }
        try {
            byte[] fileDataArr = Base64.getDecoder().decode(req.getFileData());
            if (!AipFileUtils.streamToFile(fileDataArr, check.getReqFileName())) {
                return responseStreamFail(req.getDispFileName());
            }

            try {
                SetFileInfo result = this.aipFile.getManager().deleteProtection(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getComment());
                return responseSetStream(result, req.getDispFileName(), check.getActualFileName());
            }
            finally {
                AipFileUtils.delete(check.getReqFileName());
                AipFileUtils.delete(check.getActualFileName());
            }
        }
        catch (Exception e) {
            return responseStreamException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseStream setLabelProtection(RequestStreamAllSet req) {
        CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), "", true);
        if (check.getErrorCode() != 0) {
            return new ResponseStream(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
        }
        try {
            byte[] fileDataArr = Base64.getDecoder().decode(req.getFileData());
            if (!AipFileUtils.streamToFile(fileDataArr, check.getReqFileName())) {
                return responseStreamFail(req.getDispFileName());
            }

            try {
                SetFileInfo result = this.aipFile.getManager().setLabel(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getLabelGuid(), req.getTemplateGuid(), req.getComment());
                return responseSetStream(result, req.getDispFileName(), check.getActualFileName());
            }
            finally {
                AipFileUtils.delete(check.getReqFileName());
                AipFileUtils.delete(check.getActualFileName());
            }
        }
        catch (Exception e) {
            return responseStreamException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseStream delLabelProtection(RequestStreamDel req) {
        CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), "", true);
        if (check.getErrorCode() != 0) {
            return new ResponseStream(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
        }
        try {
            byte[] fileDataArr = Base64.getDecoder().decode(req.getFileData());
            if (!AipFileUtils.streamToFile(fileDataArr, check.getReqFileName())) {
                return responseStreamFail(req.getDispFileName());
            }

            try {
                SetFileInfo result = this.aipFile.getManager().deleteLabel(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getComment(), true);
                return responseSetStream(result, req.getDispFileName(), check.getActualFileName());
            }
            finally {
                AipFileUtils.delete(check.getReqFileName());
                AipFileUtils.delete(check.getActualFileName());
            }
        }
        catch (Exception e) {
            return responseStreamException(e, req.getDispFileName());
        }
    }
}
