/*
 *    	Copyright 2015-2016 GetSocial B.V.
 *
 *	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 im.getsocialsdk.cordova.plugin;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;

import im.getsocial.sdk.core.resources.Leaderboard;
import im.getsocial.sdk.core.resources.LeaderboardMetaData;
import im.getsocial.sdk.core.resources.LeaderboardScore;

public class GetSocialUtil {

    public static String convertBitmapToBase64(Bitmap bitmap) {
        final int size =bitmap.getByteCount();

        ByteBuffer dst= ByteBuffer.allocate(size);
        bitmap.copyPixelsToBuffer(dst);
        return Base64.encodeToString(dst.array(), Base64.DEFAULT);
    }

    public static Bitmap decodeBase64(String input)
    {
        byte[] decodedBytes = Base64.decode(input, 0);
        return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
    }

    public static byte[] getBytesOfBitmap(Bitmap image)
    {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    }

    public static String serializeLeaderbord(Leaderboard leaderboard) {
        JSONObject object = new JSONObject();
        try {
            object.put("leaderboardMetaData", serializeLeaderboardMetaData(leaderboard.getLeaderboardMetaData()));
            object.put("leaderboardScore", serializeLeaderboardScore(leaderboard.getCurrentScore()));
        } catch (Exception e) {}
        return object.toString();
    }

    public static JSONObject serializeLeaderboardMetaData(LeaderboardMetaData data) {
        if(data == null)
            return new JSONObject();

        JSONObject object = new JSONObject();
        try {
            object.put("leaderboardId", data.getLeaderboardId());
            object.put("name", data.getName());
            object.put("units", data.getUnits());
            object.put("direction", data.getDirection());
            object.put("format", data.getFormat());
            object.put("published", data.isPublished());
        } catch (Exception e) {}

        return object;
    }

    public static JSONObject serializeLeaderboardScore(LeaderboardScore data) {
        if(data == null)
            return new JSONObject();

        JSONObject object = new JSONObject();
        try {
            object.put("user", data.getUser().serialize());
            object.put("value", data.getValue());
            object.put("rank", data.getRank());
        } catch (Exception e) {}

        return object;
    }
}
