#include "test_harness.h"
#include "ui_core/color.h"

using pp::foundation::StatusCode;
using pp::ui::parse_hex_color;

namespace {

void parses_short_and_long_rgb_forms(pp::tests::Harness& h)
{
    const auto short_rgb = parse_hex_color("#0f8");
    const auto long_rgb = parse_hex_color("#102aFF");

    PP_EXPECT(h, short_rgb.ok());
    PP_EXPECT(h, short_rgb.value().r == 0x00);
    PP_EXPECT(h, short_rgb.value().g == 0xff);
    PP_EXPECT(h, short_rgb.value().b == 0x88);
    PP_EXPECT(h, short_rgb.value().a == 0xff);

    PP_EXPECT(h, long_rgb.ok());
    PP_EXPECT(h, long_rgb.value().r == 0x10);
    PP_EXPECT(h, long_rgb.value().g == 0x2a);
    PP_EXPECT(h, long_rgb.value().b == 0xff);
    PP_EXPECT(h, long_rgb.value().a == 0xff);
}

void parses_alpha_forms(pp::tests::Harness& h)
{
    const auto short_rgba = parse_hex_color("#1234");
    const auto long_rgba = parse_hex_color("#11223344");

    PP_EXPECT(h, short_rgba.ok());
    PP_EXPECT(h, short_rgba.value().r == 0x11);
    PP_EXPECT(h, short_rgba.value().g == 0x22);
    PP_EXPECT(h, short_rgba.value().b == 0x33);
    PP_EXPECT(h, short_rgba.value().a == 0x44);

    PP_EXPECT(h, long_rgba.ok());
    PP_EXPECT(h, long_rgba.value().r == 0x11);
    PP_EXPECT(h, long_rgba.value().g == 0x22);
    PP_EXPECT(h, long_rgba.value().b == 0x33);
    PP_EXPECT(h, long_rgba.value().a == 0x44);
}

void rejects_invalid_colors(pp::tests::Harness& h)
{
    const auto empty = parse_hex_color("");
    const auto missing_hash = parse_hex_color("112233");
    const auto bad_length = parse_hex_color("#12");
    const auto bad_character = parse_hex_color("#12xz45");

    PP_EXPECT(h, !empty.ok());
    PP_EXPECT(h, empty.status().code == StatusCode::invalid_argument);
    PP_EXPECT(h, !missing_hash.ok());
    PP_EXPECT(h, missing_hash.status().code == StatusCode::invalid_argument);
    PP_EXPECT(h, !bad_length.ok());
    PP_EXPECT(h, bad_length.status().code == StatusCode::invalid_argument);
    PP_EXPECT(h, !bad_character.ok());
    PP_EXPECT(h, bad_character.status().code == StatusCode::invalid_argument);
}

}

int main()
{
    pp::tests::Harness harness;
    harness.run("parses_short_and_long_rgb_forms", parses_short_and_long_rgb_forms);
    harness.run("parses_alpha_forms", parses_alpha_forms);
    harness.run("rejects_invalid_colors", rejects_invalid_colors);
    return harness.finish();
}
