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

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Utility class for Telecom related methods.
 */
public class TelecomUtils {

    public static final String HFP_CLIENT_CONNECTION_SERVICE_CLASS_NAME =
            "com.android.bluetooth.hfpclient.connserv.HfpClientConnectionService";

    private TelecomManager mTelecomManager;
    private BluetoothAdapter mBluetoothAdapter;

    public TelecomUtils(Context context) {
        mTelecomManager = context.getSystemService(TelecomManager.class);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    /** Returns the list of hfp {@link BluetoothDevice}s for current callable phone accounts. */
    @NonNull
    public List<BluetoothDevice> getHfpDeviceList() {
        List<PhoneAccountHandle> phoneAccountHandles =
                mTelecomManager.getCallCapablePhoneAccounts(true);
        List<BluetoothDevice> hfpDeviceList = new ArrayList<>();
        for (PhoneAccountHandle phoneAccountHandle : phoneAccountHandles) {
            BluetoothDevice bluetoothDevice = getMatchingDevice(phoneAccountHandle);
            if (bluetoothDevice != null) {
                hfpDeviceList.add(bluetoothDevice);
            }
        }
        return hfpDeviceList;
    }

    /**
     * Returns the {@link BluetoothDevice} for the given {@link PhoneAccountHandle} if the account
     * is for hfp connection.
     */
    public BluetoothDevice getMatchingDevice(
            @Nullable PhoneAccountHandle phoneAccountHandle) {
        Set<BluetoothDevice> bondedDevices =
                mBluetoothAdapter == null ? null : mBluetoothAdapter.getBondedDevices();
        if (bondedDevices == null) {
            return null;
        }
        if (isHfpConnectionService(phoneAccountHandle)) {
            for (BluetoothDevice bluetoothDevice : bondedDevices) {
                if (TextUtils.equals(bluetoothDevice.getAddress(), phoneAccountHandle.getId())) {
                    return bluetoothDevice;
                }
            }
        }
        return null;
    }

    /** Returns if the {@link PhoneAccountHandle} is from a hfp connection for bluetooth call. */
    public boolean isHfpConnectionService(@Nullable PhoneAccountHandle phoneAccountHandle) {
        return phoneAccountHandle != null
                && HFP_CLIENT_CONNECTION_SERVICE_CLASS_NAME.equals(
                phoneAccountHandle.getComponentName().getClassName());
    }
}
