import copy
import json

from ghcl.models.pull_request import PullRequest


class PRData:
    def __init__(self, data: dict = None):
        if data is None:
            with open('./tests/models/empty_pr_data.json') as file:
                self._data = json.load(file)
        else:
            self._data = data

    def with_pr_url(self, url: str = 'some-url'):
        data = copy.deepcopy(self._data)
        data['issues_data']['pull_request']['html_url'] = url
        return PRData(data)

    def with_label(self, label_to_add: str = None):
        data = copy.deepcopy(self._data)
        if label_to_add is None:
            label_number = len(data["issues_data"]["labels"]) + 1
            label_to_add = f'label-{label_number}'

        data['issues_data']['labels'].append({'name': label_to_add})
        return PRData(data)

    def with_created_at(self, created_at: str = '2014-04-24T16:34:47Z'):
        data = copy.deepcopy(self._data)
        data['issues_data']['created_at'] = created_at
        return PRData(data)

    def with_owner(self, owner: str = 'owner_user_id'):
        data = copy.deepcopy(self._data)
        data['pr_data']['base']['repo']['owner']['login'] = owner
        return PRData(data)

    def with_pr_raised_by(self, pr_raised_by: str = 'pr_raised_by_user_id'):
        data = copy.deepcopy(self._data)
        data['pr_data']['head']['user']['login'] = pr_raised_by
        return PRData(data)

    def with_merged(self, merged=False):
        data = copy.deepcopy(self._data)
        data['pr_data']['merged'] = merged
        return PRData(data)

    def with_state(self, state='some_state'):
        data = copy.deepcopy(self._data)
        data['issues_data']['state'] = state
        return PRData(data)

    def with_defaults(self):
        return PRData(self._data).with_pr_url()\
            .with_label()\
            .with_label()\
            .with_created_at()\
            .with_owner()\
            .with_pr_raised_by()\
            .with_merged()\
            .with_state()

    def as_pull_request(self):
        return PullRequest(**self._data)

[((381, 406), 'copy.deepcopy', 'copy.deepcopy', (['self._data'], {}), '(self._data)\n', (394, 406), False, 'import copy\n'), ((565, 590), 'copy.deepcopy', 'copy.deepcopy', (['self._data'], {}), '(self._data)\n', (578, 590), False, 'import copy\n'), ((928, 953), 'copy.deepcopy', 'copy.deepcopy', (['self._data'], {}), '(self._data)\n', (941, 953), False, 'import copy\n'), ((1109, 1134), 'copy.deepcopy', 'copy.deepcopy', (['self._data'], {}), '(self._data)\n', (1122, 1134), False, 'import copy\n'), ((1322, 1347), 'copy.deepcopy', 'copy.deepcopy', (['self._data'], {}), '(self._data)\n', (1335, 1347), False, 'import copy\n'), ((1497, 1522), 'copy.deepcopy', 'copy.deepcopy', (['self._data'], {}), '(self._data)\n', (1510, 1522), False, 'import copy\n'), ((1656, 1681), 'copy.deepcopy', 'copy.deepcopy', (['self._data'], {}), '(self._data)\n', (1669, 1681), False, 'import copy\n'), ((2082, 2107), 'ghcl.models.pull_request.PullRequest', 'PullRequest', ([], {}), '(**self._data)\n', (2093, 2107), False, 'from ghcl.models.pull_request import PullRequest\n'), ((255, 270), 'json.load', 'json.load', (['file'], {}), '(file)\n', (264, 270), False, 'import json\n')]