# Path: roulier/codec.py
# class DecoderGetLabel(ABC):
#     def __init__(self, config_object):
#         """
#         items in parcels list should be a dict of this form
#         label = {
#             "id": 1,
#             "reference": "",
#             "tracking": {
#                 "number":"",
#                 "url": "",
#                 "partner": "",
#             },
#             "label": {
#                 "data": base64 label,
#                 "name": "",
#                 "type": "",
#             },
#         }
#         """
#         self.config = config_object
#         self.result = {
#             "parcels": [],
#             "annexes": [],
#         }
# 
#     @abstractmethod
#     def decode(self, response, payload):
#         """Transform a specific representation to python dict.
#         Args:
#             response : answer from the webservice
#             payload : data sent initially to the webservice
#         Need to increment the result attribute of the object, it does not need to return
#         anything
#         """
#         pass
# 
#     # helper to get the reference in case of mono_parcel
#     def _get_parcel_number(self, payload):
#         parcel_ref = ""
#         roulier_input = self.config.roulier_input or {}
#         parcels = roulier_input.get("parcels", [])
#         if len(parcels) == 1:
#             parcel_ref = parcels[0].get("reference", "")
#         return parcel_ref
# 
# Path: roulier/carriers/chronopost_fr/api.py
# CHRONOPOST_LABEL_FORMAT = {
#     "PDF": "PDF",
#     "PPR": "PDF",
#     "SPD": "PDF",
#     "THE": "PDF",
#     "Z2D": "ZPL",
#     "XML": "XML",
#     "THEPSG": "PDF",
#     "Z2DPSG": "ZPL",
# }


from lxml import objectify
from ...codec import DecoderGetLabel
from .api import CHRONOPOST_LABEL_FORMAT


class ChronopostFrDecoder(DecoderGetLabel):
    """Chronopost XML -> Python."""

    def decode(self, response, input_payload):
        """Chronopost XML -> Python."""
        body = response["body"]
        xml = objectify.fromstring(body)

        # Understand a CreateShipmentWithLabelsResponse
        result = xml.getchildren()[0]
        tracking_ref = result.skybillNumber.text
        data = result.skybill.text.encode()
        output_format = input_payload["output_format"]
        parcel = {
            "id": 1,  # no multi parcel management for now.
            "reference": self._get_parcel_number(input_payload),
            "tracking": {"number": tracking_ref, "url": ""},
            "label": {
                "data": data,
                "name": "label_%s" % tracking_ref,

# Path: roulier/codec.py
# class DecoderGetLabel(ABC):
#     def __init__(self, config_object):
#         """
#         items in parcels list should be a dict of this form
#         label = {
#             "id": 1,
#             "reference": "",
#             "tracking": {
#                 "number":"",
#                 "url": "",
#                 "partner": "",
#             },
#             "label": {
#                 "data": base64 label,
#                 "name": "",
#                 "type": "",
#             },
#         }
#         """
#         self.config = config_object
#         self.result = {
#             "parcels": [],
#             "annexes": [],
#         }
# 
#     @abstractmethod
#     def decode(self, response, payload):
#         """Transform a specific representation to python dict.
#         Args:
#             response : answer from the webservice
#             payload : data sent initially to the webservice
#         Need to increment the result attribute of the object, it does not need to return
#         anything
#         """
#         pass
# 
#     # helper to get the reference in case of mono_parcel
#     def _get_parcel_number(self, payload):
#         parcel_ref = ""
#         roulier_input = self.config.roulier_input or {}
#         parcels = roulier_input.get("parcels", [])
#         if len(parcels) == 1:
#             parcel_ref = parcels[0].get("reference", "")
#         return parcel_ref
# 
# Path: roulier/carriers/chronopost_fr/api.py
# CHRONOPOST_LABEL_FORMAT = {
#     "PDF": "PDF",
#     "PPR": "PDF",
#     "SPD": "PDF",
#     "THE": "PDF",
#     "Z2D": "ZPL",
#     "XML": "XML",
#     "THEPSG": "PDF",
#     "Z2DPSG": "ZPL",
# }


# Path: roulier/carriers/chronopost_fr/decoder.py
from lxml import objectify
from ...codec import DecoderGetLabel
from .api import CHRONOPOST_LABEL_FORMAT



class ChronopostFrDecoder(DecoderGetLabel):
    """Chronopost XML -> Python."""

    def decode(self, response, input_payload):
        """Chronopost XML -> Python."""
        body = response["body"]
        xml = objectify.fromstring(body)

        # Understand a CreateShipmentWithLabelsResponse
        result = xml.getchildren()[0]
        tracking_ref = result.skybillNumber.text
        data = result.skybill.text.encode()
        output_format = input_payload["output_format"]
        parcel = {
            "id": 1,  # no multi parcel management for now.
            "reference": self._get_parcel_number(input_payload),
            "tracking": {"number": tracking_ref, "url": ""},
            "label": {
                "data": data,
                "name": "label_%s" % tracking_ref,

                "type": CHRONOPOST_LABEL_FORMAT.get(output_format, output_format),