/*
 * Copyright 2017 Patrik Karlsson.
 *
 * 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 se.trixon.idl.shared;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Patrik Karlsson
 */
public class ImageDescriptor {

    private static final int FILE_FORMAT_VERSION = 1;
    private static final Gson GSON = new GsonBuilder()
            .setVersion(1.0)
            .serializeNulls()
            .setPrettyPrinting()
            .create();
    private static final Logger LOGGER = Logger.getLogger(ImageDescriptor.class.getName());
    @SerializedName("base64")
    private String mBase64;
    @SerializedName("format_version")
    private int mFileFormatVersion;
    @SerializedName("path")
    private String mPath;

    public static ImageDescriptor fromJson(String json) throws IOException, JsonSyntaxException {
        ImageDescriptor descriptor = GSON.fromJson(json, ImageDescriptor.class);

        if (descriptor.mFileFormatVersion != FILE_FORMAT_VERSION) {
            //TODO Handle file format version change
        }

        return descriptor;
    }

    public ImageDescriptor() {
    }

    public String getBase64() {
        return mBase64;
    }

    public byte[] getByteArray() {
        return Base64.decodeBase64(mBase64);
    }

    public int getFileFormatVersion() {
        return mFileFormatVersion;
    }

    public ImageIcon getImageIcon() {
        return new ImageIcon(getByteArray());
    }

    public String getPath() {
        return mPath;
    }

    public void setBase64(String base64) {
        mBase64 = base64;
    }

    public void setBase64FromPath(String path) {
        try {
            mBase64 = Base64.encodeBase64String(FileUtils.readFileToByteArray(new File(path)));
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }
    }

    public void setFileFormatVersion(int fileFormatVersion) {
        mFileFormatVersion = fileFormatVersion;
    }

    public void setPath(String path) {
        mPath = path;
    }

    public String toJson() {
        return GSON.toJson(this);
    }
}

@@ -785,16 +785,53 @@
 edName;%0A
+import java.io.ByteArrayInputStream;%0A
 import j
@@ -843,16 +843,16 @@
 o.File;%0A
-
 import j
@@ -1128,62 +1128,8 @@
  %7B%0A%0A
-    private static final int FILE_FORMAT_VERSION = 1;%0A
     
@@ -1757,140 +1757,8 @@
 );%0A%0A
-        if (descriptor.mFileFormatVersion != FILE_FORMAT_VERSION) %7B%0A            //TODO Handle file format version change%0A        %7D%0A%0A
     
@@ -2011,24 +2011,24 @@
 Version() %7B%0A
-
         retu
@@ -2049,32 +2049,265 @@
 Version;%0A    %7D%0A%0A
+    public java.awt.Image getImage() %7B%0A        return getImageIcon().getImage();%0A    %7D%0A%0A    public javafx.scene.image.Image getImageFx() %7B%0A        return new javafx.scene.image.Image(new ByteArrayInputStream(getByteArray()));%0A    %7D%0A%0A
     public Image

Add getters