package com.yuchen.saas.park.api.entity.door.config;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yuchen.saas.park.api.commons.domain.DoorAccessControlConfigEntry;
import com.yuchen.saas.park.api.commons.domain.DoorAccessControlConfigIssueTaskContext;
import com.yuchen.saas.park.api.commons.domain.DoorAccessControlConfigTarget;
import com.yuchen.saas.park.api.commons.entity.SimpleEntity;
import com.yuchen.saas.park.api.commons.enums.DoorAccessControlConfigType;
import com.yuchen.saas.park.api.config.convert.DoorAccessControlConfigTypeConverter;
import lombok.Data;

import javax.annotation.Nullable;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;

@TableName(autoResultMap = true)
@Data
public class DoorAccessControlConfigIssueTask extends SimpleEntity {
    @Positive
    private long userId;
    @Positive
    private long doorAccessControlId;

    @TableField(typeHandler = DoorAccessControlConfigTypeConverter.class)
    private DoorAccessControlConfigType type;

    /**
     * 配置以值为准时 = 0
     */
    @PositiveOrZero
    private long configId;

    /**
     * 配置值
     */
    @Nullable
    private String configValue;

    /**
     * 删除视为禁用
     */
    @TableField("is_disabled")
    private boolean disabled;

    @TableField("is_processed")
    private boolean processed;

    /**
     * 缓存结果,当配置未改变时可复用
     */
    @TableField("is_successful")
    private boolean successful;

    public static DoorAccessControlConfigIssueTask from(DoorAccessControlConfigIssueTaskContext o) {
        return new DoorAccessControlConfigIssueTask().setContext(o);
    }

    public boolean isEnabled() {
        return !this.disabled;
    }

    public DoorAccessControlConfigIssueTaskContext getContext() {
        return DoorAccessControlConfigIssueTaskContext.of(this.getTarget(), this.getEntry(), configValue, disabled);
    }

    public DoorAccessControlConfigTarget getTarget() {
        return DoorAccessControlConfigTarget.of(userId, doorAccessControlId);
    }

    public DoorAccessControlConfigIssueTask setTarget(DoorAccessControlConfigTarget o) {
        return this.setUserId(o.getUserId()).setDoorAccessControlId(o.getDoorAccessControlId());
    }

    public DoorAccessControlConfigEntry getEntry() {
        return DoorAccessControlConfigEntry.of(type, configId);
    }

    public DoorAccessControlConfigIssueTask setEntry(DoorAccessControlConfigEntry o) {
        return this.setType(o.getType()).setConfigId(o.getConfigId());
    }

    public DoorAccessControlConfigIssueTask setContext(DoorAccessControlConfigIssueTaskContext o) {
        return this.setTarget(o.getTarget()).setEntry(o.getEntry()).setConfigValue(o.getConfigValue()).setDisabled(o.isDisabled());
    }
}
