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.app.CheckMultipartFile;
import com.aip.gateway.api.model.request.*;
import com.aip.gateway.api.model.response.ResponseFile;
import com.aip.gateway.api.model.response.ResponseInfo;
import com.aip.gateway.api.repository.AipFileRepository;
import com.aip.gateway.api.repository.AppRepository;
import com.aip.gateway.api.service.IApiFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
public class ApiFileService extends AbstractAipService implements IApiFileService {

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

    @Override
    public ResponseInfo getInfo(RequestFile req) {
        try {
            String reqFileName = getRequestFileName(req.getRealFileName());
            if (!AipFileUtils.isExists(reqFileName)) {
                return new ResponseInfo(101, "파일이 존재하지 않습니다.");
            }
            String newFileExt = getSupportedFileType(reqFileName);
            if (newFileExt == null) {
                return new ResponseInfo(102, "지원하지 않는 파일형식 입니다.");
            }
            return responseGetFileInfo(this.aipFile.getManager().getFileInfo(reqFileName), req.getDispFileName());
        }
        catch (Exception e) {
            return responseInfoException(e);
        }
    }

    @Override
    public ResponseFile setLabel(RequestFileSet req) {
        try {
            CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), req.getRealFileName(), false);
            if (check.getErrorCode() != 0) {
                return new ResponseFile(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
            }
            SetFileInfo result = this.aipFile.getManager().setLabel(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getAipGuid(), "", req.getComment());
            return responseSetFile(result, req.getDispFileName(), check.getActualFileName());
        }
        catch (Exception e) {
            return responseFileException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseFile delLabel(RequestFileDel req) {
        try {
            CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), req.getRealFileName(), false);
            if (check.getErrorCode() != 0) {
                return new ResponseFile(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
            }
            SetFileInfo result = this.aipFile.getManager().deleteLabel(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getComment(), false);
            return responseSetFile(result, req.getDispFileName(), check.getActualFileName());
        }
        catch (Exception e) {
            return responseFileException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseFile setProtection(RequestFileSet req) {
        try {
            CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), req.getRealFileName(), false);
            if (check.getErrorCode() != 0) {
                return new ResponseFile(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
            }
            SetFileInfo result = this.aipFile.getManager().setProtection(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getAipGuid(), req.getComment());
            return responseSetFile(result, req.getDispFileName(), check.getActualFileName());
        }
        catch (Exception e) {
            return responseFileException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseFile delProtection(RequestFileDel req) {
        try {
            CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), req.getRealFileName(), false);
            if (check.getErrorCode() != 0) {
                return new ResponseFile(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
            }
            SetFileInfo result = this.aipFile.getManager().deleteProtection(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getComment());
            return responseSetFile(result, req.getDispFileName(), check.getActualFileName());
        }
        catch (Exception e) {
            return responseFileException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseFile setLabelProtection(RequestFileAllSet req) {
        try {
            CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), req.getRealFileName(), false);
            if (check.getErrorCode() != 0) {
                return new ResponseFile(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
            }
            SetFileInfo result = this.aipFile.getManager().setLabel(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getLabelGuid(), req.getTemplateGuid(), req.getComment());
            return responseSetFile(result, req.getDispFileName(), check.getActualFileName());
        }
        catch (Exception e) {
            return responseFileException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseFile delLabelProtection(RequestFileDel req) {
        try {
            CheckAip check = checkAipRequest(req.getApiGuid(), req.getDispFileName(), req.getRealFileName(), false);
            if (check.getErrorCode() != 0) {
                return new ResponseFile(check.getErrorCode(), check.getErrorMessage(), check.getDispFileName());
            }
            SetFileInfo result = this.aipFile.getManager().deleteLabel(check.getReqFileName(), check.getActualFileName(), req.getEmail(), req.getComment(), true);
            return responseSetFile(result, req.getDispFileName(), check.getActualFileName());
        }
        catch (Exception e) {
            return responseFileException(e, req.getDispFileName());
        }
    }

    @Override
    public ResponseFile encryptFile(MultipartFile file, RequestBase baseReq) {
        CheckMultipartFile result = checkMultipartFile(file);
        if (result.getErrorCode() != 0) {
            return new ResponseFile(result.getErrorCode(), result.getErrorMessage(), result.getDispFileName());
        }

        RequestFileSet req = new RequestFileSet(baseReq.getApiKey(), baseReq.getEmail(), baseReq.getApiGuid(), "",
                result.getDispFileName(), result.getSaveFileName(), this.repo.getSettings().getSetLabelId(), "SetEncryptFile");
        return setLabel(req);
    }

    @Override
    public ResponseFile decryptFile(MultipartFile file, RequestBase baseReq) {
        CheckMultipartFile result = checkMultipartFile(file);
        if (result.getErrorCode() != 0) {
            return new ResponseFile(result.getErrorCode(), result.getErrorMessage(), result.getDispFileName());
        }

        RequestFileDel req = new RequestFileDel(baseReq.getApiKey(), baseReq.getEmail(), baseReq.getApiGuid(), "",
                result.getDispFileName(), result.getSaveFileName());
        req.setComment("DelDecryptFile");
        return delLabel(req);
    }
}
