"""CLI program that allows the user to input keywords for a basic search of the Orcid API.  It uses
    the OrcidManager class to find the Orcid ID and data.
"""

from orcidmanager import OrcidManager
import click

__author__ = 'cwilli34'


def orcid_search(sandbox):
    """Get the Orcid_id from the email search

    Parameters
    ----------
    :param sandbox: boolean
        Should the sandbox be used. True (default) indicates development mode.

    Returns
    -------
    :returns orcid.orcid_Id: string
        Returns the Orcid ID from a basic search by user.
    """
    # Prompt and get search terms
    print('* You can leave fields blank *')
    query = {
        'first_name': click.prompt('Please enter a first name', default='', show_default=False),
        'last_name': click.prompt('Please enter a last name', default='', show_default=False),
        'email': click.prompt('Please enter an email', default='', show_default=False),
        'keywords': click.prompt('Please enter some keywords (like country, department or institution)',
                                 default='', show_default=False)
    }
    print('')

    first_name = query['first_name']
    last_name = query['last_name']
    email = query['email']
    keywords = query['keywords']

    if first_name:
        first_name = 'given-names:' + query['first_name']
    if last_name:
        last_name = 'family-name:' + query['last_name']
    if email:
        email = 'email:' + query['email']

    # Configures search string for lucene formatting
    if first_name and last_name:
        first_name += ' AND '
    elif first_name and (email or keywords):
        first_name += ' AND '
    if last_name and (email or keywords):
        last_name += ' AND '
    if not last_name and not first_name:
        if email and keywords:
            email += ' AND '
    else:
        if email and keywords:
            email = '(' + email + ' AND '
            keywords += ')'

    search_terms = first_name + last_name + email + keywords

    # View string input
    print search_terms + '\n'

    # Call OrcidManager from orcidmanager.py
    orcid = OrcidManager(query=search_terms, sandbox=sandbox)

    while orcid.orcid_id is None:
        orcid = orcid_search(sandbox)
        if orcid.orcid_id:
            break

    return orcid

@@ -68,24 +68,28 @@
 earch of the
+%0A   
  Orcid API. 
@@ -96,20 +96,16 @@
  It uses
-%0A   
  the Orc
@@ -160,45 +160,20 @@
 %22%22%0A%0A
-from orcidmanager 
 import 
-O
+o
 rcid
-Manager
 %0Aimp
@@ -187,34 +187,8 @@
 ck%0A%0A
-__author__ = 'cwilli34'%0A%0A%0A
 def 
@@ -640,32 +640,45 @@
 ': click.prompt(
+%0A            
 'Please enter a 
@@ -712,32 +712,41 @@
 ow_default=False
+%0A        
 ),%0A        'last
@@ -758,32 +758,45 @@
 ': click.prompt(
+%0A            
 'Please enter a 
@@ -829,32 +829,41 @@
 ow_default=False
+%0A        
 ),%0A        'emai
@@ -871,32 +871,45 @@
 ': click.prompt(
+%0A            
 'Please enter an
@@ -947,16 +947,25 @@
 lt=False
+%0A        
 ),%0A     
@@ -992,16 +992,29 @@
 .prompt(
+%0A            
 'Please 
@@ -1080,41 +1080,8 @@
 n)',
-%0A                                
  def
@@ -1107,16 +1107,25 @@
 lt=False
+%0A        
 )%0A    %7D%0A
@@ -1147,144 +1147,40 @@
     
-first_name = query%5B'first_name'%5D%0A    last_name = query%5B'last_name'%5D%0A    email = query%5B'email'%5D%0A    keywords = query%5B'keywords'%5D%0A%0A    if 
+search_terms = %22%22%0A    if query%5B'
 firs
@@ -1177,32 +1177,34 @@
 uery%5B'first_name
+'%5D
 :%0A        first_
@@ -1197,27 +1197,30 @@
         
-first_name 
+search_terms +
 = 'given
@@ -1261,464 +1261,214 @@
  if 
-last_name:%0A        last_name = 'family-name:' + query%5B'last_name'%5D%0A    if email:%0A        email = 'email:' + query%5B'email'%5D%0A%0A    # Configures search string for lucene formatting%0A    if first_name and last_name:%0A        first_name
+query%5B'last_name'%5D:%0A        if len(search_terms) %3E 0:%0A            search_terms
  += 
-'
+%22
  AND 
-'
+%22
 %0A    
-elif first_name and (email or keywords):%0A        first_name += ' AND '%0A    if last_name and (email or keywords):%0A        last_name += ' AND '%0A    if not last_name and not first_name:%0A        if email and keywords
+    search_terms += 'family-names:' + query%5B'last_name'%5D%0A    if query%5B'email'%5D:%0A        if len(search_terms) %3E 0
 :%0A  
@@ -1481,459 +1481,452 @@
     
-email
+search_terms
  += 
-'
+%22
  AND 
-'
+%22
 %0A    
-else:%0A        if email and keywords:%0A            email = '(' + email + ' AND '%0A            keywords += ')'%0A%0A    search_terms = first_name + last_name + email + keywords%0A%0A    # View string input%0A    print search_terms + '%5Cn'%0A%0A    # Call OrcidManager from orcidmanager.py%0A    orcid = OrcidManager(query=search_terms, sandbox=sandbox)%0A%0A    while orcid.orcid_id is None:%0A        orcid = 
+    search_terms += 'email:' + query%5B'email'%5D%0A    if query%5B'keywords'%5D:%0A        if len(search_terms) %3E 0:%0A            search_terms += %22 AND %22%0A        search_terms += query%5B'keywords'%5D%0A%0A    api = orcid.SearchAPI(False)%0A    results = api.search_public(search_terms).get('orcid-search-results', None)%0A    if results is None:%0A        return %22%22%0A%0A    result = results.get('
 orcid
-_
+-
 search
-(sandbox)%0A   
+-result', None)%0A
     
- 
 if 
-orcid.orcid_id
+result is None
 :%0A  
@@ -1935,17 +1935,17 @@
     
-    break
+return %22%22
 %0A%0A  
@@ -1953,14 +1953,63 @@
  return 
-orcid
+result%5B0%5D%5B'orcid-profile'%5D%5B'orcid-identifier'%5D%5B'path'%5D
 %0A

Update profilesearch to remove the requirement on orcidfind