__all__=["UserInterface"]
import sys
class UserInterface(object):
    """ 
    Class that will query the user for options and also the DB for extra information
    """
    def __init__(self,wosisObj,dicObj,schemaNameStr,tableNameStr):
        """Class/code section that will query the user for options to clean the reference dictionary from multiple options.
        @type wosisObj: class WOSIS
        @param wosisObj: WOSIS class that contains the DB connection, that will be used in case of request by the user
        @type dict: 
        @param Dictionary with keys(terms) and values (DB PrimaryKeys) that need to be evaluated by user
         
        """
        self.wosisObj = wosisObj
        self.dicObj = dicObj
        self.schemaNameStr = schemaNameStr
        self.tableNameStr = tableNameStr
    
    def run(self):
        """Method that kickstarts the process, by calling the Class's methods in a sequencial order. 
        The method returns the dictObj that has been updated
        @rtype: dict
        @return: Dictionary used by __init__ that was clean by user 
        """
        self.__queryUser() 
        return self.dicObj
        
    def __queryUser(self):
        """Private method that will loop through key:value and will call the user query function (__getUserOption) to determine
        which option is the correct
        @rtype: dict
        @return: Dictionary used by __init__ that was clean by user 
        """
        print "Please select from list or press 'q' to query DB"
        for key,value in self.dicObj.items():
            if len(value)>1:
                question="The parameter: %s has the following keys, select one %s: \n" % (key,str(value))
                selectValue=self.__getUserOption(question,value)
                self.dicObj[key]=[selectValue]
        return 
    def __getUserOption(self,question,options):
        """Private method that asks the user for the correct option returning the selected option
        @type question: string
        @param question: Question being asked to the user, e.g select one key
        @type options: list
        @param options: List of options to select, ints representing the primary key in DB
        @rtype: integer
        @return: Integer representing Primary key in DB  
        """
        #options need to be cast to str since we have integers and 'q' as options
        options = [str(item) for item in options]
        
        while True:
            sys.stdout.write(question)
            choice = raw_input()
            if choice in options:
                return choice
            elif choice == 'q' or choice == 'Q':
                sys.stdout.write("Let me get that for you \n")
                header,result=self.wosisObj.getRecordPK(int(options[0]),self.schemaNameStr,self.tableNameStr)
                print header
                print result
                for remainingNum in options[1:]:
                    header,result=self.wosisObj.getRecordPK(int(remainingNum),self.schemaNameStr,self.tableNameStr)
                    print result
            #need to query DB
            else:
                sys.stdout.write("Please select from list or press 'q' to query DB \n")
        
