/*
 * Copyright (C) 2016 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.lemenzo.files;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import androidx.test.filters.LargeTest;
import androidx.test.uiautomator.UiObjectNotFoundException;

import com.lemenzo.files.files.FilesActivity;
import com.lemenzo.files.rules.TestFilesRule;
import com.lemenzo.files.util.VersionUtils;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

@LargeTest
public class RenameDocumentUiTest extends ActivityTestJunit4<FilesActivity> {

    private final String newName = "kitties.log";

    @Rule
    public final TestFilesRule mTestFilesRule = new TestFilesRule();

    @Before
    public void setUpTest() {
        bots.roots.closeDrawer();
    }

    @Test
    public void testRenameEnabled_SingleSelection() throws Exception {
        bots.directory.selectDocument(TestFilesRule.FILE_NAME_1, 1);
        bots.main.openOverflowMenu();
        bots.main.assertMenuEnabled(R.string.menu_rename, true);

        // Dismiss more options window
        device.pressBack();
    }

    @Test
    public void testNoRenameSupport_SingleSelection() throws Exception {
        if (VersionUtils.isAtLeastR()) {
            bots.directory.selectDocument(TestFilesRule.FILE_NAME_NO_RENAME, 1);
            bots.main.openOverflowMenu();
            bots.main.assertMenuEnabled(R.string.menu_rename, false);

            // Dismiss more options window
            device.pressBack();
        }
    }

    @Test
    public void testOneHasRenameSupport_MultipleSelection() throws Exception {
        if (VersionUtils.isAtLeastR()) {
            bots.directory.selectDocument(TestFilesRule.FILE_NAME_1, 1);
            bots.directory.selectDocument(TestFilesRule.FILE_NAME_NO_RENAME, 2);
            bots.main.openOverflowMenu();
            bots.main.assertMenuEnabled(R.string.menu_rename, false);

            // Dismiss more options window
            device.pressBack();
        }
    }

    @Test
    public void testRenameDisabled_MultipleSelection() throws Exception {
        if (VersionUtils.isAtLeastR()) {
            bots.directory.selectDocument(TestFilesRule.FILE_NAME_1, 1);
            bots.directory.selectDocument(TestFilesRule.FILE_NAME_2, 2);
            bots.main.openOverflowMenu();
            bots.main.assertMenuEnabled(R.string.menu_rename, false);

            // Dismiss more options window
            device.pressBack();
        }
    }

    @Test
    public void testRenameFile_OkButton() throws Exception {
        bots.directory.selectDocument(TestFilesRule.FILE_NAME_1, 1);

        clickRename();

        device.waitForIdle();
        bots.main.setDialogText(newName);

        device.waitForIdle();
        bots.main.clickDialogOkButton(/* closeSoftKeyboard */ true);

        bots.directory.waitForDocument(newName);
        bots.directory.assertDocumentsAbsent(TestFilesRule.FILE_NAME_1);
        bots.directory.assertDocumentsCount(4);
    }

    @Test
    public void testRenameFile_Enter() throws Exception {
        bots.directory.selectDocument(TestFilesRule.FILE_NAME_1, 1);

        clickRename();

        device.waitForIdle();
        bots.main.setDialogText(newName);

        device.waitForIdle();
        bots.keyboard.pressEnter();

        bots.directory.waitForDocument(newName);
        bots.directory.assertDocumentsAbsent(TestFilesRule.FILE_NAME_1);
        bots.directory.assertDocumentsCount(4);
    }

    @Test
    public void testRenameWithoutChangeIsNoOp() throws Exception {
        bots.directory.selectDocument(TestFilesRule.FILE_NAME_1, 1);

        clickRename();

        device.waitForIdle();
        bots.keyboard.pressEnter();

        bots.directory.waitForDocument(TestFilesRule.FILE_NAME_1);
        bots.directory.assertDocumentsCount(4);
    }

    @Test
    public void testRenameFile_Cancel() throws Exception {
        bots.directory.selectDocument(TestFilesRule.FILE_NAME_1, 1);

        clickRename();

        bots.main.setDialogText(newName);

        bots.main.clickDialogCancelButton(/* closeSoftKeyboard */ true);

        bots.directory.assertDocumentsVisible(TestFilesRule.FILE_NAME_1);
        bots.directory.assertDocumentsAbsent(newName);
        bots.directory.assertDocumentsCount(4);

        bots.directory.assertSelection(1);
    }

    @Test
    public void testRenameDir() throws Exception {
        String oldName = "Dir1";
        String newName = "Dir123";
        bots.directory.selectDocument(oldName, 1);

        clickRename();

        bots.main.setDialogText(newName);

        bots.keyboard.pressEnter();

        bots.directory.assertDocumentsAbsent(oldName);
        bots.directory.assertDocumentsVisible(newName);
        bots.directory.assertDocumentsCount(4);
    }

    @Test
    public void testRename_NameExists() throws Exception {
        renameWithConflict();

        bots.main.clickDialogCancelButton(/* closeSoftKeyboard */ true);

        bots.directory.assertDocumentsVisible(TestFilesRule.FILE_NAME_1);
        bots.directory.assertDocumentsVisible(TestFilesRule.FILE_NAME_2);
        bots.directory.assertDocumentsCount(4);
    }

    @Test
    public void testRename_RecoverAfterConflict() throws Exception {
        renameWithConflict();
        device.waitForIdle();

        bots.main.setDialogText(newName);

        device.waitForIdle();
        bots.main.clickDialogOkButton(/* closeSoftKeyboard */ true);

        bots.directory.waitForDocument(newName);
        bots.directory.assertDocumentsAbsent(TestFilesRule.FILE_NAME_1);
        bots.directory.assertDocumentsCount(4);
    }

    private void renameWithConflict() throws Exception {
        // Check that document with the new name exists
        bots.directory.assertDocumentsVisible(TestFilesRule.FILE_NAME_2);
        bots.directory.selectDocument(TestFilesRule.FILE_NAME_1, 1);

        clickRename();

        bots.main.assertDialogText(TestFilesRule.FILE_NAME_1);
        assertFalse(bots.main.findRenameErrorMessage().exists());
        bots.main.setDialogText(TestFilesRule.FILE_NAME_2);
        bots.keyboard.pressEnter();
        assertTrue(bots.main.findRenameErrorMessage().exists());
    }

    private void clickRename() throws UiObjectNotFoundException {
        if (!bots.main.waitForActionModeBarToAppear()) {
            throw new UiObjectNotFoundException("ActionMode bar not found");
        }
        bots.main.clickActionbarOverflowItem("Rename");
        device.waitForIdle();
    }
}