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

import static java.lang.annotation.RetentionPolicy.SOURCE;

import android.annotation.IntDef;
import android.annotation.PluralsRes;
import android.annotation.StringRes;
import android.content.Context;
import android.content.res.Resources;

import com.android.settings.R;
import com.android.settingslib.utils.StringUtil;

import java.lang.annotation.Retention;
import java.util.HashMap;
import java.util.Map;

/** Provides utility methods related auto click. */
public final class AutoclickUtils {

    /** Used for autoclick mode in the preferences editor. */
    static final String KEY_DELAY_MODE = "delay_mode";

    /** Used for autoclick custom delay in the preferences editor. */
    static final String KEY_CUSTOM_DELAY_VALUE = "custom_delay_value";

    /** Min allowed autoclick delay value. */
    static final int MIN_AUTOCLICK_DELAY_MS = 200;

    /** Max allowed autoclick delay value. */
    static final int MAX_AUTOCLICK_DELAY_MS = 1000;

    /**
     * Allowed autoclick delay values are discrete. This is the difference between two allowed
     * values.
     */
    static final int AUTOCLICK_DELAY_STEP = 100;

    @Retention(SOURCE)
    @IntDef({
        Quantity.ONE,
        Quantity.FEW
    })
    private @interface Quantity {
        int ONE = 1;
        int FEW = 3;
    }

    /**
     * Gets string that should be used for provided autoclick delay.
     *
     * @param context context from which string should be retrieved.
     * @param id The desired resource identifier, as generated by the aapt
     *           tool. This integer encodes the package, type, and resource
     *           entry. The value 0 is an invalid identifier.
     * @param delayMillis Delay for whose value summary should be retrieved.
     */
    public static CharSequence getAutoclickDelaySummary(Context context,
                                                        @StringRes int id, int delayMillis) {
        final int quantity = (delayMillis == 1000) ? Quantity.ONE : Quantity.FEW;
        final float delaySecond =  (float) delayMillis / 1000;
        // Only show integer when delay time is 1.
        final String decimalFormat = (delaySecond == 1) ? "%.0f" : "%.1f";

        Map<String, Object> arguments = new HashMap<>();
        arguments.put("count", quantity);
        arguments.put("time", String.format(decimalFormat, delaySecond));
        return StringUtil.getIcuPluralsString(context, arguments, id);
    }

    private AutoclickUtils(){}
}
