#include "clockelements.hpp"

void elements::init(CRGB *pixels, unsigned int first_led, unsigned int number_leds, hue_color huecolor,  bool outside2inside)
{
    element_pixels = pixels;
    element_first_pixel = first_led;
    element_number_pixels = number_leds;
    element_last_pixel = first_led + number_leds - 1;
    element_outside2inside = outside2inside;
    elements_huecolor = huecolor;
    //Serial.printf("Init pixels - first:%d last:%d number:%d outsidetoinside:%d\n", element_first_pixel, element_last_pixel, element_number_pixels, element_outside2inside);
}

void elements::clean(void)
{
    fill(0, 0, 0);
}

void elements::test(bool all_at_once)
{
    test(elements_huecolor.hue, elements_huecolor.sat, elements_huecolor.bright, all_at_once);
}

void elements::test(hue_color huecolor, bool all_at_once)
{
    test(huecolor.hue, huecolor.sat, huecolor.bright, all_at_once);
}

void elements::test(uint8_t huecolor, uint8_t saturation, uint8_t brightness, bool all_at_once)
{
    unsigned int setpix = element_outside2inside == true? element_last_pixel: element_first_pixel ;
    char plus_minus = element_outside2inside == true? -1:1;
    for(uint8_t nrpixel = 0; nrpixel < element_number_pixels; nrpixel++)
    {
        element_pixels[setpix] = CHSV(huecolor,saturation, brightness);
        setpix = setpix + plus_minus;
        if( all_at_once == false)
        {
            FastLED.show();
            delay(FASTLEDDLY);
        }
    }
    if( all_at_once == true)
    {
        FastLED.show();
        delay(FASTLEDDLY);
    }
    delay(100);
    clean();
    FastLED.show();
    delay(FASTLEDDLY);
}

void elements::fill(void)
{
    fill(elements_huecolor.hue, elements_huecolor.sat, elements_huecolor.bright);
}

void elements::fill(hue_color huecolor)
{
    fill(huecolor.hue, huecolor.sat, huecolor.bright);
}

void elements::fill(uint8_t huecolor, uint8_t saturation, uint8_t brightness)
{
    for(uint8_t nrpixel = element_first_pixel; nrpixel <= element_last_pixel; nrpixel++)
    {
        element_pixels[nrpixel] = CHSV(huecolor,saturation, brightness);
    }
}

void elements::ffill(unsigned int fraction)
{
    ffill(fraction, elements_huecolor.hue, elements_huecolor.sat, elements_huecolor.bright);
}

void elements::ffill(unsigned int fraction, hue_color huecolor)
{
    ffill(fraction, huecolor.hue, huecolor.sat, huecolor.bright);
}

void elements::ffill(unsigned int fraction, uint8_t huecolor,  uint8_t saturation, uint8_t brightness)
{
    uint8_t fullpixels = element_number_pixels / fraction;
    uint8_t partpixels = element_number_pixels % fraction;
    elements_fraction = brightness / (60/element_number_pixels);
    uint8_t part_brightness = elements_fraction * partpixels;

    unsigned int current_pixel = 0;

    for(uint8_t num = 0; num < fullpixels; num++)
    {
        current_pixel = element_outside2inside==true? element_last_pixel - num : element_first_pixel + num;
        element_pixels[current_pixel] = CHSV(huecolor,saturation, brightness);
    }

    if(partpixels > 0)
    {
        current_pixel = element_outside2inside==true? element_last_pixel - fullpixels : element_first_pixel + fullpixels;
        element_pixels[current_pixel] = CHSV(huecolor,saturation, part_brightness);
    }

}

