/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.internal.operations.trace;

import com.google.common.collect.ImmutableMap;
import org.gradle.api.internal.artifacts.configurations.ResolveConfigurationDependenciesBuildOperationType;
import org.gradle.internal.logging.events.OperationIdentifier;
import org.gradle.internal.progress.BuildOperationDescriptor;
import org.gradle.internal.progress.OperationFinishEvent;

import java.util.HashMap;
import java.util.Map;

class SerializedOperationFinish {

    final Object id;

    final long endTime;

    final Object result;
    final String resultClassName;

    final String failureMsg;

    SerializedOperationFinish(BuildOperationDescriptor descriptor, OperationFinishEvent finishEvent) {
        this.id = ((OperationIdentifier) descriptor.getId()).getId();
        this.endTime = finishEvent.getEndTime();
        this.result = transform(finishEvent.getResult());
        this.resultClassName = result == null ? null : finishEvent.getResult().getClass().getName();
        this.failureMsg = finishEvent.getFailure() == null ? null : finishEvent.getFailure().toString();
    }

    private Object transform(Object details) {
        if (details != null && ResolveConfigurationDependenciesBuildOperationType.Result.class.isAssignableFrom(details.getClass())) {
            Map<String, Object> map = new HashMap<String, Object>();
            ResolveConfigurationDependenciesBuildOperationType.Result result = (ResolveConfigurationDependenciesBuildOperationType.Result) details;
            map.put("resolvedDependenciesCount", result.getRootComponent().getDependencies().size());
            return map;
        }
        return details;
    }

    SerializedOperationFinish(Map<String, ?> map) {
        this.id = map.get("id");
        this.endTime = (Long) map.get("endTime");
        this.result = map.get("result");
        this.resultClassName = (String) map.get("resultClassName");
        this.failureMsg = (String) map.get("failure");
    }

    Map<String, ?> toMap() {
        ImmutableMap.Builder<String, Object> map = ImmutableMap.builder();

        // Order is optimised for humans looking at the log.

        map.put("id", id);

        if (result != null) {
            map.put("result", result);
            map.put("resultClassName", resultClassName);
        }

        if (failureMsg != null) {
            map.put("failure", failureMsg);
        }

        map.put("endTime", endTime);

        return map.build();
    }

}

@@ -1013,23 +1013,27 @@
 va.util.
-HashMap
+Collections
 ;%0Aimport
@@ -1750,23 +1750,22 @@
 (Object 
-details
+result
 ) %7B%0A    
@@ -1776,26 +1776,25 @@
 if (
-details != null &&
+result instanceof
  Res
@@ -1851,123 +1851,11 @@
 sult
-.class.isAssignableFrom(details.getClass())) %7B%0A            Map%3CString, Object%3E map = new HashMap%3CString, Object%3E();
+) %7B
 %0A   
@@ -1921,21 +1921,19 @@
 .Result 
-resul
+cas
 t = (Res
@@ -1988,23 +1988,22 @@
 Result) 
-details
+result
 ;%0A      
@@ -2004,31 +2004,55 @@
             
-map.put
+return Collections.singletonMap
 (%22resolvedDe
@@ -2069,21 +2069,19 @@
 Count%22, 
-resul
+cas
 t.getRoo
@@ -2132,33 +2132,10 @@
     
-    return map;%0A        
 %7D
+%0A
 %0A   
@@ -2150,15 +2150,14 @@
 urn 
-details
+result
 ;%0A  

Simplify test time serialization of result.