package com.tsi.comm.server.mongo.dto;

import com.tsi.app.common.cpu.dto.TsiCvimAbnormal;
import com.tsi.app.common.cpu.dto.TsiCvimControl;
import com.tsi.app.common.cpu.dto.TsiCvimStatus;
import com.tsi.app.common.utils.StringUtils;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
//import org.springframework.data.annotation.Id;
//import org.springframework.data.mongodb.core.mapping.Document;

import java.util.ArrayList;
import java.util.List;

@Getter
@ToString
//@Document(collection = "tb_tcs_node_status")
public class TcsNodeStatus {

//    @Id
    private Long _id;
    private Long nodeId;
    private boolean isConnect;
    private String collectTime;
    private String tscDateTime;
    private int cycleElapsedTime;
    private TsiCvimControl tscControlInfo = new TsiCvimControl();
    private TsiCvimAbnormal tscAbnormalInfo = new TsiCvimAbnormal();
    private int signalStatusInfoCount;
    private List<TsiCvimStatus> signalStatusInfos = new ArrayList<>();

    @Builder
    public TcsNodeStatus(Long _id,
                         Long nodeId,
                         boolean isConnect,
                         String collectTime,
                         String tscDateTime,
                         int cycleElapsedTime,
                         TsiCvimControl tscControlInfo,
                         TsiCvimAbnormal tscAbnormalInfo,
                         int signalStatusInfoCount,
                         List<TsiCvimStatus> signalStatusInfos) {
        this._id = _id;
        this.nodeId = nodeId;
        this.isConnect = isConnect;
        this.collectTime = collectTime;
        this.tscDateTime = tscDateTime;
        this.cycleElapsedTime = cycleElapsedTime;
        this.tscControlInfo = tscControlInfo;
        this.tscAbnormalInfo = tscAbnormalInfo;
        this.signalStatusInfoCount = signalStatusInfoCount;
        this.signalStatusInfos = signalStatusInfos;
    }

    private String toValue(String connect) {
        return this.nodeId+","+connect+","+this.collectTime+","+this.tscDateTime+","+this.cycleElapsedTime+","+this.signalStatusInfoCount;
    }

    public String toDataString() {
        String connect = StringUtils.getString(this.isConnect);
        if (this.isConnect) {
            String statusValue = "";
            for (TsiCvimStatus status: this.signalStatusInfos) {
                statusValue = statusValue + status.toDataString() + "|";
            }

            return    toValue(connect)
                    + "^"
                    + this.tscControlInfo.toDataString()
                    + "^"
                    + this.tscAbnormalInfo.toDataString()
                    + "^"
                    + statusValue;
        }
        return this.nodeId+","+connect;
    }
}
