from typing import Dict


def transform_some_fields_into_markdown(collection_name, feed: Dict) -> Dict:
    """
    Some fields can have complex nesting, so this function transforms them into an appropriate state.

    :param collection_name: which collection this feed belongs to.
    :param feed: feed from GIB TI&A that needs transformation.
    :return: given feed with transformed fields.
    """

    if collection_name == "osi/git_leak":
        buffer = ""
        revisions = feed.get("revisions", [])
        for i in revisions:
            file = "[https://bt.group-ib.com/api/v2/osi/git_leak]({0})".format(i.get("file"))
            file_diff = "[https://bt.group-ib.com/api/v2/osi/git_leak]({0})".format(i.get("fileDiff"))
            info = i.get("info")
            author_email, author_name, date = info.get("authorEmail"), info.get("authorName"), info.get("dateCreated")
            buffer += "| {0} | {1} | {2} | {3} | {4} |\n".format(file, file_diff, author_email, author_name, date)
        if buffer:
            buffer = "| File | File Difference | Author Email | Author Name | Date Created |\n" \
                     "| ---- | --------------- | ------------ | ----------- | ------------ |\n" + buffer
            feed["revisions"] = buffer
        else:
            del feed["revisions"]

    elif collection_name == "osi/public_leak":
        buffer = ""
        link_list = feed.get("linkList", [])
        for i in link_list:
            author = i.get("author")
            detected = i.get("dateDetected")
            published = i.get("datePublished")
            hash_ = i.get("hash")
            link = "[{0}]({0})".format(i.get("link"))
            source = i.get("source")
            buffer += "| {0} | {1} | {2} | {3} | {4} | {5} |\n".format(author, detected, published, hash_, link, source)
        if buffer:
            buffer = "| Author | Date Detected | Date Published | Hash | Link | Source |\n" \
                     "| ------ | ------------- | -------------- | ---- |----- | ------ |\n" + buffer
            feed["linkList"] = buffer
        else:
            del feed["linkList"]

        buffer = ""
        matches = feed.get("matches", {})
        if isinstance(matches, list):
            matches = {}
        for type_, sub_dict in matches.items():
            for sub_type, sub_list in sub_dict.items():
                for value in sub_list:
                    buffer += "| {0} | {1} | {2} |\n".format(type_, sub_type, value)
        if buffer:
            buffer = "| Type | Sub Type | Value |\n" \
                     "| ---- | -------- | ----- |\n" + buffer
            feed["matches"] = buffer
        else:
            del feed["matches"]

    elif collection_name == "bp/phishing_kit":
        buffer = ""
        downloaded_from = feed.get("downloadedFrom", [])
        for i in downloaded_from:
            date, url, domain, filename = i.get("date"), i.get("url"), i.get("domain"), i.get("fileName")
            buffer += "| {0} | {1} | {2} | {3} |\n".format(url, filename, domain, date)
        if buffer:
            buffer = "| URL | File Name | Domain | Date |\n| --- | --------- | ------ | ---- |\n" + buffer
            feed["downloadedFrom"] = buffer
        else:
            del feed["downloadedFrom"]

    return feed