package com.njzscloud.common.sn.support;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.core.ienum.Dict;
import com.njzscloud.common.sn.contant.PadMode;
import com.njzscloud.common.sn.contant.SnSection;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.Map;

@Getter
@Setter
@ToString
@Accessors(chain = true)
public class IncSectionConfig implements SectionConfig {
    private final SnSection sectionName = SnSection.ZiZeng;
    private String code;
    private Integer step;
    private Integer initialVal;
    private PadMode padMode;
    private String padVal;
    private Integer padLen;

    @Override
    public ISnSection section() {
        return new IncSection(code, step, initialVal, padMode, padVal, padLen);
    }

    @Override
    public void resolve(Map<String, Object> config) {
        code = MapUtil.getStr(config, "code");
        step = MapUtil.getInt(config, "step");
        initialVal = MapUtil.getInt(config, "initialVal");
        padMode = Dict.parse(MapUtil.getStr(config, "padMode"), PadMode.values());
        padVal = MapUtil.getStr(config, "padVal");
        padLen = MapUtil.getInt(config, "padLen");
        Assert.notBlank(code, () -> Exceptions.clierr("递增编码配置错误，为设置字段 code"));
        Assert.notNull(step, () -> Exceptions.clierr("递增编码配置错误，为设置字段 step"));
        Assert.notNull(initialVal, () -> Exceptions.clierr("递增编码配置错误，为设置字段 initialVal"));
        if (padMode != null) {
            Assert.notBlank(padVal, () -> Exceptions.clierr("递增编码配置错误，为设置字段 padVal"));
            Assert.notNull(padLen, () -> Exceptions.clierr("递增编码配置错误，为设置字段 padLen"));
        }
    }
}
