package com.android.settings.applications;

import android.content.Context;
import android.content.pm.GosPackageState;
import android.ext.settings.app.AppSwitch;

import androidx.appcompat.app.AlertDialog;

import com.android.settings.R;

public abstract class AswExploitProtectionFragment<T extends AppSwitch> extends AswAppInfoFragment<T> {

    @Override
    protected void completeStateChange(int newEntryId, boolean curValue, Runnable stateChangeAction) {
        Context ctx = requireContext();

        boolean showWarning = false;
        if (curValue) {
            if (newEntryId == ID_OFF) {
                showWarning = true;
            } else if (newEntryId == ID_DEFAULT) {
                AppSwitch asw = adapter.getAppSwitch();
                int userId = mUserId;
                var ps = GosPackageState.get(mPackageName, userId);
                showWarning = !asw.getDefaultValue(ctx, userId, getAppInfo(), ps);
            }
        }
        if (showWarning) {
            var d = getExploitProtectionWarningOnDisable(ctx, stateChangeAction);
            d.show();
        } else {
            stateChangeAction.run();
        }
    }

    public static AlertDialog.Builder getExploitProtectionWarningOnDisable(Context ctx, Runnable action) {
        var b = new AlertDialog.Builder(ctx);
        b.setTitle(R.string.aep_confirm_disable_title);
        b.setMessage(R.string.aep_confirm_disable_warning_msg);
        b.setNegativeButton(R.string.cancel, null);
        b.setPositiveButton(R.string.aep_confirm_disable_proceed_btn, (d, w) -> action.run());
        return b;
    }
}
