"""Module calculating heat map and statistics on top of warehouse model."""


def item_locations(inventory):
    """Create dictionary mapping (date, item) to list of locations."""
    item_to_loc = {}
    for date, status in inventory.items():
        item_to_loc[date] = {}
        for loc_id, loc_inv in status.items():
            for item in loc_inv.has_items:
                if item.name not in item_to_loc[date]:
                    item_to_loc[date][item.name] = []
                item_to_loc[date][item.name].append(loc_id)

    return item_to_loc


def calculate_frquencies(locations, inventory, orders):
    """Calculate order frequency for each location."""
    item_locs = item_locations(inventory)

    date = list(item_locs.keys())[-1]

    for order in orders.values():
        for item in order.has_ordered_items:
            for loc in item_locs[date][item.has_item.name]:
                locations[loc].has_freq += item.has_total_qty


# Bytes representing values of viridis colors for 0 to 255
VIRIDIS_COLORS = b'D\x01TD\x02UD\x03WE\x05XE\x06ZE\x08[F\t\\F\x0b^F\x0c_F\x0eaG\x0fbG\x11cG\x12eG\x14fG\x15gG\x16iG\x18jH\x19kH\x1alH\x1cnH\x1doH\x1epH qH!rH"sH#tG%uG&vG\'wG(xG*yG+zG,{F-|F/|F0}F1~E2\x7fE4\x7fE5\x80E6\x81D7\x81D9\x82C:\x83C;\x83C<\x84B=\x84B>\x85B@\x85AA\x86AB\x86@C\x87@D\x87?E\x87?G\x88>H\x88>I\x89=J\x89=K\x89=L\x89<M\x8a<N\x8a;P\x8a;Q\x8a:R\x8b:S\x8b9T\x8b9U\x8b8V\x8b8W\x8c7X\x8c7Y\x8c6Z\x8c6[\x8c5\\\x8c5]\x8c4^\x8d4_\x8d3`\x8d3a\x8d2b\x8d2c\x8d1d\x8d1e\x8d1f\x8d0g\x8d0h\x8d/i\x8d/j\x8d.k\x8e.l\x8e.m\x8e-n\x8e-o\x8e,p\x8e,q\x8e,r\x8e+s\x8e+t\x8e*u\x8e*v\x8e*w\x8e)x\x8e)y\x8e(z\x8e(z\x8e({\x8e\'|\x8e\'}\x8e\'~\x8e&\x7f\x8e&\x80\x8e&\x81\x8e%\x82\x8e%\x83\x8d$\x84\x8d$\x85\x8d$\x86\x8d#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e1\xe3\x18\xe4\xe3\x18\xe7\xe4\x19\xe9\xe4\x19\xec\xe4\x1a\xee\xe5\x1b\xf1\xe5\x1c\xf3\xe5\x1e\xf6\xe6\x1f\xf8\xe6!\xfa\xe6"\xfd\xe7$'


def get_heatmap_color(val):
    """Convert 0-1 value into #RRGGBB color."""
    val = int(val * 255)
    v = [VIRIDIS_COLORS[3 * val + i] for i in range(3)]
    return f"#{v[0]:02X}{v[1]:02X}{v[2]:02X}"
