package acceptance;

import com.tagmycode.plugin.Framework;
import com.tagmycode.plugin.StorageEngine;
import com.tagmycode.plugin.gui.form.LoginDialog;
import org.junit.Test;
import support.FrameworkBuilder;

import javax.swing.*;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class FrameworkAcceptanceTest extends AcceptanceTestBase {

    @Test
    public void notAuthenticatedUserShouldSeeAuthorizationDialog() throws Exception {
        Framework frameworkSpy = createSpyFramework();

        frameworkSpy.canOperate();

        assertFalse(frameworkSpy.isInitialized());
        verify(frameworkSpy, times(1)).showLoginDialog();
    }

    @Test
    public void afterLoginInitializeIsCalled() throws Exception {
        Framework framework = createFramework(createStorageEngineWithData());
        Framework frameworkSpy = spy(framework);

        final LoginDialog loginDialog = frameworkSpy.showLoginDialog();
        String verificationCode = "verification-code";
        loginDialog.getVerificationCodeTextField().setText(verificationCode);

        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                loginDialog.getButtonOk().doClick();
            }
        });
        Thread.sleep(200);

        verify(frameworkSpy, times(1)).initialize(verificationCode);
    }

    @Test
    public void afterLogoutLSetLastSnippetsUpdateIsCleared() throws Exception {
        Framework framework = createFramework(createStorageEngineWithData());
        framework.start();
        mockTagMyCodeReturningValidAccountData(framework);
        Framework frameworkSpy = spy(framework);
        assertTrue(frameworkSpy.isInitialized());
        assertDataIsValid(framework.getData());

        framework.logout();
        assertDataIsCleared(framework.getData());
    }

    @Test
    public void selectASnippetAndSeeDetailsOnRightPanel() throws Exception {
        Framework framework = createFramework(createStorageEngineWithData());
        mockTagMyCodeReturningValidAccountData(framework);
        framework.getData().setAccount(resourceGenerate.aUser());
        framework.getData().setSnippets(resourceGenerate.aSnippetCollection());
        framework.getData().saveAll();

        framework.start();

        JFrame jFrame = new JFrame();
        jFrame.add(framework.getMainWindow().getMainComponent());
        jFrame.pack();

        JPanel snippetViewFormPane = framework.getMainWindow().getSnippetsPanel().getSnippetViewFormPane();
        assertEquals("welcome view", snippetViewFormPane.getComponent(0).getName());

        framework.getMainWindow().getSnippetsPanel().getSnippetsTable().getJTable().setRowSelectionInterval(1, 1);

        assertEquals("snippet view", snippetViewFormPane.getComponent(0).getName());
    }

    @Test
    public void networkingEnabledAfterRestart() throws Exception {
        StorageEngine storage = createStorageEngine();
        storage.saveNetworkingEnabledFlag(true);
        Framework framework = createFramework(storage);
        mockTagMyCodeReturningValidAccountData(framework);

        framework.start();

        assertTrue(framework.getData().isNetworkingEnabled());
        assertTrue(framework.getMainWindow().getSnippetsPanel().getButtonNetworking().getIcon().toString().contains("/icons/connected.png"));
    }

    @Test
    public void networkingDisabledAfterRestart() throws Exception {
        StorageEngine storageEngineWithData = createStorageEngineWithData();
        storageEngineWithData.saveNetworkingEnabledFlag(false);
        final Framework framework = new FrameworkBuilder().setStorageEngine(storageEngineWithData).build();
        mockTagMyCodeReturningValidAccountData(framework);

        framework.start();

        waitForFalse(framework.getData().isNetworkingEnabled());
        waitForTrue(framework.getMainWindow().getSnippetsPanel().getButtonNetworking().getIcon().toString().contains("/icons/disconnected.png"));
    }
}
@@ -257,17 +257,70 @@
 .Assert.
-*
+assertFalse;%0Aimport static org.junit.Assert.assertTrue
 ;%0Aimport
@@ -2592,28 +2592,27 @@
         
-assertEquals
+waitForTrue
 (%22welcom
@@ -2614,26 +2614,32 @@
 elcome view%22
-, 
+.equals(
 snippetViewF
@@ -2672,16 +2672,17 @@
 tName())
+)
 ;%0A%0A     
@@ -2804,20 +2804,19 @@
     
-assertEquals
+waitForTrue
 (%22sn
@@ -2826,18 +2826,24 @@
 et view%22
-, 
+.equals(
 snippetV
@@ -2880,16 +2880,17 @@
 tName())
+)
 ;%0A    %7D%0A

Fix test