# -*- coding: utf-8 -*-

import scss

import poobrains.helpers
import poobrains.rendering
from poobrains import app, Markup

from . import color

def get_highlight_color():

    cc = app.scss_compiler.make_compilation()
    #cc.add_source(scss.compiler.SourceFile.from_filename('colors.scss'))
    cc.add_source(scss.compiler.SourceFile.from_string("@import 'colors.scss'"))
    app.scss_compiler.call_and_catch_errors(cc.run)
    cc.rules[0].namespace._variables
    scss_color = cc.rules[0].namespace.variables['$color-highlight']
    return color.Color(red=scss_color.rgb[0] / 255.0, green=scss_color.rgb[1] / 255.0, blue=scss_color.rgb[2] / 255.0, alpha=float(scss_color.alpha))

palettes = {}
def palette(func):
    palettes[func.__name__] = func
    return func


@palette
def contrast_neighbors(base_color: color.Color, length: int) -> list:

    palette = []
    for i in range(0, length): 

        color = base_color.clone()
        if i % 2 != 0:
            color.hue += 180 # make this a complementary palette */
    
        color.hue += (180/length) * i
        palette.append(color)

    return palette

#@palette
#def hue_progression(base_color, 

@palette
def single_value_progression(base_color: color.Color, length: int) -> list:

    if base_color.value < 0.25:
        raise ValueError(f"Base color {base_color} .value too low, must be >= 0.25")

    palette = [base_color]

    step = (base_color.value -0.05) / (length - 1)

    for multiplier in range(1, length):
        variant_color = base_color.clone()
        variant_color.value -= step * multiplier
        palette.append(variant_color)

    return palette

@palette
def bipolar_color_progression(color_a: color.Color, color_b: color.Color, length: int) -> list:

    lightness_delta = 1 - color_a.lightness
    lightness_steps = int(length / 2)
    lightness_step = lightness_delta / lightness_steps

    palette = []
    for i in range(0, length):

        if i < lightness_steps:
            c = color_a.clone()
            c.lightness += lightness_step * i
        elif i == lightness_steps:
            c = color.Color(red=1, green=1, blue=1)
        else:
            c = color_b.clone()
            c.lightness = 1 - ((i - lightness_steps + 1) / length)
        
        palette.append(c)

    return palette


@palette
def bipolar_blend(color_a: color.Color, color_b: color.Color, length: int) -> list:
    palette = [color_a]

    step = 1.0 / (length - 1)
    for multiplier in range(1, length):
        complementary_blend = color_b.clone()
        complementary_blend.alpha = step * multiplier * color_b.alpha
        variant_color = color_a.clone()
        variant_color.blend(complementary_blend)
        palette.append(variant_color)

    return palette

def create_palette(length):
    return contrast_neighbors(get_highlight_color(), length)

@app.admin.route('/palettes')
@poobrains.helpers.themed
def sample_palettes():

    output = ''

    for name, func in palettes.items():

        hl_color = get_highlight_color()
        if name in ('bipolar_color_progression', 'bipolar_blend'):
            b = hl_color.clone()
            b.hue += 180
            palette = func(hl_color, b, 5)
        else:
            palette = func(hl_color, 5)
        output += f'<div><h2>{name}</h2>'
        for color in palette:
            output += f'<div style="display: inline-block; width: 5rem; height: 5rem; margin-right: 1rem; background:{color.hexrgb()};">&nbsp;</div>'
        output += '</div>'

    return poobrains.rendering.RenderString(Markup(output), name="Palettes")
