package com.zcloud.personnel.positioning.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.personnel.positioning.persistence.dataobject.FenceDO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@AllArgsConstructor
public class FencePersistenceSupport {
    public static final String SCOPE_COMPANY_AREA = "COMPANY_AREA";
    public static final String SCOPE_ELECTRONIC = "ELECTRONIC";
    public static final String SCOPE_WORK_AREA = "WORK_AREA";
    public static final String SCOPE_SILENCE = "SILENCE";
    public static final String DEFAULT_COOR = "WGS84";
    public static final String DEFAULT_SPACE_TYPE = "OUTDOOR";
    public static final String DEFAULT_BIZ_NAME = "\u533a\u57df";
    public static final String SILENCE_BIZ_NAME = "\u9759\u9ed8\u533a";

    private final FencePointPersistenceService fencePointPersistenceService;

    public void normalize(FenceDO fence, boolean create) {
        if (fence == null) {
            throw new BizException("Fence is required");
        }
        if (!StringUtils.hasText(fence.getFenceName())) {
            throw new BizException("Fence name is required");
        }
        if (!StringUtils.hasText(fence.getPolygonJson())) {
            throw new BizException("Fence polygonJson is required");
        }
        if (!StringUtils.hasText(fence.getFenceCode())) {
            fence.setFenceCode((create ? "F" : "FU") + System.currentTimeMillis());
        }
        if (!StringUtils.hasText(fence.getFenceScope())) {
            fence.setFenceScope(SCOPE_WORK_AREA);
        }
        if (fence.getSilence() == null) {
            fence.setSilence(SCOPE_SILENCE.equals(fence.getFenceScope()) ? 1 : 0);
        }
        if (!StringUtils.hasText(fence.getFenceBizName())) {
            fence.setFenceBizName(fence.getSilence() != null && fence.getSilence() == 1
                    ? SILENCE_BIZ_NAME
                    : DEFAULT_BIZ_NAME);
        }
        if (!StringUtils.hasText(fence.getCoor())) {
            fence.setCoor(DEFAULT_COOR);
        }
        if (!StringUtils.hasText(fence.getSpaceType())) {
            fence.setSpaceType(DEFAULT_SPACE_TYPE);
        }
        fence.setStatus(normalizeStatus(fence.getStatus()));
        if (!StringUtils.hasText(fence.getCorpinfoName()) && StringUtils.hasText(fence.getOrgName())) {
            fence.setCorpinfoName(fence.getOrgName());
        }
        if (!StringUtils.hasText(fence.getOrgName()) && StringUtils.hasText(fence.getCorpinfoName())) {
            fence.setOrgName(fence.getCorpinfoName());
        }
        fencePointPersistenceService.fillCenter(fence);
    }

    public String normalizeStatus(String status) {
        if (!StringUtils.hasText(status)) {
            return AlarmRuleConfigPersistence.STATUS_ENABLED;
        }
        String value = status.trim();
        if ("1".equals(value) || "\u542f\u7528".equals(value)) {
            return AlarmRuleConfigPersistence.STATUS_ENABLED;
        }
        if ("0".equals(value) || "\u505c\u7528".equals(value)) {
            return AlarmRuleConfigPersistence.STATUS_DISABLED;
        }
        return value;
    }
}
