/*
 * constructor
 *
 */
pixel_styles_controller::pixel_styles_controller()
{
	srand((unsigned) time(0));

	mFrames = 0;
	mTcpServer = NULL;
	mUdpSocket = NULL;

	LOG_INFO << "Pixel Styles v" << VERSION << " started";

	mIniFile = new ini_parser((string) CONFIGURATION_DIRECTORY + "config.cfg");

	int width = mIniFile->get<size_t>("PIXEL_STYLES", "Width", 19);
	int height = mIniFile->get<size_t>("PIXEL_STYLES", "Height", 12);
	string device = mIniFile->get<string>("PIXEL_STYLES", "SpiDevice", "/dev/spidev0.0");
	strip_bytes_order bytesOrder = mIniFile->get<string>("PIXEL_STYLES", "RgbBytesOrder", "RGB").compare("RGB") ? bgrBytesOrder : rgbBytesOrder;

	mStrip = new led_strip(device.c_str(), bytesOrder, width, height);
	delete mIniFile;

	LOG_INFO << "Initialized with size " << width << "x" << height << " on SPI device " << device << " with " << (bytesOrder == rgbBytesOrder ? "RGB" : "BGR") << " bytes order";

	mIniFile = new ini_parser((string) SETTINGS_DIRECTORY + "application.cfg");

	mAliveTimer = new timer(1000000, bind(&pixel_styles_controller::alive, this));
	mPaintTimer = new timer(10000, bind(&pixel_styles_controller::paint, this));
	mPreviewTimer = new timer(50000, bind(&pixel_styles_controller::preview, this));
	mModesController = new modes_controller(width, height);

	mStaticColors.resize(mIniFile->get<size_t>("COLORS", "Count", 1));
	for (size_t i = 0; i < mStaticColors.size(); i++)
	{
		mStaticColors[i] = int_to_rgb(mIniFile->get<uint32_t>("COLORS", "Color" + to_string(i), 0x00FF0000));
	}

	mModesController->lock();

	mModesController->set_active_mode_name(mIniFile->get<string>("MODE", "ActiveMode", "Touch"));

	mIniFile->set<string>("MODE", "ActiveMode", mModesController->active_mode_name());
	mModesController->initialize(mStaticColors);

	bool active = mIniFile->get<bool>("MODE", "Active", true);
	int brightness = mIniFile->get<int>("MODE", "Brightness", 100);

	mModesController->set_active(active);
	mModesController->set_brightness(brightness);

	mStrip->setActive(active);
	mStrip->setBrightness(brightness);

	mModesController->unlock();

	LOG_INFO << "pixel_styles_controller initialized";
}
hsv_color int_to_hsv(uint32_t pIntColor)
{
	return rgb_to_hsv(int_to_rgb(pIntColor));
}