import { crouton_thrift } from '../platform_abstraction_layer'; // eslint-disable-line camelcase
import _each from '../_each'; // eslint-disable-line camelcase
import * as coerce from './coerce.js';
let proto = require('./generated_proto/collector_pb.js');
let converter = require('hex2dec');
const packageObject = require('../../package.json');

export default class RuntimeImp {
    constructor(runtimeGUID, startMicros, componentName, attributes) {
        this._runtimeGUID = runtimeGUID;
        this._startMicros = startMicros;
        this._componentName = componentName;
        this._attributes = attributes;
    }

    toThrift() {
        let thriftAttrs = [];
        _each(this._attributes, (val, key) => {
            thriftAttrs.push(new crouton_thrift.KeyValue({
                Key   : coerce.toString(key),
                Value : coerce.toString(val),
            }));
        });

        // NOTE: for legacy reasons, the Thrift field is called "group_name"
        // but is semantically equivalent to the "component_name"
        return new crouton_thrift.Runtime({
            guid         : this._runtimeGUID,
            start_micros : this._startMicros,
            group_name   : this._componentName,
            attrs        : thriftAttrs,
        });
    }

    toProto() {
        let tracerVersion = new proto.KeyValue();
        tracerVersion.setKey('lightstep.tracer_version');
        tracerVersion.setStringValue(packageObject.version);

        let tracerPlatform = new proto.KeyValue();
        tracerPlatform.setKey('lightstep.tracer_platform');
        tracerPlatform.setStringValue(this._attributes['lightstep.tracer_platform']);

        let tracerPlatformVersion = new proto.KeyValue();
        tracerPlatformVersion.setKey('lightstep.tracer_platform_version');
        tracerPlatformVersion.setStringValue(this._attributes['lightstep.tracer_platform_version']);

        let componentName = new proto.KeyValue();
        componentName.setKey('lightstep.component_name');
        componentName.setStringValue(this._componentName);

        let commandLine = new proto.KeyValue();
        commandLine.setKey('lightstep.command_line');
        commandLine.setStringValue(this._attributes['lightstep.command_line']);

        let hostname = new proto.KeyValue();
        hostname.setKey('lightstep.hostname');
        hostname.setStringValue(this._attributes['lightstep.hostname']);

        let reporterId = converter.hexToDec(this._runtimeGUID);

        let reporterProto = new proto.Reporter();
        reporterProto.setReporterId(reporterId);
        reporterProto.setTagsList([
            tracerVersion, tracerPlatform, componentName, commandLine, hostname, tracerPlatformVersion,
        ]);

        return reporterProto;
    }
}

@@ -2493,29 +2493,101 @@
     let 
-reporterProto
+tracerTags = %5B%5D;%0A        _each(this._attributes, (val, key) =%3E %7B%0A            let ttag
  = new p
@@ -2591,24 +2591,24 @@
 w proto.
-Reporter
+KeyValue
 ();%0A    
@@ -2615,97 +2615,144 @@
     
-reporterProto.setReporterId(reporterId);%0A        reporterProto.setTagsList(%5B%0A            
+    ttag.setKey(key);%0A            ttag.setStringValue(val);%0A            tracerTags.push(ttag);%0A        %7D);%0A%0A        let reporterTags = %5B
 trac
@@ -2837,17 +2837,18 @@
 mVersion
-,
+%5D;
 %0A       
@@ -2848,17 +2848,204 @@
         
-%5D
+let allTags = reporterTags.concat(tracerTags);%0A%0A        let reporterProto = new proto.Reporter();%0A        reporterProto.setReporterId(reporterId);%0A        reporterProto.setTagsList(allTags
 );%0A%0A    

add tracer tags to report (#170)