#include "led_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::show(void)
{
    FastLED.show();
    delay(FASTLEDDLY);
}

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(unsigned int nrpixel = 0; nrpixel < element_number_pixels; nrpixel++)
    {
        element_pixels[setpix] = CHSV(huecolor,saturation, brightness);
        setpix = setpix + plus_minus;
        if( all_at_once == false)
        {
            show();
        }
    }
    if( all_at_once == true)
    {
        show();
    }
    delay(100);
    clean();
    show();
}

void elements::run_init(){
    element_current_pixel = -1;
}

void elements::run(hue_color  huecolor, bool to_outside){
     
    if(element_outside2inside == true)
    {
        if(to_outside == true)
        {
            if(element_current_pixel == -1)
            {
                element_current_pixel = element_first_pixel;
            }
            element_current_pixel = element_current_pixel == element_first_pixel?element_last_pixel:element_current_pixel-1;
        }
        else
        {
            if(element_current_pixel == -1)
            {
                element_current_pixel = element_last_pixel;
            }
            element_current_pixel = element_current_pixel == element_last_pixel?element_first_pixel:element_current_pixel+1;
        }
    }
    else
    {
        if(to_outside == false)
        {
            if(element_current_pixel == -1)
            {
                element_current_pixel = element_first_pixel;
            }
            element_current_pixel = element_current_pixel == element_first_pixel?element_last_pixel:element_current_pixel-1;
        }
        else
        {
            if(element_current_pixel == -1)
            {
                element_current_pixel = element_last_pixel;
            }
            element_current_pixel = element_current_pixel == element_last_pixel?element_first_pixel:element_current_pixel+1;
        }
    }
    element_pixels[element_current_pixel] = CHSV(huecolor.hue,huecolor.sat, huecolor.bright);
}

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(unsigned int nrpixel = element_first_pixel; nrpixel <= element_last_pixel; nrpixel++)
    {
        element_pixels[nrpixel] = CHSV(huecolor,saturation, brightness);
    }
}

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

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

void elements::ffill(unsigned int fraction, unsigned int fullvalue, uint8_t huecolor,  uint8_t saturation, uint8_t brightness)
{
    unsigned int value_of_pixel = fullvalue / element_number_pixels;            //this is what part one pixel of the element represents of the full value
    unsigned int fullpixels = fraction / value_of_pixel;                        //the number of pixels that represents the fraction (part of the fullvalue)
    unsigned int partialpixel = fraction % value_of_pixel;                      //the left over of the division of fraction and value of one pixel
    uint32_t temp_brightness = brightness * partialpixel;
    temp_brightness = temp_brightness / value_of_pixel;
    uint8_t partial_brightness = (uint8_t)temp_brightness;

    unsigned int current_pixel = 0;
    for(unsigned int 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(partialpixel > 0)
    {
        current_pixel = element_outside2inside==true? element_last_pixel - fullpixels : element_first_pixel + fullpixels;
        element_pixels[current_pixel] = CHSV(huecolor,saturation, partial_brightness);
    }

}

