#!/usr/bin/env python

###############################################################################
# Copyright 2017 The Apollo Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################

import random

import matplotlib.pyplot as plt
from matplotlib import cm as cmx
from matplotlib import colors as mcolors

import common.proto_utils as proto_utils
from modules.map.proto import map_pb2


class Map:
    def __init__(self):
        self.map_pb = map_pb2.Map()
        self.colors = []
        self.init_colors()

    def init_colors(self):
        color_num = 6
        self.colors = []
        values = range(color_num)
        jet = plt.get_cmap('brg')
        color_norm = mcolors.Normalize(vmin=0, vmax=values[-1])
        scalar_map = cmx.ScalarMappable(norm=color_norm, cmap=jet)
        for val in values:
            color_val = scalar_map.to_rgba(val)
            self.colors.append(color_val)

    def load(self, map_file_name):
        res = proto_utils.get_pb_from_file(map_file_name, self.map_pb)
        return res != None

    def draw_roads(self, ax):
        cnt = 1
        for road in self.map_pb.road:
            color_val = self.colors[cnt % len(self.colors)]
            self.draw_road(ax, road, color_val)