package com.inspect.metadata.prodevmnt.domain.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.inspect.metadata.patrolpointmnt.domain.BasedataPatrolPoint;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class AreaDeviceTreeSelect implements Serializable {
    private static final long serialVersionUID = 1L;
    private String dataId;
    private Long areaId;
    private Long areaParentId;
    private String areaName;
    private String areaFlag;
    private String areaType;
    private String powerMainEquFlag;
    private Long deviceId;
    private Long deviceParentId;
    private String deviceName;
    private String parentId;
    private String positionX;
    private String positionY;
    private String positionZ;
    private String deviceLength;
    private String deviceWidth;
    private String deviceHigh;

    private Long pointNum;

    private List<String> patrolPointNames;

    private  List<BasedataPatrolPoint> basedataPatrolPoints;

    public List<String> getPatrolPointNames() {
        return patrolPointNames;
    }

    public void setPatrolPointNames(List<String> patrolPointNames) {
        this.patrolPointNames = patrolPointNames;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String patrolPointCode;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String patrolPointId;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String patrolPointName;
    @JsonInclude(Include.NON_EMPTY)
    private List<AreaDeviceTreeSelect> children;

    public AreaDeviceTreeSelect() {
    }

    public AreaDeviceTreeSelect(BaseDataAreaProDeviceVO area) {
        this.dataId = area.getAreaIdString();
        this.areaId = area.getAreaId();
        this.areaName = area.getAreaName();
        this.areaFlag = area.getAreaFlag();
        this.areaType = area.getAreaType();
        this.powerMainEquFlag = area.getPowerMainEquFlag();
        this.deviceId = area.getDeviceId();
        this.deviceName = area.getDeviceName();
        this.parentId = area.getParentIdString();
        this.positionX = area.getPositionX();
        this.positionY = area.getPositionY();
        this.positionZ = area.getPositionZ();
        this.deviceLength = area.getDeviceLength();
        this.deviceWidth = area.getDeviceWidth();
        this.deviceHigh = area.getDeviceHigh();
        this.deviceParentId = area.getDeviceParentId();
        this.areaParentId = area.getAreaParentId();
        this.children = (List) area.getChildren().stream().map(AreaDeviceTreeSelect::new).collect(Collectors.toList());
    }

    public String getDataId() {
        return this.dataId;
    }

    public void setDataId(String dataId) {
        this.dataId = dataId;
    }

    public Long getAreaId() {
        return this.areaId;
    }

    public void setAreaId(Long areaId) {
        this.areaId = areaId;
    }

    public String getAreaName() {
        return this.areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public String getAreaFlag() {
        return this.areaFlag;
    }

    public void setAreaFlag(String areaFlag) {
        this.areaFlag = areaFlag;
    }

    public String getAreaType() {
        return this.areaType;
    }

    public void setAreaType(String areaType) {
        this.areaType = areaType;
    }

    public String getPowerMainEquFlag() {
        return this.powerMainEquFlag;
    }

    public void setPowerMainEquFlag(String powerMainEquFlag) {
        this.powerMainEquFlag = powerMainEquFlag;
    }

    public Long getDeviceId() {
        return this.deviceId;
    }

    public void setDeviceId(Long deviceId) {
        this.deviceId = deviceId;
    }

    public String getDeviceName() {
        return this.deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getParentId() {
        return this.parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getPositionX() {
        return this.positionX;
    }

    public void setPositionX(String positionX) {
        this.positionX = positionX;
    }

    public String getPositionY() {
        return this.positionY;
    }

    public void setPositionY(String positionY) {
        this.positionY = positionY;
    }

    public String getPositionZ() {
        return this.positionZ;
    }

    public void setPositionZ(String positionZ) {
        this.positionZ = positionZ;
    }

    public String getDeviceLength() {
        return this.deviceLength;
    }

    public void setDeviceLength(String deviceLength) {
        this.deviceLength = deviceLength;
    }

    public String getDeviceWidth() {
        return this.deviceWidth;
    }

    public void setDeviceWidth(String deviceWidth) {
        this.deviceWidth = deviceWidth;
    }

    public String getDeviceHigh() {
        return this.deviceHigh;
    }

    public void setDeviceHigh(String deviceHigh) {
        this.deviceHigh = deviceHigh;
    }

    public Long getAreaParentId() {
        return this.areaParentId;
    }

    public void setAreaParentId(Long areaParentId) {
        this.areaParentId = areaParentId;
    }

    public Long getDeviceParentId() {
        return this.deviceParentId;
    }

    public void setDeviceParentId(Long deviceParentId) {
        this.deviceParentId = deviceParentId;
    }

    public List<AreaDeviceTreeSelect> getChildren() {
        return this.children;
    }

    public void setChildren(List<AreaDeviceTreeSelect> children) {
        this.children = children;
    }

    public String getPatrolPointId() {
        return patrolPointId;
    }

    public void setPatrolPointId(String patrolPointId) {
        this.patrolPointId = patrolPointId;
    }

    public String getPatrolPointName() {
        return patrolPointName;
    }

    public void setPatrolPointName(String patrolPointName) {
        this.patrolPointName = patrolPointName;
    }

    public Long getPointNum() {
        return pointNum;
    }

    public void setPointNum(Long pointNum) {
        this.pointNum = pointNum;
    }

    public List<BasedataPatrolPoint> getBasedataPatrolPoints() {
        return basedataPatrolPoints;
    }

    public void setBasedataPatrolPoints(List<BasedataPatrolPoint> basedataPatrolPoints) {
        this.basedataPatrolPoints = basedataPatrolPoints;
    }

    public String getPatrolPointCode() {
        return patrolPointCode;
    }

    public void setPatrolPointCode(String patrolPointCode) {
        this.patrolPointCode = patrolPointCode;
    }

    //    public List<AreaDeviceTreeSelect> cleanEmptyPatrolPoints() {
//        // 递归清理
//        if (children != null) {
//            // 清理子节点
//            for (int i = children.size() - 1; i >= 0; i--) {
//                AreaDeviceTreeSelect child = children.get(i);
//                child.cleanEmptyPatrolPoints(); // 递归清理子节点
//                // 如果子节点是叶子节点且 patrolPointId 为空，则移除
//                if (child.getChildren() == null || child.getChildren().isEmpty()) {
//                    if (child.getPatrolPointId() == null) {
//                        children.remove(i);
//                    }
//                }
//            }
//        }
//        return children;
//    }

    public List<AreaDeviceTreeSelect> cleanEmptyPatrolPoints() {
        if (children != null) {
            // 清理子节点
            Set<String> seenDataIds = new HashSet<>();
            List<AreaDeviceTreeSelect> uniqueChildren = new ArrayList<>();

            for (AreaDeviceTreeSelect child : children) {
                // 递归清理子节点
                child.cleanEmptyPatrolPoints();

                // 检查 patrolPointId 是否为空且没有子节点
                if (child.getPatrolPointId() == null && (child.getChildren() == null || child.getChildren().isEmpty())) {
                    continue; // 跳过此子节点
                }

                // 检查 dataId 是否已存在
                if (!seenDataIds.contains(child.getDataId())) {
                    seenDataIds.add(child.getDataId());
                    uniqueChildren.add(child); // 只保留第一个出现的子节点
                }
            }

            // 更新 children 列表为唯一的子节点
            this.children = uniqueChildren;
        }
        return children;
    }

}
