package io.subutai.core.hubmanager.impl.processor;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.subutai.common.peer.ResourceHost;
import io.subutai.common.settings.SubutaiInfo;
import io.subutai.core.hubmanager.impl.ConfigManager;
import io.subutai.core.hubmanager.impl.HubManagerImpl;
import io.subutai.core.hubmanager.impl.http.HubRestClient;
import io.subutai.core.hubmanager.impl.http.RestResult;
import io.subutai.core.peer.api.PeerManager;
import io.subutai.hub.share.dto.VersionInfoDto;

import static java.lang.String.format;


public class VersionInfoProcessor implements Runnable
{
    private final Logger log = LoggerFactory.getLogger( getClass() );

    private ConfigManager configManager;

    private HubManagerImpl manager;

    private PeerManager peerManager;

    private final HubRestClient restClient;


    public VersionInfoProcessor( final HubManagerImpl integration, final PeerManager peerManager,
                                 final ConfigManager configManager )
    {
        this.peerManager = peerManager;
        this.configManager = configManager;
        this.manager = integration;

        restClient = new HubRestClient( configManager );
    }


    @Override
    public void run()
    {
        try
        {
            sendVersionInfo();
        }
        catch ( Exception e )
        {
            log.debug( "Sending version info failed." );

            log.error( e.getMessage(), e );
        }
    }


    public void sendVersionInfo() throws Exception
    {
        String path = format( "/rest/v1/peers/%s/version-info", peerManager.getLocalPeer().getId() );

        VersionInfoDto versionInfoDto = new VersionInfoDto();

        versionInfoDto.setPeerId( configManager.getPeerId() );
        versionInfoDto.setSsVersion( SubutaiInfo.getVersion() );
        versionInfoDto.setBuildTime( SubutaiInfo.getBuildTime() );
        versionInfoDto.setBranch( SubutaiInfo.getBranch() );
        versionInfoDto.setCommitId( SubutaiInfo.getCommitId() );

        ResourceHost host = configManager.getPeerManager().getLocalPeer().getManagementHost();

        versionInfoDto.setP2pVersion( host.getP2pVersion().replace( "p2p Cloud project", "" ).trim() );
        versionInfoDto.setRhVersion( host.getRhVersion().replace( "Subutai version", "" ).trim() );

        RestResult<Object> restResult = restClient.post( path, versionInfoDto );
        if ( !restResult.isSuccess() )
        {
            throw new Exception( "Error on sending version info to hub: " + restResult.getError() );
        }
    }
}

@@ -1521,24 +1521,76 @@
 ption%0A    %7B%0A
+        if ( manager.isRegistered() )%0A        %7B%0A    
         Stri
@@ -1688,16 +1688,20 @@
         
+    
 VersionI
@@ -1747,32 +1747,36 @@
 Dto();%0A%0A        
+   
+ 
 versionInfoDto.s
@@ -1810,24 +1810,28 @@
 PeerId() );%0A
+    
         vers
@@ -1883,32 +1883,36 @@
 on() );%0A        
+   
+ 
 versionInfoDto.s
@@ -1954,32 +1954,36 @@
 me() );%0A        
+    
 versionInfoDto.s
@@ -2019,32 +2019,36 @@
 ch() );%0A        
+  
+  
 versionInfoDto.s
@@ -2081,32 +2081,36 @@
 tCommitId() );%0A%0A
+    
         Resource
@@ -2189,32 +2189,36 @@
 ost();%0A%0A        
+  
+  
 versionInfoDto.s
@@ -2297,32 +2297,36 @@
 im() );%0A        
+    
 versionInfoDto.s
@@ -2410,16 +2410,20 @@
         
+ 
+   
 RestResu
@@ -2491,24 +2491,28 @@
  );%0A        
+    
 if ( !restRe
@@ -2530,34 +2530,42 @@
 ess() )%0A        
-%7B%0A
+    %7B%0A    
             thro
@@ -2645,24 +2645,38 @@
 tError() );%0A
+            %7D%0A
         %7D%0A  

Check for if peer registered to Hub