/*
 * Copyright 2020 The Android Open Source Project
 *
 * 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 androidx.wear.watchface.control.data;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.versionedparcelable.ParcelField;
import androidx.versionedparcelable.ParcelUtils;
import androidx.versionedparcelable.VersionedParcelable;
import androidx.versionedparcelable.VersionedParcelize;
import androidx.wear.watchface.control.IWatchFaceControlService;
import androidx.wear.watchface.data.DeviceConfig;
import androidx.wear.watchface.data.IdAndComplicationDataWireFormat;
import androidx.wear.watchface.data.WatchUiState;
import androidx.wear.watchface.style.data.UserStyleWireFormat;

import java.util.List;

/**
 * Parameters for {@link IWatchFaceControlService#createPendingInteractiveWatchFace}.
 *
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@VersionedParcelize(allowSerialization = true)
@SuppressLint("BanParcelableUsage") // TODO(b/169214666): Remove Parcelable
public class WallpaperInteractiveWatchFaceInstanceParams
        implements VersionedParcelable, Parcelable {

    /** The id for the new instance, must be unique. */
    @ParcelField(1)
    @NonNull
    String mInstanceId;

    /** The {@link DeviceConfig} for the host wearable. */
    @ParcelField(2)
    @NonNull
    DeviceConfig mDeviceConfig;

    /** The {@link WatchUiState} for the host wearable. */
    @ParcelField(3)
    @NonNull
    WatchUiState mWatchUiState;

    /** The initial {@link UserStyleWireFormat}. */
    @ParcelField(4)
    @NonNull
    UserStyleWireFormat mUserStyle;

    /** The initial state of the complications if known, or null otherwise. */
    @ParcelField(100)
    @Nullable
    List<IdAndComplicationDataWireFormat> mIdAndComplicationDataWireFormats;

    /** Reserved field */
    @ParcelField(101)
    @Nullable
    ComponentName mAuxiliaryComponentName;

    /** Used by VersionedParcelable. */
    WallpaperInteractiveWatchFaceInstanceParams() {
    }

    public WallpaperInteractiveWatchFaceInstanceParams(
            @NonNull String instanceId,
            @NonNull DeviceConfig deviceConfig,
            @NonNull WatchUiState watchUiState,
            @NonNull UserStyleWireFormat userStyle,
            @Nullable List<IdAndComplicationDataWireFormat> idAndComplicationDataWireFormats,
            @Nullable ComponentName auxiliaryComponentName) {
        mInstanceId = instanceId;
        mDeviceConfig = deviceConfig;
        mWatchUiState = watchUiState;
        mUserStyle = userStyle;
        mIdAndComplicationDataWireFormats = idAndComplicationDataWireFormats;
        mAuxiliaryComponentName = auxiliaryComponentName;
    }

    @NonNull
    public String getInstanceId() {
        return mInstanceId;
    }

    @NonNull
    public DeviceConfig getDeviceConfig() {
        return mDeviceConfig;
    }

    @NonNull
    public WatchUiState getWatchUiState() {
        return mWatchUiState;
    }

    @NonNull
    public UserStyleWireFormat getUserStyle() {
        return mUserStyle;
    }

    public void setUserStyle(@NonNull UserStyleWireFormat userStyle) {
        mUserStyle = userStyle;
    }

    @Nullable
    public List<IdAndComplicationDataWireFormat> getIdAndComplicationDataWireFormats() {
        return mIdAndComplicationDataWireFormats;
    }

    public void setIdAndComplicationDataWireFormats(
            @Nullable List<IdAndComplicationDataWireFormat> idAndComplicationDataWireFormats
    ) {
        mIdAndComplicationDataWireFormats = idAndComplicationDataWireFormats;
    }

    /**
     * Serializes this WallpaperInteractiveWatchFaceInstanceParams to the specified {@link Parcel}.
     */
    @Override
    public void writeToParcel(@NonNull Parcel parcel, int flags) {
        parcel.writeParcelable(ParcelUtils.toParcelable(this), flags);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<WallpaperInteractiveWatchFaceInstanceParams> CREATOR =
            new Parcelable.Creator<WallpaperInteractiveWatchFaceInstanceParams>() {
                @SuppressWarnings("deprecation")
                @Override
                public WallpaperInteractiveWatchFaceInstanceParams createFromParcel(Parcel source) {
                    return ParcelUtils.fromParcelable(
                            source.readParcelable(getClass().getClassLoader()));
                }

                @Override
                public WallpaperInteractiveWatchFaceInstanceParams[] newArray(int size) {
                    return new WallpaperInteractiveWatchFaceInstanceParams[size];
                }
            };
}

@@ -2566,29 +2566,126 @@
 ble%0A    
-ComponentName
+String mAuxiliaryComponentClassName;%0A%0A    /** Reserved field */%0A    @ParcelField(102)%0A    @Nullable%0A    String
  mAuxili
@@ -2692,24 +2692,31 @@
 aryComponent
+Package
 Name;%0A%0A    /
@@ -3425,24 +3425,74 @@
 ireFormats;%0A
+        if (auxiliaryComponentName != null) %7B%0A    
         mAux
@@ -3502,24 +3502,29 @@
 aryComponent
+Class
 Name = auxil
@@ -3540,17 +3540,128 @@
 nentName
-;
+.getClassName();%0A            mAuxiliaryComponentPackageName = auxiliaryComponentName.getPackageName();%0A        %7D
 %0A    %7D%0A%0A

Remove Parcelable field from WallpaperInteractiveWatchFaceInstanceParams