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

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yuchen.saas.park.api.commons.domain.DoorAccessControlConfigSetting;
import com.yuchen.saas.park.api.commons.domain.DoorAccessControlConfigSettingOne;
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.DoorAccessControlConfigState;
import com.yuchen.saas.park.api.config.convert.DoorAccessControlConfigStateConverter;
import lombok.Data;

import javax.annotation.Nullable;
import javax.validation.constraints.Positive;
import java.time.LocalDateTime;

import static com.yuchen.saas.park.api.commons.enums.DoorAccessControlConfigState.INIT;

/**
 * 时间配置(源)明细
 * 同一{@link DoorAccessControlConfigTarget} + {@link #timeConfigId}可能存在多份配置
 *
 * @see DoorAccessControlConfigSetting 此处冗余,可通过{@link #doorAccessControlMappingId} -> {@link DoorAccessControlConfigMapping}获取
 */
@TableName(autoResultMap = true)
@Data
public class DoorAccessControlTimeConfigDetails extends SimpleEntity {
    @Positive
    private long userId;
    @Positive
    private long doorAccessControlId;

    @Positive
    private long doorAccessControlMappingId;

    @Positive
    private long timeConfigId;
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    @Nullable
    private LocalDateTime effectiveStartTime;
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    @Nullable
    private LocalDateTime effectiveEndTime;

    @TableField(typeHandler = DoorAccessControlConfigStateConverter.class)
    private DoorAccessControlConfigState state;

    @TableField("is_effective_start_processed")
    private boolean effectiveStartProcessed;
    @TableField("is_effective_end_processed")
    private boolean effectiveEndProcessed;

    @TableField("is_activated")
    private boolean activated;

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

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

    public DoorAccessControlTimeConfigDetails setSetting(DoorAccessControlConfigSetting o) {
        return this.setTimeConfigId(o.getTimeConfigId()).setEffectiveStartTime(o.getEffectiveStartTime()).setEffectiveEndTime(o.getEffectiveEndTime());
    }

    public DoorAccessControlTimeConfigDetails setSettingOne(DoorAccessControlConfigSettingOne o) {
        return this.setTimeConfigId(o.getTimeConfigId()).setEffectiveStartTime(o.getEffectiveStartTime()).setEffectiveEndTime(o.getEffectiveEndTime());
    }

    public DoorAccessControlTimeConfigDetails resetState() {
        return this.setState(INIT).setEffectiveStartProcessed(false).setEffectiveEndProcessed(false).setActivated(false);
    }
}
