# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import base64
import hashlib
import re

from recipe_engine import recipe_test_api


class GitilesTestApi(recipe_test_api.RecipeTestApi):
    def hash(self, *bases):
        return hashlib.sha1(":".join(bases).encode()).hexdigest()

    def log(
        self,
        step_name,
        s,
        n=3,
        add_change_id=False,
        extra_footers=None,
        committer_email=None,
    ):
        commits = []
        for i in range(n):
            commit = "fake %s hash %d" % (s, i)
            name = "Fake %s" % (s)
            email = committer_email or "fake_%s@fake_%i.email.com" % (s, i)

            message_lines = ["fake %s msg %d" % (s, i)]

            footers = {}
            if extra_footers:
                footers.update(extra_footers)
            if add_change_id:
                # Using commit from above as Change-Id, but removing whitespace
                # and possibly future punctuation.
                alphanumeric_only_commit = re.sub(r"\W+", "", commit)
                footers["Change-Id"] = "I%s" % alphanumeric_only_commit

            for key, value in sorted(footers.items()):
                message_lines.append("%s: %s" % (key, value))

            commits.append(
                {
                    "id": self.hash(commit),
                    "tree": self.hash("tree", commit),
                    "parents": [self.hash("parent", commit)],
                    "author": {
                        "name": name,
                        "email": email,
                        "time": "Mon Jan 01 00:00:00 2015",
                    },
                    "committer": {
                        "name": name,
                        "email": email,
                        "time": "Mon Jan 01 00:00:00 2015",
                    },
                    "message": "\n".join(message_lines),
                    "tree_diff": [
                        {
                            "type": "add",
                            "old_id": 40 * "0",
                            "old_mode": 0,
                            "new_id": self.hash("file", f, commit),
                            "new_mode": 33188,
                            "new_path": f,
                        }
                        for f in ["%s.py" % (chr(i + ord("a")))]
                    ],
                }
            )
        return self.step_data(step_name, self.m.json.output(commits))

    def fetch(self, step_name, data):
        return self.m.url.text(step_name, base64.b64encode(data.encode()))
