/*
 * Copyright (C) 2021 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.systemui.statusbar.notification.row;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.Notification;
import android.content.Context;
import android.testing.TestableLooper.RunWithLooper;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.android.systemui.SysuiTestCase;
import com.android.systemui.kosmos.KosmosJavaAdapter;
import com.android.systemui.plugins.statusbar.NotificationMenuRowPlugin;
import com.android.systemui.res.R;
import com.android.systemui.shade.ShadeController;
import com.android.systemui.statusbar.notification.collection.EntryAdapter;
import com.android.systemui.statusbar.notification.collection.NotificationEntry;
import com.android.systemui.statusbar.notification.headsup.PinnedStatus;
import com.android.systemui.statusbar.notification.logging.NotificationPanelLogger;
import com.android.systemui.statusbar.notification.shared.NotificationBundleUi;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@SmallTest
@RunWith(AndroidJUnit4.class)
@RunWithLooper
public class ExpandableNotificationRowDragControllerTest extends SysuiTestCase {

    private ExpandableNotificationRow mRow;
    private ExpandableNotificationRowDragController mController;
    private NotificationMenuRow mMenuRow = mock(NotificationMenuRow.class);
    private NotificationMenuRowPlugin.MenuItem mMenuItem =
            mock(NotificationMenuRowPlugin.MenuItem.class);
    private ShadeController mShadeController = mock(ShadeController.class);
    private NotificationPanelLogger mNotificationPanelLogger = mock(NotificationPanelLogger.class);

    private final KosmosJavaAdapter mKosmos = new KosmosJavaAdapter(this);

    @Before
    public void setUp() throws Exception {
        allowTestableLooperAsMainThread();

        mDependency.injectMockDependency(ShadeController.class);
        mRow = spy(mKosmos.createRow());
        doReturn(true).when(mRow).startDragAndDrop(any(), any(), any(), anyInt());
        when(mMenuRow.getLongpressMenuItem(any(Context.class))).thenReturn(mMenuItem);

        mController = new ExpandableNotificationRowDragController(mContext,
                mKosmos.getMockHeadsUpManager(), mShadeController, mNotificationPanelLogger);
    }

    @Test
    public void testDoStartDragHeadsUpNotif_startDragAndDrop() throws Exception {
        ExpandableNotificationRowDragController controller = createSpyController();
        mRow.setDragController(controller);
        mRow.setHeadsUp(true);
        mRow.setPinnedStatus(PinnedStatus.PinnedBySystem);

        mRow.doLongClickCallback(0, 0);
        mRow.doDragCallback(0, 0);
        verify(controller).startDragAndDrop(mRow);
        verify(mKosmos.getMockHeadsUpManager(), times(1)).releaseAllImmediately();
        if (NotificationBundleUi.isEnabled()) {
            verify(mNotificationPanelLogger, times(1))
                    .logNotificationDrag(any(EntryAdapter.class));
        } else {
            verify(mNotificationPanelLogger, times(1))
                    .logNotificationDrag(any(NotificationEntry.class));
        }
    }

    @Test
    public void testDoStartDragNotif() throws Exception {
        ExpandableNotificationRowDragController controller = createSpyController();
        mRow.setDragController(controller);

        mRow.doDragCallback(0, 0);
        verify(controller).startDragAndDrop(mRow);
        verify(mShadeController).animateCollapseShade(eq(0), eq(true),
                eq(false), anyFloat());
        if (NotificationBundleUi.isEnabled()) {
            verify(mNotificationPanelLogger, times(1))
                    .logNotificationDrag(any(EntryAdapter.class));
        } else {
            verify(mNotificationPanelLogger, times(1))
                    .logNotificationDrag(any(NotificationEntry.class));
        }
    }

    @Test
    public void testDoStartDrag_noLaunchIntent() throws Exception {
        mRow = spy(mKosmos.createRow(new Notification.Builder(mContext, "channel")
                .setSmallIcon(R.drawable.ic_person)
                .build()));
        ExpandableNotificationRowDragController controller = createSpyController();
        mRow.setDragController(controller);

        mRow.doDragCallback(0, 0);
        verify(controller).startDragAndDrop(mRow);

        // Verify that we never start the actual drag since there is no content
        verify(mRow, never()).startDragAndDrop(any(), any(), any(), anyInt());
        if (NotificationBundleUi.isEnabled()) {
            verify(mNotificationPanelLogger, never())
                    .logNotificationDrag(any(EntryAdapter.class));
        } else {
            verify(mNotificationPanelLogger, never())
                    .logNotificationDrag(any(NotificationEntry.class));
        }
    }

    @Test
    public void testCleanUpDragListenerOnDragFailed() {
        ExpandableNotificationRowDragController controller = createSpyController();
        mRow.setDragController(controller);
        mRow.setHeadsUp(true);
        mRow.setPinnedStatus(PinnedStatus.PinnedBySystem);

        // Simulate a failure to initiate drag and drop
        doReturn(false).when(mRow).startDragAndDrop(any(), any(), any(), anyInt());

        mRow.doLongClickCallback(0, 0);
        mRow.doDragCallback(0, 0);

        // Verify that we've reset the listener
        verify(mRow).setOnDragListener(null);
    }

    private ExpandableNotificationRowDragController createSpyController() {
        return spy(mController);
    }
}
