import xmltodict


def gen_query(search_ligand, search_protein=None, querymode=None):
    """
    Args:
        search_ligand: the Chem_ID of the FDA-approved inhibitor of interest
        search_protein: UniProt Accession ID for the approved target for the inhibitor
        querymode: string indicating whether searching by ligand or for approved target/inhibitor

    Returns: xml parsed dictionary to be used with pypdb.do_search

    """

    if querymode == 'Lig':
        xml = """
        <orgPdbQuery>
         <queryType>org.pdb.query.simple.ChemCompIdQuery</queryType>
         <description>Chemical ID(s): searching for FDA approved kinase inhibitors</description>
            <chemCompId>%s</chemCompId>
            <polymericType>Any</polymericType>
        </orgPdbQuery>
        """ % search_ligand

        final_params = xmltodict.parse(xml)

    elif querymode == 'Apo':
        xml = """
        <orgPdbCompositeQuery>
         <queryRefinement>
          <queryRefinementLevel>0</queryRefinementLevel>
          <orgPdbQuery>
             <queryType>org.pdb.query.simple.UpAccessionIdQuery</queryType>
             <accessionIdList>%s</accessionIdList>
          </orgPdbQuery>
         </queryRefinement>
         <queryRefinement>
          <queryRefinementLevel>1</queryRefinementLevel>
          <conjunctionType>and</conjunctionType>
          <orgPdbQuery>
            <queryType>org.pdb.query.simple.NoLigandQuery</queryType>
            <haveLigands>no</haveLigands>
          </orgPdbQuery>
         </queryRefinement>
         <queryRefinement>
          <queryRefinementLevel>2</queryRefinementLevel>
          <conjunctionType>and</conjunctionType>
          <orgPdbQuery>
            <version>head</version>
            <queryType>org.pdb.query.simple.EnzymeClassificationQuery</queryType>
            <Enzyme_Classification>2.7.11.*</Enzyme_Classification>
          </orgPdbQuery>
          <conjunctionType>or</conjunctionType>
          <orgPdbQuery>
            <version>head</version>
            <queryType>org.pdb.query.simple.EnzymeClassificationQuery</queryType>
            <Enzyme_Classification>2.7.10.*</Enzyme_Classification>
          </orgPdbQuery>
           </queryRefinement>
        </orgPdbCompositeQuery>

        """"" % (search_protein)

        final_params = xmltodict.parse(xml)

    else:
        xml = """
        <orgPdbCompositeQuery>
         <queryRefinement>
          <queryRefinementLevel>0</queryRefinementLevel>
          <orgPdbQuery>
            <queryType>org.pdb.query.simple.UpAccessionIdQuery</queryType>
            <accessionIdList>%s</accessionIdList>
          </orgPdbQuery>
         </queryRefinement>
         <queryRefinement>
          <queryRefinementLevel>1</queryRefinementLevel>
          <conjunctionType>and</conjunctionType>
          <orgPdbQuery>
            <queryType>org.pdb.query.simple.ChemCompIdQuery</queryType>
            <chemCompId>%s</chemCompId>
          </orgPdbQuery>
         </queryRefinement>
        </orgPdbCompositeQuery>
        """ % (search_protein, search_ligand)

        final_params = xmltodict.parse(xml)

    return final_params