from typing import Optional
from typing import Dict
from typing import List


def get_mock_aws_alb_event(
    method,
    path,
    query_parameters: Optional[Dict[str, List[str]]],
    headers: Optional[Dict[str, List[str]]],
    body,
    body_base64_encoded,
    multi_value_headers: bool,
):
    """Return a mock AWS ELB event.

    The `query_parameters` parameter must be given in the
    `multiValueQueryStringParameters` format - and if `multi_value_headers`
    is disabled, then they are simply transformed in to the
    `queryStringParameters` format.
    Similarly for `headers`.
    If `headers` is None, then some defaults will be used.
    if `query_parameters` is None, then no query parameters will be used.
    """
    resp = {
        "requestContext": {
            "elb": {
                "targetGroupArn": (
                    "arn:aws:elasticloadbalancing:us-east-2:123456789012:"
                    "targetgroup/lambda-279XGJDqGZ5rsrHC2Fjr/49e9d65c45c6791a"
                )
            }
        },
        "httpMethod": method,
        "path": path,
        "body": body,
        "isBase64Encoded": body_base64_encoded,
    }

    if headers is None:
        headers = {
            "accept": [
                "text/html,application/xhtml+xml,application/xml;"
                "q=0.9,image/webp,image/apng,*/*;q=0.8"
            ],
            "accept-encoding": ["gzip"],
            "accept-language": ["en-US,en;q=0.9"],
            "connection": ["keep-alive"],
            "host": ["lambda-alb-123578498.us-east-2.elb.amazonaws.com"],
            "upgrade-insecure-requests": ["1"],
            "user-agent": [
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                "(KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
            ],
            "x-amzn-trace-id": ["Root=1-5c536348-3d683b8b04734faae651f476"],
            "x-forwarded-for": ["72.12.164.125"],
            "x-forwarded-port": ["80"],
            "x-forwarded-proto": ["http"],
            "x-imforwards": ["20"],
        }

    query_parameters = {} if query_parameters is None else query_parameters

    # Only set one of `queryStringParameters`/`multiValueQueryStringParameters`
    # and one of `headers`/multiValueHeaders (per AWS docs for ALB/lambda)
    if multi_value_headers:
        resp["multiValueQueryStringParameters"] = query_parameters
        resp["multiValueHeaders"] = headers
    else:
        # Take the last query parameter/cookie (per AWS docs for ALB/lambda)
        resp["queryStringParameters"] = {
            k: (v[-1] if len(v) > 0 else []) for k, v in query_parameters.items()
        }
        resp["headers"] = {k: (v[-1] if len(v) > 0 else []) for k, v in headers.items()}

    return resp