/*
 * Copyright (C) 2008 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;

import static com.android.settings.core.FeatureFlags.DEVICE_INFO_V2;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.telephony.TelephonyManager;
import android.util.FeatureFlagUtils;
import android.support.annotation.VisibleForTesting;

import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.deviceinfo.BasebandVersionPreferenceController;
import com.android.settings.deviceinfo.BluetoothAddressPreferenceController;
import com.android.settings.deviceinfo.BuildNumberPreferenceController;
import com.android.settings.deviceinfo.DeviceModelPreferenceController;
import com.android.settings.deviceinfo.FccEquipmentIdPreferenceController;
import com.android.settings.deviceinfo.FeedbackPreferenceController;
import com.android.settings.deviceinfo.FirmwareVersionPreferenceController;
import com.android.settings.deviceinfo.ImsStatusPreferenceController;
import com.android.settings.deviceinfo.IpAddressPreferenceController;
import com.android.settings.deviceinfo.KernelVersionPreferenceController;
import com.android.settings.deviceinfo.ManualPreferenceController;
import com.android.settings.deviceinfo.PhoneNumberPreferenceController;
import com.android.settings.deviceinfo.RegulatoryInfoPreferenceController;
import com.android.settings.deviceinfo.SafetyInfoPreferenceController;
import com.android.settings.deviceinfo.SecurityPatchPreferenceController;
import com.android.settings.deviceinfo.WifiMacAddressPreferenceController;
import com.android.settings.deviceinfo.firmwareversion.FirmwareVersionPreferenceControllerV2;
import com.android.settings.deviceinfo.imei.ImeiInfoPreferenceControllerV2;
import com.android.settings.deviceinfo.simstatus.SimStatusPreferenceControllerV2;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;

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

public class DeviceInfoSettings extends DashboardFragment implements Indexable {

    private static final String LOG_TAG = "DeviceInfoSettings";

    private static final String KEY_LEGAL_CONTAINER = "legal_container";

    @VisibleForTesting
    static final int SIM_PREFERENCES_COUNT = 3;
    @VisibleForTesting
    static final int NON_SIM_PREFERENCES_COUNT = 2;

    @Override
    public int getMetricsCategory() {
        return MetricsEvent.DEVICEINFO;
    }

    @Override
    public int getHelpResource() {
        return R.string.help_uri_about;
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        final Bundle arguments = getArguments();
        if (FeatureFlagUtils.isEnabled(getContext(), DEVICE_INFO_V2)) {
            // Do not override initial expand children count if we come from
            // search (EXTRA_FRAGMENT_ARG_KEY is set) - we need to display every if entry point
            // is search.
            if (arguments == null
                    || !arguments.containsKey(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY)) {

                // Increase the number of children when the device contains more than 1 sim.
                final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(
                        Context.TELEPHONY_SERVICE);
                final int numberOfChildren = Math.max(SIM_PREFERENCES_COUNT,
                        SIM_PREFERENCES_COUNT * telephonyManager.getPhoneCount())
                        + NON_SIM_PREFERENCES_COUNT;
                getPreferenceScreen().setInitialExpandedChildrenCount(numberOfChildren);
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        final BuildNumberPreferenceController buildNumberPreferenceController =
                getPreferenceController(BuildNumberPreferenceController.class);
        if (buildNumberPreferenceController.onActivityResult(requestCode, resultCode, data)) {
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected String getLogTag() {
        return LOG_TAG;
    }

    @Override
    protected int getPreferenceScreenResId() {
        return FeatureFlagUtils.isEnabled(getContext(), DEVICE_INFO_V2)
                ? R.xml.device_info_settings_v2 : R.xml.device_info_settings;
    }

    @Override
    protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
        return buildPreferenceControllers(context, getActivity(), this /* fragment */,
                getLifecycle());
    }

    private static class SummaryProvider implements SummaryLoader.SummaryProvider {

        private final SummaryLoader mSummaryLoader;

        public SummaryProvider(SummaryLoader summaryLoader) {
            mSummaryLoader = summaryLoader;
        }

        @Override
        public void setListening(boolean listening) {
            if (listening) {
                mSummaryLoader.setSummary(this, DeviceModelPreferenceController.getDeviceModel());
            }
        }
    }

    public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
            = new SummaryLoader.SummaryProviderFactory() {
        @Override
        public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
                SummaryLoader summaryLoader) {
            return new SummaryProvider(summaryLoader);
        }
    };

    private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
            Activity activity, Fragment fragment, Lifecycle lifecycle) {
        if (FeatureFlagUtils.isEnabled(context, DEVICE_INFO_V2)) {
            final List<AbstractPreferenceController> controllers = new ArrayList<>();
            // Device name

            controllers.add(new PhoneNumberPreferenceController(context));

            controllers.add(new SimStatusPreferenceControllerV2(context, fragment));

            controllers.add(new DeviceModelPreferenceController(context, fragment));

            controllers.add(new ImeiInfoPreferenceControllerV2(context, fragment));

            controllers.add(new FirmwareVersionPreferenceControllerV2(context, fragment));

            controllers.add(new ImsStatusPreferenceController(context, lifecycle));

            controllers.add(new IpAddressPreferenceController(context, lifecycle));

            controllers.add(new WifiMacAddressPreferenceController(context, lifecycle));

            controllers.add(new BluetoothAddressPreferenceController(context, lifecycle));

            controllers.add(new RegulatoryInfoPreferenceController(context));

            controllers.add(new SafetyInfoPreferenceController(context));

            controllers.add(new ManualPreferenceController(context));

            controllers.add(new FeedbackPreferenceController(fragment, context));

            controllers.add(new FccEquipmentIdPreferenceController(context));

            controllers.add(
                    new BuildNumberPreferenceController(context, activity, fragment, lifecycle));

            return controllers;
        }

        final List<AbstractPreferenceController> controllers = new ArrayList<>();
        controllers.add(
                new BuildNumberPreferenceController(context, activity, fragment, lifecycle));
        controllers.add(new ManualPreferenceController(context));
        controllers.add(new FeedbackPreferenceController(fragment, context));
        controllers.add(new KernelVersionPreferenceController(context));
        controllers.add(new BasebandVersionPreferenceController(context));
        controllers.add(new FirmwareVersionPreferenceController(context, lifecycle));
        controllers.add(new RegulatoryInfoPreferenceController(context));
        controllers.add(new DeviceModelPreferenceController(context, fragment));
        controllers.add(new SecurityPatchPreferenceController(context));
        controllers.add(new FccEquipmentIdPreferenceController(context));
        controllers.add(new SafetyInfoPreferenceController(context));
        return controllers;
    }

    /**
     * For Search.
     */
    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider() {

                @Override
                public List<SearchIndexableResource> getXmlResourcesToIndex(
                        Context context, boolean enabled) {
                    final SearchIndexableResource sir = new SearchIndexableResource(context);
                    sir.xmlResId = FeatureFlagUtils.isEnabled(context, DEVICE_INFO_V2)
                            ? R.xml.device_info_settings_v2 : R.xml.device_info_settings;
                    return Arrays.asList(sir);
                }

                @Override
                public List<AbstractPreferenceController> getPreferenceControllers(
                        Context context) {
                    return buildPreferenceControllers(context, null /*activity */,
                            null /* fragment */, null /* lifecycle */);
                }

                @Override
                public List<String> getNonIndexableKeys(Context context) {
                    List<String> keys = super.getNonIndexableKeys(context);
                    keys.add(KEY_LEGAL_CONTAINER);
                    return keys;
                }
            };
}
