/*
 * Copyright (C) 2025 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 android.bluetooth;

import android.annotation.FlaggedApi;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.RequiresNoPermission;
import android.bluetooth.BluetoothDevice.EncryptionAlgorithm;
import android.os.Parcel;
import android.os.Parcelable;

import com.android.bluetooth.flags.Flags;

/**
 * Represents the encryption status of a Bluetooth device.
 *
 * <p>This class is used to hold the encryption status details like key size and algorithm of a
 * Bluetooth device.
 */
@FlaggedApi(Flags.FLAG_LINK_STATUS_API)
public final class EncryptionStatus {
    private final InnerParcel mParcel;

    public EncryptionStatus(
            @IntRange(from = 1, to = 16) int keySize, @EncryptionAlgorithm int algorithm) {
        mParcel = new InnerParcel(keySize, algorithm);
    }

    private EncryptionStatus(InnerParcel p) {
        mParcel = p;
    }

    /** @hide */
    @RequiresNoPermission
    public InnerParcel getParcel() {
        return mParcel;
    }

    /**
     * @return the size of the encryption key, in number of bytes. i.e. value of 16 means 16-octets,
     *     or 128 bit key size.
     */
    @RequiresNoPermission
    public @IntRange(from = 1, to = 16) int getKeySize() {
        return mParcel.mKeySize;
    }

    /**
     * @return the encryption algorithm used for the encrypting the link.
     */
    @RequiresNoPermission
    public @EncryptionAlgorithm int getAlgorithm() {
        return mParcel.mAlgorithm;
    }

    @Override
    public String toString() {
        return "EncryptionStatus{keySize="
                + mParcel.mKeySize
                + ", algorithm="
                + mParcel.mAlgorithm
                + "}";
    }

    /** @hide */
    public static final class InnerParcel implements Parcelable {
        private final int mKeySize;
        private final int mAlgorithm;

        private InnerParcel(@NonNull Parcel in) {
            this(in.readInt(), in.readInt());
        }

        public InnerParcel(int keySize, int algorithm) {
            mKeySize = keySize;
            mAlgorithm = algorithm;
        }

        /**
         * @return the {@link EncryptionStatus} associated with this parcel
         */
        @FlaggedApi(Flags.FLAG_LINK_STATUS_API)
        @RequiresNoPermission
        public @NonNull EncryptionStatus toEncryptionStatus() {
            return new EncryptionStatus(this);
        }

        @Override
        public void writeToParcel(@NonNull Parcel out, int flags) {
            out.writeInt(mKeySize);
            out.writeInt(mAlgorithm);
        }

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

        /** {@link Parcelable.Creator} interface implementation. */
        public static final @NonNull Parcelable.Creator<InnerParcel> CREATOR =
                new Parcelable.Creator<InnerParcel>() {
                    public @NonNull InnerParcel createFromParcel(Parcel in) {
                        return new InnerParcel(in);
                    }

                    public @NonNull InnerParcel[] newArray(int size) {
                        return new InnerParcel[size];
                    }
                };
    }
}
