from typing import OrderedDict


def _create_jpred_query(rest_format, file=None, seq=None, skipPDB=True, email=None, name=None, silent=False):
    """Create query string to be submitted to JPred server.

    :param str rest_format: Format for JPred REST interface.
    :param str file: File path to a file with the job input (sequence or msa).
    :param str seq: Alternatively, amino acid sequence passed as string of single-letter code without spaces, e.g. --seq=ATWFGTHY
    :param skipPDB: Should the PDB query be skipped?
    :type skipPDB: :py:obj:`True` or :py:obj:`False`
    :param str email: For a batch job submission, where to send the results?
    :param str name: A name for the job.
    :param silent: Should the work be done silently?
    :type silent: :py:obj:`True` or :py:obj:`False`
    :return: Query string.
    :rtype: :py:class:`str`
    """
    if file is None and seq is None:
        raise ValueError("""Neither input sequence nor input file are defined.
        Please provide either --file or --seq parameters.""")
    elif file and seq:
        raise ValueError("""Both input sequence and input file are defined.
        Please choose --file or --seq parameter.""")

    if file is not None:
        with open(file, "r") as infile:
            sequence_query = infile.read()
    elif seq is not None:
        sequence_query = ">query\n{}".format(seq)
    else:
        sequence_query = ""

    if skipPDB:
        skipPDB = "on"
    else:
        skipPDB = "off"

    if rest_format == "batch" and email is None:
        raise ValueError("""When submitting batch job email is obligatory.
        You will receive detailed report, list of links and a link to archive
        to all results via email.""")

    if not silent:
        print("Your job will be submitted with the following parameters:")
        print("format:", rest_format)
        print("skipPDB:", skipPDB)
        if file is not None:
            print("file:", file)
        elif seq is not None:
            print("seq:", seq)
        if email is not None:
            print("email:", email)
        if name is not None:
            print("name:", name)

    parameters_dict = OrderedDict([("skipPDB", skipPDB),
                                   ("format", rest_format),
                                   ("email", email),
                                   ("name", name)])

    parameters_list = ["{}={}".format(k, v) for k, v in parameters_dict.items() if v]
    parameters_list.append(sequence_query)
    query = u"£€£€".join(parameters_list)
    return query