package com.njzscloud.common.sn.support;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.njzscloud.common.core.ex.Exceptions;
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 TimeSectionConfig implements SectionConfig {
    private final SnSection sectionName = SnSection.ShiJian;
    private String pattern;
    private Boolean timestamp;
    private Integer unit;

    @Override
    public ISnSection section() {
        return new TimeSection(pattern, timestamp, unit);
    }

    @Override
    public void resolve(Map<String, Object> config) {
        pattern = MapUtil.getStr(config, "pattern");
        timestamp = MapUtil.getBool(config, "timestamp", false);
        unit = MapUtil.getInt(config, "unit");
        if (timestamp) {
            Assert.notNull(unit, () -> Exceptions.clierr("时间编码配置错误，为设置字段 unit"));
            Assert.isTrue(StrUtil.isBlank(pattern), () -> Exceptions.clierr("时间编码配置错误，为设置字段 pattern 与 timestamp 不能同时使用"));

        } else {
            Assert.notBlank(pattern, () -> Exceptions.clierr("时间编码配置错误，为设置字段 pattern"));
            Assert.isTrue(unit == null, () -> Exceptions.clierr("时间编码配置错误，为设置字段 unit 与 pattern 不能同时使用"));

        }
    }
}
