/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ---------------------------------------------------------------------
 *
 * Portions of this file were modified and ported to Vala by Victor Vintorez in 2026.
 * These modifications are licensed under the European Union Public License (EUPL),
 * Version 1.2 or later. You may obtain a copy of the License at:
 * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
 */

using GLib;
using MaterialColorUtilities;

public class QuantizeCelebiTest : McuTestCase {
	private const int red = (int) 0xffff0000u;
	private const int green = (int) 0xff00ff00u;
	private const int blue = (int) 0xff0000ffu;
	private const int white = (int) 0xffffffffu;
	private const int random = (int) 0xff426088u;
	private const int max_colors = 256;

	public QuantizeCelebiTest() {
		base("quantize_celebi");
		add_test("random", test_1_random);
		add_test("red", test_1_red);
		add_test("green", test_1_green);
		add_test("blue", test_1_blue);
		add_test("5_blue", test_5_blue);
		add_test("2_red_3_green", test_2_red_3_green);
		add_test("1_red_1_green_1_blue", test_1_red_1_green_1_blue);
		add_test("stability", test_stability);
	}

	public override void set_up() {}

	public void test_1_random() {
		var result = QuantizerCelebi.quantize(new int[] { (int) 0xff141216u }, max_colors);
		var answer = result.get_keys_as_array();

		McuAssert.uints_equal(answer.length, 1);
		McuAssert.uints_equal(answer[0], 0xff141216u);
	}

	public void test_1_red() {
		var result = QuantizerCelebi.quantize(new int[] { red }, max_colors);
		var answer = result.get_keys_as_array();

		McuAssert.uints_equal(answer.length, 1);
		McuAssert.uints_equal(answer[0], red);
	}

	public void test_1_green() {
		var result = QuantizerCelebi.quantize(new int[] { green }, max_colors);
		var answer = result.get_keys_as_array();

		McuAssert.uints_equal(answer.length, 1);
		McuAssert.uints_equal(answer[0], green);
	}

	public void test_1_blue() {
		var result = QuantizerCelebi.quantize(new int[] { blue }, max_colors);
		var answer = result.get_keys_as_array();

		McuAssert.uints_equal(answer.length, 1);
		McuAssert.uints_equal(answer[0], blue);
	}

	public void test_5_blue() {
		var result = QuantizerCelebi.quantize(new int[] { blue, blue, blue, blue, blue }, max_colors);
		var answer = result.get_keys_as_array();

		McuAssert.uints_equal(answer.length, 1);
		McuAssert.uints_equal(answer[0], blue);
	}

	public void test_2_red_3_green() {
		var answer = QuantizerCelebi.quantize(new int[] { red, red, green, green, green }, max_colors);;

		McuAssert.uints_equal(answer.length, 2);
		McuAssert.uints_equal(answer[red], 2);
		McuAssert.uints_equal(answer[green], 3);
	}

	public void test_1_red_1_green_1_blue() {
		var answer = QuantizerCelebi.quantize(new int[] { red, green, blue }, max_colors);;

		McuAssert.uints_equal(answer.length, 3);
		McuAssert.uints_equal(answer[red], 1);
		McuAssert.uints_equal(answer[green], 1);
		McuAssert.uints_equal(answer[blue], 1);
	}

	public void test_stability() {
		var image_pixels = new int[] {
			(int) 0xff050505u, (int) 0xff000000u, (int) 0xff000000u, (int) 0xff000000u, (int) 0xff000000u, (int) 0xff090909u, (int) 0xff060404u,
			(int) 0xff030102u, (int) 0xff080607u, (int) 0xff070506u, (int) 0xff010001u, (int) 0xff070506u, (int) 0xff364341u, (int) 0xff223529u,
			(int) 0xff14251cu, (int) 0xff11221au, (int) 0xff1f3020u, (int) 0xff34443au, (int) 0xff64817eu, (int) 0xff638777u, (int) 0xff486d58u,
			(int) 0xff2f5536u, (int) 0xff467258u, (int) 0xff7fb7b9u, (int) 0xff6d8473u, (int) 0xff859488u, (int) 0xff7a947eu, (int) 0xff5f815du,
			(int) 0xff3a5d46u, (int) 0xff497469u, (int) 0xff737a73u, (int) 0xff656453u, (int) 0xff445938u, (int) 0xff657c4bu, (int) 0xff65715bu,
			(int) 0xff6a816eu, (int) 0xff667366u, (int) 0xff5b5547u, (int) 0xff3b391eu, (int) 0xff705e3du, (int) 0xff7f6c5eu, (int) 0xff6d7c6cu,
			(int) 0xffa99c9cu, (int) 0xff8b7671u, (int) 0xff6a3229u, (int) 0xff80514bu, (int) 0xff857970u, (int) 0xff4f5a4cu, (int) 0xff897273u,
			(int) 0xff745451u, (int) 0xff512823u, (int) 0xff78585au, (int) 0xff535552u, (int) 0xff40493fu, (int) 0xff151616u, (int) 0xff0a0c0cu,
			(int) 0xff050808u, (int) 0xff010303u, (int) 0xff000100u, (int) 0xff010200u, (int) 0xff191816u, (int) 0xff181818u, (int) 0xff0c0c0cu,
			(int) 0xff040404u, (int) 0xff0c0c0cu, (int) 0xff151514u, (int) 0xffb1c3b9u, (int) 0xffbfbfbfu, (int) 0xffbababau, (int) 0xffb7b7b7u,
			(int) 0xffb3b3b3u, (int) 0xffadadadu, (int) 0xff535756u, (int) 0xff575656u, (int) 0xff555555u, (int) 0xff555555u, (int) 0xff545454u,
			(int) 0xff474646u, (int) 0xff000000u, (int) 0xff000000u, (int) 0xff0b0b0bu, (int) 0xff0b0b0bu, (int) 0xff000000u, (int) 0xff000000u,
		};

		var count = 16;

		var answer1 = QuantizerCelebi.quantize(image_pixels, count);
		var answer2 = QuantizerCelebi.quantize(image_pixels, count);

		McuAssert.uints_equal(answer1.length, answer2.length);
		for (uint i = 0; i < answer1.length; i++) {
			McuAssert.uints_equal(answer1.get_keys_as_array()[i], answer2.get_keys_as_array()[i]);
		}
	}

	public override void tear_down() {}
}
