"""
requests.py
====================================
Request handelling and data structures for interacting with iMath REST API
"""


class PartData:
    """
    Part data helper class.

    Includes addition functions creating json
    data from part data for easy use in REST API.

    Attributes
    ----------
    timestamp: str
        Timestamp e.g. 1516193959559
    part_id: str
        Unique part ID e.g. Part1234
    source: str
        Data source e.g. I3DR_DESKTOP_ABC123
    part_data: list
        Additional part data as a key value pair array

    """

    def __init__(
            self, timestamp: str, part_id: str,
            source: str, part_data: list):
        """
        Part data construction.

        Parameters
        ----------
        timestamp: str
            Timestamp e.g. 1516193959559
        part_id: str
            Unique part ID e.g. Part1234
        source: str
            Data source e.g. I3DR_DESKTOP_ABC123
        part_data: list
            Additional part data as a key value pair array

        """
        self.timestamp = timestamp
        self.part_id = part_id
        self.source = source
        self.part_data = part_data

    def get_json(self) -> dict:
        """
        Convert PartData into json string for use in REST API.

        Returns
        -------
        dict
            Json formatted string

        """
        return {
            "timestamp": self.timestamp,
            "part_id": self.part_id,
            "source": self.source,
            "part_data": self.part_data
        }


class ImageValue:
    """
    Image Value helper class.

    Includes addition functions creating json data
    from part data for easy use in REST API.

    Attributes
    ----------
    value: str
        Name of the image e.g. test001.png
    timestamp: str
        Timestamp e.g. 1516193959559
    position: str
        Position on the part where the image was taken
    dimension: str
        Dimension of the area of the part, which is on the image
    quality: str
        Quality

    """

    def __init__(
            self, value: str, timestamp: str,
            position: str, dimension: str, quality: str):
        """
        Image value construction.

        Parameters
        ----------
        value: str
            Name of the image e.g. test001.png
        timestamp: str
            Timestamp e.g. 1516193959559
        position: str
            Position on the part where the image was taken
        dimension: str
            Dimension of the area of the part, which is on the image
        quality : str
            Quality

        """
        self.value = value
        self.timestamp = timestamp
        self.position = position
        self.dimension = dimension
        self.quality = quality

    def get_json(self) -> dict:
        """
        Convert ImageValue into json string for use in REST API.

        Returns
        -------
        dict
            Json formatted string

        """
        return {
            "value": self.value,
            "timestamp": self.timestamp,
            "position": self.position,
            "dimension": self.dimension,
            "quality": self.quality
        }


class ImageMetaData:
    """
    Image meta data helper class.

    Includes addition functions creating json data
    from image meta data for easy use in REST API.

    Attributes
    ----------
    part_id: str
        Unique part ID e.g. Part1234
    value_id: str
        Unique identification of the capture device e.g. Camera1
    source: str
        Data source e.g. I3DR_DESKTOP_ABC123
    values: list
        List of ImageValues
    qualifying_metadata: list
        Qualifying metadata should be a key value pair array

    """

    def __init__(
            self, part_id: str, value_id: str,
            source: str, values: list, qualifying_metadata):
        """
        Image meta data construction.

        Parameters
        ----------
        part_id: str
            Unique part ID e.g. Part1234
        value_id: str
            Unique identification of the capture device e.g. Camera1
        source: str
            Data source e.g. I3DR_DESKTOP_ABC123
        values: list
            List of ImageValues
        qualifying_metadata: list
            Qualifying metadata should be a key value pair array

        """
        self.part_id = part_id
        self.value_id = value_id
        self.source = source
        self.values = []
        for value in values:
            self.values.append(value.get_json())
        self.qualifying_metadata = qualifying_metadata

    def get_json(self) -> dict:
        """
        Convert ImageMetaData into json string for use in REST API.

        Returns
        -------
        dict
            Json formatted string

        """
        return {
            "part_id": self.part_id,
            "value_id": self.value_id,
            "source": self.source,
            "values": self.values,
            "qualifying_metadata": self.qualifying_metadata
        }


class ImageAnalysisData:
    """
    Image meta data helper class.

    (Not yet implimented)
    Includes addition functions creating json data
    from image meta data for easy use in REST API.
    TODO finish implimenting image analysis data.

    Attributes
    ----------
    part_id : str
        Unique part ID e.g. Part1234
    source : str
        Data source e.g. I3DR_DESKTOP_ABC123
    value: str
        List of ImageValues
    timestamp : str
        Timestamp e.g. 1516193959559
    failures: list
        TODO impliment

    """

    def __init__(
            self, part_id: str, source: str,
            value: str, timestamp: str, failures: list):
        """
        Image Meta Data construction.

        (Not yet implimented)

        Parameters
        ----------
        part_id : str
            Unique part ID e.g. Part1234
        source : str
            Data source e.g. I3DR_DESKTOP_ABC123
        value: str
            List of ImageValues
        timestamp : str
            Timestamp e.g. 1516193959559
        failures: list
            TODO impliment

        """
        raise NotImplementedError
        # self.part_id = part_id  # Unique part ID e.g. Part1234
        # self.source = source  # Data source e.g. I3DR_DESKTOP_ABC123
        # self.value = value  # name of the image (NOT ImageValue)
        # self.timestamp = timestamp
        # # Values should be a list of ImageValues
        # self.values = []
        # for value in failures:
        #     self.values.append(value.get_json())
        # Qualifying metadata should be a key value pair array e.g.
        # qualifying_metadata = [
        # 	{
        # 		"key": "key1",
        # 		"value": "value1"
        # 	},
        # 	{
        # 		"key": "key2",
        # 		"value": "value2"
        # 	}
        # ]
        # self.qualifying_metadata = qualifying_metadata

    def get_json(self) -> dict:
        """
        Convert ImageAnalysisData into json string for use in REST API.

        (Not yet implimented)

        Returns
        -------
        dict
            Json formatted string

        """
        raise NotImplementedError
        # json = {
        # 	"part_id": self.part_id,
        # 	"value_id": self.value_id,
        # 	"source": self.source,
        # 	"values": self.values,
        # 	"qualifying_metadata": self.qualifying_metadata
        # }


# Image analysis example data
# {
#     "part_id": "Part1234",
#     "source": "Analysis System",
#     "value": "Part1234_1.1_2.2_1.jpg",
#     "timestamp": "1516193959559",
#     "failures": [
#         {
#             "id": 124355435321576
#             "failure": "4711",
#             "position": [
#              44.2,
#              17.4
#             ],
#             "dimension": [
#                 Page 8 of 10
#                 5.2,
#                 1
#             ],
#             "qualifying_metadata": [
#                 {
#                     "key": "xxx",
#                     "value": "1"
#                 },
#                 {
#                     "key": "yyy",
#                     "value": "2"
#                 }
#             ]
#         },
#         {
#             "id": 124355435321578
#             "failure": "4712",
#             "position": [
#                 33.2,
#                 3
#             ],
#             "dimension": [
#                 2.3,
#                 1.1
#             ],
#             "qualifying_metadata": [
#                 {
#                     "key": "xxx",
#                     "value": "1"
#                 },
#                 {
#                     "key": "yyy",
#                     "value": "2"
#                 }
#             ]
#         }
#     ]
# }

# TODO add REST API post functions
# # Create a new resource
# response = requests.post('https://httpbin.org/post', data = {'key':'value'})
# # Update an existing resource
# requests.put('https://httpbin.org/put', data = {'key':'value'})
