package com.crm.manager.service.impl.base;

import com.crm.manager.service.SysConfigService;
import com.crm.rely.backend.configuration.LocaleMessage;
import com.crm.rely.backend.core.constant.ConfigConstants;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.pojo.table.SysConfigTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.service.impl.base.BaseUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

public class BaseUploadServiceImpl extends BaseUpload {
    @Autowired
    private LocaleMessage localeMessage;

    @Autowired
    private SysConfigService sysConfigService;

    public void validated(MultipartFile file) throws ServiceException {
        if (file == null) {
            throw ServiceException.exception(localeMessage.getMessage(Constants.UPLOAD_FILE_NOT_NULL,
                    Constants.UPLOAD_FILE_NOT_NULL));
        }
        List<String> codes = new ArrayList<>(2);
        codes.add(ConfigConstants.UPLOAD_SIZE_MAX);
        codes.add(ConfigConstants.UPLOAD_REGEX);
        List<SysConfigTable> sysConfigTables = sysConfigService.getByCodes(codes);
        if (sysConfigTables == null || sysConfigTables.size() != 2) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        int size = 0;
        String regex = null;
        for (SysConfigTable sysConfigTable : sysConfigTables) {
            if (ConfigConstants.UPLOAD_SIZE_MAX.equals(sysConfigTable.getCode())) {
                try {
                    size = Integer.parseInt(sysConfigTable.getValue());
                } catch (NumberFormatException e) {
                    throw new ServiceException(Constants.SYSTEM_ERROR);
                }
            } else if (ConfigConstants.UPLOAD_REGEX.equals(sysConfigTable.getCode())) {
                regex = sysConfigTable.getValue();
            }
        }

        validated(file, size, regex);
    }

    public void validatedVideo(MultipartFile file) throws ServiceException {
        if (file == null) {
            throw ServiceException.exception(localeMessage.getMessage(Constants.UPLOAD_FILE_NOT_NULL, Constants.UPLOAD_FILE_NOT_NULL));
        }
        List<String> codes = new ArrayList<>(2);
        codes.add(ConfigConstants.UPLOAD_VIDEO_SIZE_MAX);
        codes.add(ConfigConstants.UPLOAD_VIDEO_REGEX);
        List<SysConfigTable> sysConfigTables = sysConfigService.getByCodes(codes);
        if (sysConfigTables == null || sysConfigTables.size() != 2) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        int size = 0;
        String regex = null;
        for (SysConfigTable sysConfigTable : sysConfigTables) {
            if (ConfigConstants.UPLOAD_VIDEO_SIZE_MAX.equals(sysConfigTable.getCode())) {
                try {
                    size = Integer.parseInt(sysConfigTable.getValue());
                } catch (NumberFormatException e) {
                    throw new ServiceException(Constants.SYSTEM_ERROR);
                }
            } else if (ConfigConstants.UPLOAD_VIDEO_REGEX.equals(sysConfigTable.getCode())) {
                regex = sysConfigTable.getValue();
            }
        }

        validated(file, size, regex);
    }
}
