#include "graphics.hpp"
#include "color.h"
#include <sstream>
#include <iostream>

void Color::setValue(unsigned char r0, unsigned char g0, unsigned char b0)
{
    r = r0;
    g = g0;
    b = b0;
}

void Color::setValue(const Color &c)
{
    r = c.r;
    g = c.g;
    b = c.b;
}

bool Color::setValue(std::string str)
{
    // Hibás stringek szûrése
    if (str[0] != '#' || str.length() != 7)
    {
        std::cerr << "Incorrect string passed to Color::setValue (it does not start with '#')" << std::endl;
        return false;
    }
    std::string allowedChars = "0123456789abcdef";
    for (unsigned int i = 1; i < 7; i++)
        if (allowedChars.find(tolower(str[i])) == std::string::npos)
        {
            std::cerr << "Incorrect string passed to Color::setValue (it contains illegal characters)" << std::endl;
            return false;
        }

    // konvertálás
    unsigned int x, y, z;
    std::stringstream ss;
    ss << std::hex << str.substr(1,2);
    ss >> x;
    r = x;
    ss.clear();
    ss << std::hex << str.substr(3,2);
    ss >> y;
    g = y;
    ss.clear();
    ss << std::hex << str.substr(5,2);
    ss >> z;
    b = z;

    return true;
}

Color::Color()
{
    r = g = b = 0;
}

Color::Color(const Color &c)
{
    setValue(c);
}

Color::Color (unsigned char r0, unsigned char g0, unsigned char b0)
{
    setValue(r0, g0, b0);
}

Color::Color(std::string str)
{
    setValue(str);
}

Color& Color::operator= (const Color& c)
{
    setValue(c);
    return *this;
}

bool Color::operator= (const std::string str)
{
    return setValue(str);
}

void Color::getValue(unsigned char &r0, unsigned char &g0, unsigned char &b0) const
{
    r0 = r;
    g0 = g;
    b0 = b;
}

void colorize(const Color &c)
{
    genv::gout << genv::color(c.r, c.g, c.b);
}

void rectange(int x, int y, int width, int height)
{
    colorize(Black);
    genv::gout << genv::move_to(x,y) << genv::line(width, 0) << genv::line(0, height) << genv::line(-width,0) << genv::line(0, -height);
}

