import re

import accloudtant


class UsageRecord(object):
    def __init__(self, data):
        self._data = data

        self.area_id = self.kind.split("-")[0]

    def __repr__(self):
        return "UsageRecord({})".format(self._data)

    @property
    def service(self):
        return self._data["Service"]

    @property
    def operation(self):
        return self._data[" Operation"]

    @property
    def kind(self):
        return self._data[" UsageType"]

    @kind.setter
    def kind(self, value):
        self._data[" UsageType"] = value

    @property
    def resource(self):
        return self._data[" Resource"]

    @property
    def start(self):
        return self._data[" StartTime"]

    @property
    def end(self):
        return self._data[" EndTime"]

    @property
    def value(self):
        return self._data[" UsageValue"]

    @property
    def area(self):
        if self.kind == "Invalidations" or self.service == "AmazonRoute53":
            return "Global"
        if self.area_id == "EU":
            return "Europe"
        elif self.area_id == "US":
            return "United States"
        elif self.area_id == "CA":
            return "Canada"
        elif self.area_id == "EUC1":
            return "EU (Frankfurt)"
        return None

    @property
    def is_bandwidth(self):
        bandwidth_regexp = "[A-Z]{2}-(In|Out)-(Bytes|OBytes)-(HTTP|HTTPS)-(Static|Dynamic|Proxy)"
        return re.search(bandwidth_regexp, self._data[" UsageType"])
