/*
 * Copyright 2021 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.car.app.model.signin;

import static java.util.Objects.requireNonNull;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.car.app.annotations.RequiresCarApi;
import androidx.car.app.model.CarText;

import java.util.Objects;

/**
 * A {@link SignInTemplate.SignInMethod} that presents a PIN or activation code that the user can
 * use to sign-in.
 */
@RequiresCarApi(2)
public final class PinSignInMethod implements SignInTemplate.SignInMethod {
    /** Maximum length, in characters, for a PIN. */
    private static final int MAX_PIN_LENGTH = 12;

    @Keep
    @Nullable
    private final CarText mPinCode;

    // TODO(b/189881361): this field is kept around for the alpha01 release to avoid breaking apps.
    // Remove once we are in beta and deem safe.
    @Keep
    @Nullable
    private final String mPin;

    /**
     * Returns a {@link PinSignInMethod} instance.
     *
     * <h4>Requirements</h4>
     *
     * <p>The provided pin must be no more than 12 characters long. To facilitate typing this
     * code, it is recommended restricting the string to a limited set (for example, numbers,
     * upper-case letters, hexadecimal, etc.).
     *
     * <p>Spans are not supported in the pin and will be ignored.
     *
     * @param pinCode the PIN to display is empty.
     * @throws IllegalArgumentException if {@code pin} is empty or longer than 12 characters.
     * @throws NullPointerException     if {@code pin} is {@code null}
     */
    // TODO(b/183750545): check that no spans are present in the input pin.
    public PinSignInMethod(@NonNull CharSequence pinCode) {
        int pinLength = requireNonNull(pinCode).length();
        if (pinLength == 0) {
            throw new IllegalArgumentException("PIN must not be empty");
        }
        if (pinLength > MAX_PIN_LENGTH) {
            throw new IllegalArgumentException(
                    "PIN must not be longer than " + MAX_PIN_LENGTH + " characters");
        }
        mPinCode = CarText.create(pinCode);
        mPin = mPinCode.toString();
    }

    /** Returns the PIN or activation code to present to the user. */
    @NonNull
    public CarText getPinCode() {
        if (mPinCode != null) {
            // For apps that uses a newer version of the library, this field should always be set.
            return mPinCode;
        }

        // Fallback to the String value for older clients.
        return CarText.create(requireNonNull(mPin));
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }

        if (!(other instanceof PinSignInMethod)) {
            return false;
        }

        PinSignInMethod that = (PinSignInMethod) other;
        return Objects.equals(mPinCode, that.mPinCode);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mPinCode);
    }

    /** Constructs an empty instance, used by serialization code. */
    private PinSignInMethod() {
        mPinCode = null;
        mPin = null;
    }
}

@@ -1313,221 +1313,16 @@
 e;%0A%0A
-    // TODO(b/189881361): this field is kept around for the alpha01 release to avoid breaking apps.%0A    // Remove once we are in beta and deem safe.%0A    @Keep%0A    @Nullable%0A    private final String mPin;%0A%0A
     /**%0A
     
@@ -1317,16 +1317,16 @@
     /**%0A
+
      * R
@@ -2491,44 +2491,8 @@
 e);%0A
-        mPin = mPinCode.toString();%0A
     
@@ -2623,260 +2623,15 @@
     
-if (mPinCode != null) %7B%0A            // For apps that uses a newer version of the library, this field should always be set.%0A            return mPinCode;%0A        %7D%0A%0A        // Fallback to the String value for older clients.%0A        return CarText.create(
+return 
 requ
@@ -2645,17 +2645,20 @@
 ull(mPin
-)
+Code
 );%0A    %7D
@@ -3172,24 +3172,24 @@
 nMethod() %7B%0A
+
         mPin
@@ -3205,29 +3205,8 @@
 ll;%0A
-        mPin = null;%0A
     

Remove obsolete PinSignInMethod#mPin field