package net.rdrei.android.dirchooser;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowAlertDialog;
import org.robolectric.shadows.ShadowDialog;

import static org.assertj.android.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

@RunWith(RobolectricGradleTestRunner.class)
public class DirectoryChooserFragmentTest {

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static void startFragment(@NonNull Fragment fragment, @Nullable Class activityClass) {
        if (activityClass == null) {
            activityClass = Activity.class;
        }

        Activity activity = (Activity) Robolectric.buildActivity(activityClass)
                .create()
                .start()
                .resume()
                .get();

        FragmentManager fragmentManager = activity.getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.main, fragment);
        fragmentTransaction.commit();

        fragmentManager.executePendingTransactions();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Test
    public void testWithDirectory() {
        final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance(
                DirectoryChooserConfig.builder().newDirectoryName("mydir").build());

        startFragment(fragment, DirectoryChooserActivityMock.class);

        final View chooseBtn = fragment.getActivity().findViewById(R.id.btnConfirm);
        assertThat(chooseBtn).isEnabled();

        assertThat(chooseBtn.performClick()).isTrue();
        assertThat(((DirectoryChooserActivityMock) fragment.getActivity()).selectedDirectory)
                .isNotNull();
    }

    @Test
    public void testCreateDirectoryDialogAllowFolderNameModification() {
        final String directoryName = "mydir";
        final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance(
                DirectoryChooserConfig.builder()
                        .newDirectoryName(directoryName)
                        .initialDirectory("")
                        .allowReadOnlyDirectory(false)
                        .allowNewDirectoryNameModification(true)
                        .build());

        startFragment(fragment, DirectoryChooserActivityMock.class);

        fragment.onOptionsItemSelected(new TestMenuItem() {
            @Override
            public int getItemId() {
                return R.id.new_folder_item;
            }
        });

        final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
        final ShadowAlertDialog shadowAlertDialog = Shadows.shadowOf(dialog);
        assertThat(shadowAlertDialog.getTitle()).isEqualTo("Create folder");
        assertThat(ShadowDialog.getShownDialogs()).contains(dialog);

        final TextView msgView = (TextView) dialog.findViewById(R.id.msgText);
        assertThat(msgView).hasText("Create new folder with name \"mydir\"?");

        final EditText editText = (EditText) dialog.findViewById(R.id.editText);
        assertThat(editText).isVisible();
        assertThat(editText).hasTextString(directoryName);
    }

    @Test
    public void testCreateDirectoryDialogDisallowFolderNameModification() {
        final String directoryName = "mydir";
        final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance(
                DirectoryChooserConfig.builder()
                        .newDirectoryName(directoryName)
                        .initialDirectory("")
                        .allowReadOnlyDirectory(false)
                        .allowNewDirectoryNameModification(false)
                        .build());

        startFragment(fragment, DirectoryChooserActivityMock.class);

        fragment.onOptionsItemSelected(new TestMenuItem() {
            @Override
            public int getItemId() {
                return R.id.new_folder_item;
            }
        });

        final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
        final ShadowAlertDialog shadowAlertDialog = Shadows.shadowOf(dialog);
        assertThat(shadowAlertDialog.getTitle()).isEqualTo("Create folder");
        assertThat(ShadowDialog.getShownDialogs()).contains(dialog);

        final TextView msgView = (TextView) dialog.findViewById(R.id.msgText);
        assertThat(msgView).hasText("Create new folder with name \"mydir\"?");

        final EditText editText = (EditText) dialog.findViewById(R.id.editText);
        assertThat(editText).isGone();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Test
    public void testWithCustomListener() {
        final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance(
                DirectoryChooserConfig.builder().newDirectoryName("mydir").build());

        startFragment(fragment, CustomDirectoryChooserActivity.class);
        final CustomDirectoryChooserListener listener = new CustomDirectoryChooserListener();
        fragment.setDirectoryChooserListener(listener);

        final View chooseBtn = fragment.getActivity().findViewById(R.id.btnConfirm);
        assertThat(chooseBtn).isEnabled();

        assertThat(chooseBtn.performClick()).isTrue();
        assertThat(listener.selectedDirectory).isNotNull();
    }

    static final private class DirectoryChooserActivityMock extends Activity implements
            DirectoryChooserFragment.OnFragmentInteractionListener {
        public String selectedDirectory;

        @Override
        public void onCreate(@Nullable Bundle bundle) {
            super.onCreate(bundle);
            setContentView(R.layout.directory_chooser_activity);
        }

        @Override
        public void onSelectDirectory(@NonNull String path) {
            this.selectedDirectory = path;
        }

        @Override
        public void onCancelChooser() {

        }
    }

    private static class CustomDirectoryChooserActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.directory_chooser_activity);
        }
    }

    private static class CustomDirectoryChooserListener implements
            DirectoryChooserFragment.OnFragmentInteractionListener {
        public String selectedDirectory;

        @Override
        public void onSelectDirectory(@NonNull String path) {
            selectedDirectory = path;
        }

        @Override
        public void onCancelChooser() {
            selectedDirectory = null;
        }
    }
}

@@ -644,50 +644,8 @@
 ws;%0A
-import org.robolectric.annotation.Config;%0A
 impo
@@ -686,24 +686,24 @@
 lertDialog;%0A
+
 import org.r
@@ -1037,16 +1037,22 @@
 NonNull 
+final 
 Fragment
@@ -1195,16 +1195,22 @@
         
+final 
 Activity
@@ -1375,32 +1375,38 @@
 get();%0A%0A        
+final 
 FragmentManager 
@@ -1462,16 +1462,22 @@
         
+final 
 Fragment
@@ -5929,28 +5929,22 @@
     
-static final 
 private
+ static
  cla
@@ -6175,20 +6175,38 @@
 ble 
+final 
 Bundle 
-bundl
+savedInstanceStat
 e) %7B
@@ -6237,13 +6237,25 @@
 ate(
-bundl
+savedInstanceStat
 e);%0A
@@ -6387,32 +6387,38 @@
 ectory(@NonNull 
+final 
 String path) %7B%0A 
@@ -6432,13 +6432,8 @@
     
-this.
 sele
@@ -6666,16 +6666,22 @@
 nCreate(
+final 
 Bundle s
@@ -7074,16 +7074,22 @@
 NonNull 
+final 
 String p

fix code smells