package org.ethereum.net.message;

import org.ethereum.config.SystemProperties;
import org.ethereum.net.client.Capability;
import org.ethereum.net.client.ConfigCapabilities;
import org.ethereum.net.p2p.*;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * This class contains static values of messages on the network. These message
 * will always be the same and therefore don't need to be created each time.
 *
 * @author Roman Mandeleil
 * @since 13.04.14
 */
@Component
public class StaticMessages {

    @Autowired
    SystemProperties config;

    @Autowired
    ConfigCapabilities configCapabilities;

    public final static PingMessage PING_MESSAGE = new PingMessage();
    public final static PongMessage PONG_MESSAGE = new PongMessage();
    public final static GetPeersMessage GET_PEERS_MESSAGE = new GetPeersMessage();
    public final static DisconnectMessage DISCONNECT_MESSAGE = new DisconnectMessage(ReasonCode.REQUESTED);

    public static final byte[] SYNC_TOKEN = Hex.decode("22400891");

    public HelloMessage createHelloMessage(String peerId) {
        return createHelloMessage(peerId, config.listenPort());
    }
    public HelloMessage createHelloMessage(String peerId, int listenPort) {

        String helloAnnouncement = buildHelloAnnouncement();
        byte p2pVersion = (byte) config.defaultP2PVersion();
        List<Capability> capabilities = configCapabilities.getConfigCapabilities();

        return new HelloMessage(p2pVersion, helloAnnouncement,
                capabilities, listenPort, peerId);
    }

    private String buildHelloAnnouncement() {
        String version = config.projectVersion();
        String system = System.getProperty("os.name");
        if (system.contains(" "))
            system = system.substring(0, system.indexOf(" "));
        if (System.getProperty("java.vm.vendor").contains("Android"))
            system = "Android";
        String phrase = config.helloPhrase();

        return String.format("Ethereum(J)/v%s/%s/%s/Java", version, phrase, system);
    }
}

@@ -376,16 +376,80 @@
 il.List;
+%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0A/**%0A *
@@ -1825,32 +1825,317 @@
 ojectVersion();%0A
+        String numberVersion = version;%0A        Pattern pattern = Pattern.compile(%22%5E%5C%5Cd+(%5C%5C.%5C%5Cd+)*%22);%0A        Matcher matcher = pattern.matcher(numberVersion);%0A        if (matcher.find()) %7B%0A            numberVersion = numberVersion.substring(matcher.start(), matcher.end());%0A        %7D%0A
         String s
@@ -2169,24 +2169,24 @@
 %22os.name%22);%0A
-
         if (
@@ -2479,34 +2479,140 @@
 Java
-%22, version, phrase, system
+/%25s%22, numberVersion, system,%0A                config.projectVersionModifier().equalsIgnoreCase(%22release%22) ? %22Release%22 : %22Dev%22, phrase
 );%0A 

Update hello string according to https://github.com/ethereum/EIPs/issues/58