# Path: washmap/washmap/chart_constants.py
# SANITATION_COLOR_RANGE = ["#d45500", "#da670f", "#eb7e1f", "#eb941f", "#ebb01f", "#f2c83d", "#d3cc4f", "#86c26f", "#4db181", "#15b598"]
# 
# WATER_COLOR_RANGE = ["#8c9494", "#8398a2", "#7c9baa", "#73a1b4", "#6aa6bd", "#62abc7", "#5aafd0", "#52b4d9", "#49bae4", "#3fc0f0"]
# 
# GRAY = "#CCCCCC"


from pandas import DataFrame
from numpy import where
from country.models import Country
from main.utils import build_coords_lists
from stats.models import StatValue
from .chart_constants import SANITATION_COLOR_RANGE, WATER_COLOR_RANGE, GRAY





def color_data(data):
    def _get_color(value, palette):
        if value < 0:
            return GRAY
        index = int((value-0.01) / 10)
        return palette[index]

    data['wat_color'] = data['wat_value'].apply(
        _get_color, args=([WATER_COLOR_RANGE])
    )
    data['san_color'] = data['san_value'].apply(
        _get_color, args=([SANITATION_COLOR_RANGE])
    )
    data['wat_value'] = where(data['wat_value'] < 0, '-', data['wat_value'])
    data['san_value'] = where(data['san_value'] < 0, '-', data['san_value'])
    return data


def get_countries():
    # Get the countries data frame
    countries = Country.objects.exclude(boundary='')
    countries = countries.filter(region__in=[1, 2, 3, 6, 7])  # Africa only
    countries = countries.values('name', 'boundary', 'id')
    countries_df = DataFrame.from_records(countries)
    countries_df['xs'], countries_df['ys'] = build_coords_lists(countries_df['boundary'])  # nopep8

# Path: washmap/washmap/chart_constants.py
# SANITATION_COLOR_RANGE = ["#d45500", "#da670f", "#eb7e1f", "#eb941f", "#ebb01f", "#f2c83d", "#d3cc4f", "#86c26f", "#4db181", "#15b598"]
# 
# WATER_COLOR_RANGE = ["#8c9494", "#8398a2", "#7c9baa", "#73a1b4", "#6aa6bd", "#62abc7", "#5aafd0", "#52b4d9", "#49bae4", "#3fc0f0"]
# 
# GRAY = "#CCCCCC"


# Path: washmap/washmap/map_data.py
from pandas import DataFrame
from numpy import where
from country.models import Country
from main.utils import build_coords_lists
from stats.models import StatValue
from .chart_constants import SANITATION_COLOR_RANGE, WATER_COLOR_RANGE, GRAY






def color_data(data):
    def _get_color(value, palette):
        if value < 0:
            return GRAY
        index = int((value-0.01) / 10)
        return palette[index]

    data['wat_color'] = data['wat_value'].apply(
        _get_color, args=([WATER_COLOR_RANGE])
    )
    data['san_color'] = data['san_value'].apply(
        _get_color, args=([SANITATION_COLOR_RANGE])
    )
    data['wat_value'] = where(data['wat_value'] < 0, '-', data['wat_value'])
    data['san_value'] = where(data['san_value'] < 0, '-', data['san_value'])
    return data


def get_countries():
    # Get the countries data frame
    countries = Country.objects.exclude(boundary='')
    countries = countries.filter(region__in=[1, 2, 3, 6, 7])  # Africa only
    countries = countries.values('name', 'boundary', 'id')
    countries_df = DataFrame.from_records(countries)
    countries_df['xs'], countries_df['ys'] = build_coords_lists(countries_df['boundary'])  # nopep8

    countries_df = countries_df.drop(['id', 'boundary'], axis=1)