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

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.car.drivingstate.CarUxRestrictions;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.SoftApConfiguration;

import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.preference.Preference;

import com.android.car.settings.common.FragmentController;
import com.android.car.settings.common.PreferenceControllerTestHelper;
import com.android.car.settings.common.ValidatedEditTextPreference;
import com.android.car.settings.testutils.ShadowCarWifiManager;
import com.android.car.settings.testutils.ShadowLocalBroadcastManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowCarWifiManager.class, ShadowLocalBroadcastManager.class})
public class WifiTetherBasePreferenceControllerTest {

    private static final String SUMMARY = "SUMMARY";
    private static final String DEFAULT_SUMMARY = "DEFAULT_SUMMARY";

    private static class TestWifiTetherBasePreferenceController extends
            WifiTetherBasePreferenceController<Preference> {

        private String mSummary;
        private String mDefaultSummary;

        TestWifiTetherBasePreferenceController(Context context, String preferenceKey,
                FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
            super(context, preferenceKey, fragmentController, uxRestrictions);
        }

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

        @Override
        protected String getSummary() {
            return mSummary;
        }

        @Override
        protected String getDefaultSummary() {
            return mDefaultSummary;
        }

        protected void setConfigSummaries(@Nullable String summary,
                @Nullable String defaultSummary) {
            mSummary = summary;
            mDefaultSummary = defaultSummary;
        }
    }

    private Context mContext;
    private ValidatedEditTextPreference mPreference;
    private PreferenceControllerTestHelper<TestWifiTetherBasePreferenceController>
            mControllerHelper;
    private TestWifiTetherBasePreferenceController mController;

    @Before
    public void setup() {
        mContext = RuntimeEnvironment.application;
        mPreference = new ValidatedEditTextPreference(mContext);
        mControllerHelper =
                new PreferenceControllerTestHelper<TestWifiTetherBasePreferenceController>(mContext,
                        TestWifiTetherBasePreferenceController.class, mPreference);
        mController = mControllerHelper.getController();
        when(mControllerHelper.getMockFragmentController().getSettingsLifecycle())
                .thenReturn(mock(Lifecycle.class));
    }

    @After
    public void tearDown() {
        ShadowCarWifiManager.reset();
    }

    @Test
    public void noSummaryToShow_defaultSummarySet_shouldShowDefaultSummary() {
        mController.setConfigSummaries(/* summary= */ null, DEFAULT_SUMMARY);
        mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
        mController.updateState(mPreference);

        assertThat(mPreference.getSummary()).isEqualTo(DEFAULT_SUMMARY);
    }

    @Test
    public void noSummaryToShow_defaultSummaryNotSet_shouldNotShowSummary() {
        mController.setConfigSummaries(/* summary= */ null, /* defaultSummary= */ null);
        mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
        mController.updateState(mPreference);

        assertThat(mPreference.getSummary()).isEqualTo(null);
    }

    @Test
    public void summaryToShow_defaultSummarySet_shouldShowNonDefaultSummary() {
        mController.setConfigSummaries(SUMMARY, DEFAULT_SUMMARY);
        mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
        mController.updateState(mPreference);

        assertThat(mPreference.getSummary()).isEqualTo(SUMMARY);
    }

    @Test
    public void summaryToShow_defaultSummaryNotSet_shouldShowNonDefaultSummary() {
        mController.setConfigSummaries(SUMMARY, /* defaultSummary= */ null);
        mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
        mController.updateState(mPreference);

        assertThat(mPreference.getSummary()).isEqualTo(SUMMARY);
    }

    @Test
    public void onSetWifiTetherConfig_requestsWifiTetherRestart() {
        mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
        SoftApConfiguration config = new SoftApConfiguration.Builder().build();
        mController.setCarSoftApConfig(config);

        Intent expectedIntent = new Intent(
                WifiTetherBasePreferenceController.ACTION_RESTART_WIFI_TETHERING);

        assertThat(
                ShadowLocalBroadcastManager.getSentBroadcastIntents().get(0).toString())
                .isEqualTo(expectedIntent.toString());
    }

    private ShadowCarWifiManager getShadowCarWifiManager() {
        return Shadow.extract(new CarWifiManager(mContext, mock(Lifecycle.class)));
    }
}
