// LedCommand.cpp
#include "LedCommand.h"
#include <algorithm>

LedCommand::LedCommand()
    : header{0xDD, 0x55, 0xEE},
      group_addr{0x00, 0x00},
      device_addr{0x00, 0x00},
      port(0x00),
      func_code(0x99),
      led_type(0x02),
      reserved{0x00, 0x00},
      data_length(3),
      ext_count(1),
      color_data{0x00, 0x00, 0x00},
      tail{0xAA, 0xBB}
{
}

void LedCommand::setColor(uint8_t r, uint8_t g, uint8_t b)
{
    color_data[0] = r;
    color_data[1] = g;
    color_data[2] = b;
}

void LedCommand::setExtCount(uint16_t count)
{
    ext_count = count;
}

std::vector<uint8_t> LedCommand::serialize() const
{
    std::vector<uint8_t> buf;
    buf.insert(buf.end(), std::begin(header), std::end(header));
    buf.insert(buf.end(), std::begin(group_addr), std::end(group_addr));
    buf.insert(buf.end(), std::begin(device_addr), std::end(device_addr));
    buf.push_back(port);
    buf.push_back(func_code);
    buf.push_back(led_type);
    buf.insert(buf.end(), std::begin(reserved), std::end(reserved));
    buf.push_back((data_length >> 8) & 0xFF);
    buf.push_back(data_length & 0xFF);
    buf.push_back((ext_count >> 8) & 0xFF);
    buf.push_back(ext_count & 0xFF);
    buf.insert(buf.end(), std::begin(color_data), std::end(color_data));
    buf.insert(buf.end(), std::begin(tail), std::end(tail));
    return buf;
}
