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

import com.aip.gateway.api.aip.model.AipFileInfo;
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.aip.utils.AipFileUtils;
import com.aip.gateway.api.utils.TimeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
@RequiredArgsConstructor
abstract class AbstractAipService {

    protected final AppRepository repo;
    protected final AipFileRepository aipFile;

    protected String getRequestFileName(String realFileName) {
        return this.repo.getSettings().getSourceFileDir() + realFileName;
    }
    protected String getActualFileName(String fileName) {
        return this.repo.getSettings().getTargetFileDir() + fileName;
    }

    protected String getSupportedFileType(String reqFileName) {
        return this.repo.getSupportedFileType(reqFileName);
    }

    protected ResponseInfo responseGetFileInfo(AipFileInfo fileInfo, String displayFileName) {
        boolean isLabled = false;
        boolean isProtected = false;
        Date creationTime = TimeUtils.getCurrentDate();
        String contentId  = "";
        String owner = "";
        String labelId  = "";
        String labelName  = "";
        String templateId  = "";
        String templateName  = "";
        long fileSize = 0;
        if (fileInfo != null) {
            fileSize = fileInfo.getFileSize();
            if (fileInfo.getContent() != null) {
                creationTime = fileInfo.getContent().getCreationTime();
            }
            if (fileInfo.getLabel() != null) {
                isLabled = true;
                labelId = fileInfo.getLabel().getId();
                labelName = fileInfo.getLabel().getName();
            }
            if (fileInfo.getProtection() != null) {
                owner = fileInfo.getProtection().getOwner();
                contentId = fileInfo.getProtection().getContentId();
                if (fileInfo.getProtection().getProtectionDescriptor() != null) {
                    isProtected = true;
                    templateId = fileInfo.getProtection().getProtectionDescriptor().getTemplateId();
                    templateName = fileInfo.getProtection().getProtectionDescriptor().getName();
                }
            }
        }
        ResponseInfo result = new ResponseInfo(0, "success");
        result.setLabeled(isLabled);
        result.setProtected(isProtected);
        result.setCreationTime(creationTime);
        result.setContentId(contentId);
        result.setOwner(owner);
        result.setLabelId(labelId);
        result.setLabelName(labelName);
        result.setTemplateId(templateId);
        result.setTemplateName(templateName);
        result.setDispFileName(displayFileName);
        result.setFileLabelGuid(labelId);
        result.setFileProtectionGuid(templateId);
        result.setFileOwner(owner);
        result.setFileExt(AipFileUtils.getExtension(displayFileName));
        result.setFileSize(fileSize);
        return result;
    }
}
