package com.lanyuanxiaoyao.service.web.entity;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lanyuanxiaoyao.service.common.entity.RunMeta;
import com.lanyuanxiaoyao.service.configuration.entity.zookeeper.ZookeeperNode;
import com.lanyuanxiaoyao.service.configuration.utils.DatetimeUtil;

import java.time.Instant;
import java.util.Optional;

/**
 * Zookeeper 节点
 *
 * @author lanyuanxiaoyao
 * @date 2023-05-23
 */
public class ZookeeperNodeVO {
    @JsonIgnore
    private final ZookeeperNode node;
    private final RunMeta runMeta;
    private String createTimeFromNow;
    private String modifiedTimeFromNow;

    public ZookeeperNodeVO(ZookeeperNode node, RunMeta runMeta) {
        this.node = node;
        this.runMeta = runMeta;

        long now = Instant.now().toEpochMilli();
        if (ObjectUtil.isNotNull(node.getCreateTime()) && node.getCreateTime() != 0) {
            this.createTimeFromNow = DatetimeUtil.fromNow(now, node.getCreateTime());
        }
        if (ObjectUtil.isNotNull(node.getModifiedTime()) && node.getModifiedTime() != 0) {
            this.modifiedTimeFromNow = DatetimeUtil.fromNow(now, node.getModifiedTime());
        }
    }

    public String getPath() {
        return node.getPath();
    }

    public String getLabel() {
        return node.getLabel();
    }

    public Long getCreateTime() {
        return node.getCreateTime();
    }

    public Long getModifiedTime() {
        return node.getModifiedTime();
    }

    public String getCluster() {
        return Optional.of(runMeta).map(RunMeta::getCluster).orElse(null);
    }

    public Long getFlinkJobId() {
        return Optional.of(runMeta).map(RunMeta::getFlinkJobId).orElse(null);
    }

    public String getFlinkJobName() {
        return Optional.of(runMeta).map(RunMeta::getFlinkJobName).orElse(null);
    }

    public String getHost() {
        return Optional.of(runMeta).map(RunMeta::getHost).orElse(null);
    }

    public String getApplicationId() {
        return Optional.of(runMeta).map(RunMeta::getApplicationId).orElse(null);
    }

    public String getContainerId() {
        return Optional.of(runMeta).map(RunMeta::getContainerId).orElse(null);
    }

    public String getContainerPath() {
        return Optional.of(runMeta).map(RunMeta::getContainerPath).orElse(null);
    }

    public String getRunType() {
        return Optional.of(runMeta).map(RunMeta::getRunType).orElse(null);
    }

    public String getExecutorVersion() {
        return Optional.of(runMeta).map(RunMeta::getExecutorVersion).orElse(null);
    }

    public String getJvmPid() {
        return Optional.of(runMeta).map(RunMeta::getJvmPid).orElse(null);
    }

    public String getApplicationProxy() {
        return Optional.of(runMeta).map(RunMeta::getApplicationProxy).orElse(null);
    }

    public String getCreateTimeFromNow() {
        return createTimeFromNow;
    }

    public String getModifiedTimeFromNow() {
        return modifiedTimeFromNow;
    }

    @Override
    public String toString() {
        return "ZookeeperNodeVO{" +
                "node=" + node +
                ", createTimeFormNow='" + createTimeFromNow + '\'' +
                ", modifiedTimeFormNow='" + modifiedTimeFromNow + '\'' +
                '}';
    }
}
