package io.wdd.rpc.status.deprecate;


import io.wdd.common.utils.FormatUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import oshi.hardware.NetworkIF;

import java.util.List;
import java.util.stream.Collectors;


@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder(toBuilder = true)
public class NetworkInfo {

    private String name;
    private String displayName;
    private String macAddr;
    private String mtu;
    private String[] innerIpv4Addr;
    private String publicIpv4Addr;
    private String[] innerIpv6Addr;
    private String publicIpv6Addr;
    /**
     * 过去1s内的网速，接收速率
     */
    private String recvSpeed;
    /**
     * 过去1s内的网速，发送速率
     */
    private String sendSpeed;
    private String trafficRecv;
    private String trafficSend;

    public static NetworkInfo mapFromNetworkIF(NetworkIF networkIF) {

        return NetworkInfo
                .builder()
                .name(networkIF.getName())
                .displayName(networkIF.getDisplayName())
                .mtu(String.valueOf(networkIF.getMTU()))
                .macAddr(networkIF.getMacaddr())
                .innerIpv4Addr(generateIPDICRFromNetworkIFList(
                        networkIF,
                        4
                ))
                .innerIpv6Addr(generateIPDICRFromNetworkIFList(
                        networkIF,
                        6
                ))
                .trafficSend(FormatUtils.formatData(networkIF.getBytesSent()))
                .trafficRecv(FormatUtils.formatData(networkIF.getBytesRecv())
                )
                .build();

    }
    public static List<NetworkInfo> mapFromNetworkIFS(List<NetworkIF> networkIFList) {

        return networkIFList
                .stream()
                .map(
                        networkIF -> NetworkInfo
                                .builder()
                                .name(networkIF.getName())
                                .displayName(networkIF.getDisplayName())
                                .mtu(String.valueOf(networkIF.getMTU()))
                                .macAddr(networkIF.getMacaddr())
                                .innerIpv4Addr(generateIPDICRFromNetworkIFList(
                                        networkIF,
                                        4
                                ))
                                .innerIpv6Addr(generateIPDICRFromNetworkIFList(
                                        networkIF,
                                        6
                                ))
                                .trafficSend(FormatUtils.formatData(networkIF.getBytesSent()))
                                .trafficRecv(FormatUtils.formatData(networkIF.getBytesRecv())
                                )
                                .build())
                .collect(Collectors.toList());
    }

    private static String[] generateIPDICRFromNetworkIFList(NetworkIF networkIF, int Ipv4OrIpv6) {

        String[] iPAddr;
        Short[] subnetMasks;

        if (Ipv4OrIpv6 == 4) {
            iPAddr = networkIF.getIPv4addr();
            subnetMasks = networkIF.getSubnetMasks();
        } else {
            iPAddr = networkIF.getIPv6addr();
            subnetMasks = networkIF.getPrefixLengths();
        }

        String[] result = new String[iPAddr.length];
        for (int index = 0; index < iPAddr.length; index++) {
            result[index] = iPAddr[index] + "/" + subnetMasks[index];
        }

        return result;
    }
}
