/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.jclouds.dimensiondata.cloudcontroller.compute.functions;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.get;

import javax.inject.Singleton;

import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.dimensiondata.cloudcontroller.domain.OsImage;

import com.google.common.base.Function;
import com.google.common.base.Splitter;

@Singleton
public class OsImageToImage implements Function<OsImage, Image> {


    private static final String CENTOS = "CENTOS";
    private static final String REDHAT = "REDHAT";
    private static final String UBUNTU = "UBUNTU";

    @Override
    public Image apply(OsImage from) {

        checkNotNull(from, "image");

        OsFamily osFamily = osFamily().apply(from.operatingSystem().id());
        String osVersion = parseVersion(from.operatingSystem().id());

        OperatingSystem os = OperatingSystem.builder()
                .description(from.description())
                .family(osFamily)
                .version(osVersion)
                .is64Bit(is64bit(from))
                .build();

        return new ImageBuilder()
                .id(from.id())
                .name(from.name())
                .status(Image.Status.AVAILABLE)
                .operatingSystem(os)
                .build();
    }

    private boolean is64bit(OsImage inspectedImage) {
        return inspectedImage.operatingSystem().id().contains("64");
    }

    private Function<String, OsFamily> osFamily() {
        return new Function<String, OsFamily>() {

            @Override
            public OsFamily apply(final String id) {
                if (id != null) {
                    if (id.startsWith(CENTOS)) return OsFamily.CENTOS;
                    else if (id.contains(UBUNTU)) return OsFamily.UBUNTU;
                    else if (id.contains(REDHAT)) return OsFamily.RHEL;
                }
                return OsFamily.UNRECOGNIZED;
            }
        };
    }

    private String parseVersion(String id) {
        String idWithoutArch = get(Splitter.on("/").split(id), 0);
        return idWithoutArch.substring(idWithoutArch.length() - 1);
    }
}

@@ -877,73 +877,8 @@
 s;%0A%0A
-import static com.google.common.base.Preconditions.checkNotNull;%0A
 impo
@@ -1414,19 +1414,19 @@
 TOS = %22C
-ENT
+ent
 OS%22;%0A   
@@ -1465,21 +1465,21 @@
 HAT = %22R
-EDHAT
+edHat
 %22;%0A    p
@@ -1516,21 +1516,117 @@
 NTU = %22U
-BUNTU
+buntu%22;%0A    private static final String SUSE = %22SuSE%22;%0A    private static final String WINDOWS = %22Win
 %22;%0A%0A    
@@ -1678,47 +1678,8 @@
 ) %7B%0A
-%0A        checkNotNull(from, %22image%22);%0A%0A
     
@@ -1724,36 +1724,27 @@
 ly(from.
-operatingSystem().id
+description
 ());%0A   
@@ -1785,36 +1785,27 @@
 on(from.
-operatingSystem().id
+description
 ());%0A%0A  
@@ -2559,26 +2559,35 @@
 inal String 
-i
 d
+escription
 ) %7B%0A        
@@ -2598,18 +2598,27 @@
     if (
-i
 d
+escription
  != null
@@ -2645,18 +2645,27 @@
     if (
-i
 d
+escription
 .startsW
@@ -2722,34 +2722,43 @@
        else if (
-i
 d
+escription
 .contains(UBUNTU
@@ -2813,18 +2813,27 @@
 lse if (
-i
 d
+escription
 .contain
@@ -2865,16 +2865,180 @@
 y.RHEL;%0A
+                    else if (description.contains(SUSE)) return OsFamily.RHEL;%0A                    else if (description.contains(WINDOWS)) return OsFamily.WINDOWS;%0A
         
@@ -3164,18 +3164,29 @@
 (String 
-i
 d
+ocumentation
 ) %7B%0A    
@@ -3196,29 +3196,137 @@
  String 
-idWithoutArch
+version;%0A        if (documentation.toLowerCase().startsWith(OsFamily.CENTOS.value().toLowerCase())) %7B%0A            version
  = get(S
@@ -3337,17 +3337,17 @@
 ter.on(%22
-/
+ 
 %22).split
@@ -3351,14 +3351,25 @@
 lit(
-i
 d
+ocumentation
 ), 
-0
+2
 );%0A 
@@ -3379,66 +3379,285 @@
     
-return idWithoutArch.substring(idWithoutArch.length() - 1)
+%7D else if (documentation.toLowerCase().startsWith(OsFamily.SUSE.value().toLowerCase())) %7B%0A            version = get(Splitter.on(%22 %22).split(documentation), 4);%0A        %7D else %7B%0A            version = get(Splitter.on(%22 %22).split(documentation), 1);%0A        %7D%0A        return version
 ;%0A  

improve OsImageToImage