/*
 * 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.applications.assist;

import android.car.drivingstate.CarUxRestrictions;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.provider.Settings;

import androidx.annotation.VisibleForTesting;
import androidx.preference.TwoStatePreference;

import com.android.car.settings.common.FragmentController;
import com.android.car.settings.common.PreferenceController;
import com.android.internal.app.AssistUtils;

import java.util.List;

/** Common logic for preference controllers that configure the assistant's behavior. */
public abstract class AssistConfigBasePreferenceController extends
        PreferenceController<TwoStatePreference> {

    final SettingObserver mSettingObserver;
    private final AssistUtils mAssistUtils;

    public AssistConfigBasePreferenceController(Context context, String preferenceKey,
            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
        this(context, preferenceKey, fragmentController, uxRestrictions, new AssistUtils(context));
    }

    @VisibleForTesting
    AssistConfigBasePreferenceController(Context context, String preferenceKey,
            FragmentController fragmentController, CarUxRestrictions uxRestrictions,
            AssistUtils assistUtils) {
        super(context, preferenceKey, fragmentController, uxRestrictions);
        mAssistUtils = assistUtils;
        mSettingObserver = new SettingObserver(getSettingUris(), this::refreshUi);
    }

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

    @Override
    protected int getDefaultAvailabilityStatus() {
        return mAssistUtils.getAssistComponentForUser(
                UserHandle.myUserId()) != null ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    }

    @Override
    protected void onStartInternal() {
        mSettingObserver.register(getContext().getContentResolver(), true);
    }

    @Override
    protected void onStopInternal() {
        mSettingObserver.register(getContext().getContentResolver(), false);
    }

    /** Gets the Setting Uris that should be observed */
    protected abstract List<Uri> getSettingUris();

    /**
     * Creates an observer that listens for changes to {@link Settings.Secure#ASSISTANT} as well as
     * any other URI defined by {@link #getSettingUris()}.
     */
    @VisibleForTesting
    static class SettingObserver extends ContentObserver {
        private static final Uri ASSIST_URI = Settings.Secure.getUriFor(Settings.Secure.ASSISTANT);
        private final List<Uri> mUriList;
        private final Runnable mSettingChangeListener;

        SettingObserver(List<Uri> uriList, Runnable settingChangeListener) {
            super(new Handler(Looper.getMainLooper()));
            mUriList = uriList;
            mSettingChangeListener = settingChangeListener;
        }

        /** Registers or unregisters this observer to the given content resolver. */
        void register(ContentResolver cr, boolean register) {
            if (register) {
                cr.registerContentObserver(ASSIST_URI, /* notifyForDescendants= */ false,
                        /* observer= */ this);
                if (mUriList != null) {
                    for (Uri uri : mUriList) {
                        cr.registerContentObserver(uri, /* notifyForDescendants= */ false,
                                /* observer= */ this);
                    }
                }
            } else {
                cr.unregisterContentObserver(this);
            }
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            super.onChange(selfChange, uri);

            if (shouldUpdatePreference(uri)) {
                mSettingChangeListener.run();
            }
        }

        private boolean shouldUpdatePreference(Uri uri) {
            return ASSIST_URI.equals(uri) || (mUriList != null && mUriList.contains(uri));
        }
    }
}
