# ---------------------------------------------------------------------
# Annotations listener
#
# This file implements a robot framework listener for test rail ids
# It gets TestRailIds from the TAGS of the test case
# Only Tags beggining with 'C' are considered TestRail ids
# It then writes the annotations file to $ROBOTOUT/annotations.json
# -------------------------------------------------------------------

from accelarobotkeywords.framework.listeners.annotation import Annotation
from robot.api import logger
import json
import os

ROBOT_LISTENER_API_VERSION = 3
ANNOTATIONS_FILE = 'annotations.json'
SUITE_ANNOTATIONS = []


def end_test(data, result):
    """Called at the end of a test case"""
    logger.info('Start of end_test listener')
    try:
        # get robot data
        test_name = data.name
        full_suite_name = data.parent.longname
        tags = data.tags._tags
        # process to annotations data
        class_name = full_suite_name
        testrailids = __parse_test_rail_tags(tags)
        jira_defect = __parse_jira_defect_tags(tags)
        jira_story = __parse_jira_story_tags(tags)
        jira_epic = __parse_jira_epic_tags(tags)
        annotation = Annotation(class_name=class_name, test_name=test_name, test_rail_ids=testrailids,
                                jira_defect_id=jira_defect, jira_story_id=jira_story,
                                jira_epic_id=jira_epic)
        SUITE_ANNOTATIONS.append(annotation)
        logger.info('Test case annotation added to suite annotations')
        logger.info(json.dumps(annotation.__dict__))
    except Exception as e:
        logger.error('Unable to process annotations for test case: %s' % data.name)
        logger.error(e)


def end_suite(data, result):
    """Called at the end of a test suite"""
    logger.info('Start of end_suite listener')
    try:
        json_data = '['
        for item in SUITE_ANNOTATIONS:
            json_data = json_data + json.dumps(item.__dict__) + ','
        json_data = json_data.rstrip(',') + ']'
        robot_out = os.environ.get('ROBOTOUT')
        file_path = os.path.join(robot_out, ANNOTATIONS_FILE)
        with open(file_path, 'w') as json_file:
            json_file.write(json_data)
        logger.info('Annotations file created at: %s' % file_path)
    except Exception as e:
        logger.error('Unable to generate annotations file')
        logger.error(e)


def __parse_test_rail_tags(tags):
    """Parses the tags looking for test rail ids"""
    testrailids = []
    for tag in tags:
        try:
            if 'TestRail:' in tag:
                items = tag.split(':')
                testrailids.append(items[1])
        except Exception:
            logger.warn('Tag: %s, not processed as in wrong format' % tag)
    return testrailids


def __parse_jira_epic_tags(tags):
    """Parses the tags looking for jira epic"""
    for tag in tags:
        try:
            if 'JIRAEpic:' in tag:
                items = tag.split(':')
                return items[1]
        except Exception:
            logger.warn('Tag: %s, not processed as in wrong format' % tag)
    return None


def __parse_jira_story_tags(tags):
    """Parses the tags looking for jira story"""
    for tag in tags:
        try:
            if 'JIRAStory:' in tag:
                items = tag.split(':')
                return items[1]
        except Exception:
            logger.warn('Tag: %s, not processed as in wrong format' % tag)
    return None


def __parse_jira_defect_tags(tags):
    """Parses the tags looking for jira defect"""
    for tag in tags:
        try:
            if 'JIRADefect:' in tag:
                items = tag.split(':')
                return items[1]
        except Exception:
            logger.warn('Tag: %s, not processed as in wrong format' % tag)
    return None




