/*
 * Copyright 2013-2015 microG Project Team
 *
 * 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 com.google.android.gms.wearable.internal;

import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;

import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataItem;

import org.microg.gms.common.PublicApi;
import org.microg.safeparcel.AutoSafeParcelable;
import org.microg.safeparcel.SafeParceled;

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

/**
 * {@link PutDataRequest} is used to create new data items in the Android Wear network.
 */
@PublicApi
public class PutDataRequest extends AutoSafeParcelable {
    public static final String WEAR_URI_SCHEME = "wear";

    @SafeParceled(1)
    private int versionCode = 1;
    @SafeParceled(2)
    final Uri uri;
    @SafeParceled(4)
    private final Bundle assets;
    @SafeParceled(5)
    byte[] data;

    private PutDataRequest() {
        uri = null;
        assets = new Bundle();
    }

    private PutDataRequest(Uri uri) {
        this.uri = uri;
        assets = new Bundle();
    }

    public static PutDataRequest create(Uri uri) {
        return new PutDataRequest(uri);
    }

    public static PutDataRequest create(String path) {
        if (TextUtils.isEmpty(path)) {
            throw new IllegalArgumentException("An empty path was supplied.");
        } else if (!path.startsWith("/")) {
            throw new IllegalArgumentException("A path must start with a single / .");
        } else if (path.startsWith("//")) {
            throw new IllegalArgumentException("A path must start with a single / .");
        } else {
            return create((new Uri.Builder()).scheme(WEAR_URI_SCHEME).path(path).build());
        }
    }

    public static PutDataRequest createFromDataItem(DataItem source) {
        PutDataRequest dataRequest = new PutDataRequest(source.getUri());
        dataRequest.data = source.getData();
        // TODO: assets
        return dataRequest;
    }

    public static PutDataRequest createWithAutoAppendedId(String pathPrefix) {
        return new PutDataRequest(null);
    }

    public Asset getAsset(String key) {
        return assets.getParcelable(key);
    }

    public Map<String, Asset> getAssets() {
        Map<String, Asset> map = new HashMap<String, Asset>();
        assets.setClassLoader(DataItemAssetParcelable.class.getClassLoader());
        for (String key : assets.keySet()) {
            map.put(key, (Asset) assets.getParcelable(key));
        }
        return map;
    }

    public byte[] getData() {
        return data;
    }

    public Uri getUri() {
        return uri;
    }

    public boolean hasAsset(String key) {
        return assets.containsKey(key);
    }

    public PutDataRequest putAsset(String key, Asset value) {
        assets.putParcelable(key, value);
        return this;
    }

    public PutDataRequest removeAsset(String key) {
        assets.remove(key);
        return this;
    }

    public PutDataRequest setData(byte[] data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return toString(false);
    }

    public String toString(boolean verbose) {
        return "PutDataRequest[uri=" + uri +
                ", data=" + (data == null ? "null" : Base64.encodeToString(data, Base64.NO_WRAP)) +
                ", numAssets=" + getAssets().size() + "]";
        // TODO: Verbose: dump all assets
    }

    public static final Creator<PutDataRequest> CREATOR = new AutoCreator<PutDataRequest>(PutDataRequest.class);
}

@@ -1261,16 +1261,85 @@
  %22wear%22;
+%0A    private static final int DEFAULT_SYNC_DEADLINE = 30 * 60 * 1000;
 %0A%0A    @S
@@ -1517,16 +1517,84 @@
 %5B%5D data;
+%0A    @SafeParceled(6)%0A    long syncDeadline = DEFAULT_SYNC_DEADLINE;
 %0A%0A    pr
@@ -3909,23 +3909,74 @@
         
-return 
+StringBuilder sb = new StringBuilder();%0A        sb.append(
 %22PutData
@@ -3992,16 +3992,21 @@
 ri=%22
- + uri +
+).append(uri)
 %0A   
@@ -4018,16 +4018,24 @@
         
+.append(
 %22, data=
@@ -4035,19 +4035,24 @@
 , data=%22
- + 
+).append
 (data ==
@@ -4112,18 +4112,16 @@
 O_WRAP))
- +
 %0A       
@@ -4129,16 +4129,24 @@
         
+.append(
 %22, numAs
@@ -4151,19 +4151,25 @@
 Assets=%22
- + 
+).append(
 getAsset
@@ -4182,57 +4182,345 @@
 ze()
- + %22%5D%22;%0A        // TODO: Verbose: dump all assets
+);%0A        if (verbose) %7B%0A            sb.append(%22,assets=%5B%22);%0A            for (String key : getAssets().keySet()) %7B%0A                sb.append(key).append('=').append(getAsset(key)).append(%22, %22);%0A            %7D%0A            sb.delete(sb.length() - 2, sb.length()).append('%5D');%0A        %7D%0A        sb.append(%22%5D%22);%0A        return sb.toString();
 %0A   

Add field to wearable PutDataRequest