import json
import os
from flask import Blueprint
from flask import Response
from flask import request
from flask.ext.cors import cross_origin
from geobricks_common.core.log import logger
from geobricks_distribution.core.distribution_core import Distribution
from geobricks_distribution.config.config import config
from flask import request, send_from_directory

log = logger(__file__)

app = Blueprint("distribution", "distribution")


# TODO: How to map it to the download distribution URL? Get the one in the @app.route "/download/"
#distribution_url = request.host_url + "distribution/download/"

zip_filename = "layers"


@app.route('/')
@cross_origin(origins='*')
def root():
    """
    Root REST service.
    @return: Welcome message.
    """
    return 'Welcome to Geobricks Distribution!'
from brewer2mpl import get_map as brewer2mpl_get_map
from geobricks_common.core.log import logger

log = logger(__file__)


def get_colors(color_ramp, intervals, reverse=False):
    try:
        return brewer2mpl_get_map(color_ramp,
                                  "Sequential",
                                  intervals,
                                  reverse=reverse).hex_colors
    except Exception, e:
        log.warn(e)
        pass
    try:
        return brewer2mpl_get_map(color_ramp,
                                  "Diverging",
                                  intervals,
                                  reverse=reverse).hex_colors
    except Exception, e:
        log.warn(e)
        pass
    try:
        return brewer2mpl_get_map(color_ramp,
                                  "Qualitative",
                                  intervals,
                                  reverse=reverse).hex_colors
    except Exception, e:
        log.warn(e)
        pass