/*
 * Copyright (C) 2019 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.settings.inputmethod;

import android.app.admin.DevicePolicyManager;
import android.car.drivingstate.CarUxRestrictions;
import android.content.Context;
import android.content.pm.PackageManager;
import android.icu.text.ListFormatter;
import android.text.BidiFormatter;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;

import androidx.preference.Preference;

import com.android.car.settings.common.FragmentController;
import com.android.car.settings.common.PreferenceController;

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

/** Updates the keyboard settings entry summary with the currently enabled keyboard. */
public class KeyboardPreferenceController extends PreferenceController<Preference> {
    private static final String SUMMARY_EMPTY = "";

    private final InputMethodManager mInputMethodManager;
    private final DevicePolicyManager mDevicePolicyManager;
    private final PackageManager mPackageManager;

    public KeyboardPreferenceController(Context context, String preferenceKey,
            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
        super(context, preferenceKey, fragmentController, uxRestrictions);
        mPackageManager = context.getPackageManager();
        mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
        mInputMethodManager = context.getSystemService(InputMethodManager.class);
    }

    @Override
    protected Class<Preference> getPreferenceType() {
        return Preference.class;
    }

    @Override
    protected void updateState(Preference preference) {
        List<InputMethodInfo> inputMethodInfos =
                InputMethodUtil.getPermittedAndEnabledInputMethodList(
                    mInputMethodManager, mDevicePolicyManager);
        if (inputMethodInfos == null) {
            preference.setSummary(SUMMARY_EMPTY);
            return;
        }

        List<String> labels = new ArrayList<>();
        for (InputMethodInfo inputMethodInfo : inputMethodInfos) {
            labels.add(InputMethodUtil.getPackageLabel(mPackageManager, inputMethodInfo));
        }
        if (labels.isEmpty()) {
            preference.setSummary(SUMMARY_EMPTY);
            return;
        }

        BidiFormatter bidiFormatter = BidiFormatter.getInstance();

        List<String> summaries = new ArrayList<>();
        for (String label : labels) {
            summaries.add(bidiFormatter.unicodeWrap(label));
        }
        preference.setSummary(ListFormatter.getInstance().format(summaries));
    }
}
