// Exact Rust translations of LibreMetaverse.Tests/BakerTests.cs.
// Source SHA-256: 7727140f0fbf2c77ed7a6f3f468798d8f7ba96c5d9889246a2bccd2d4bd8def2

use super::*;
use libremetaverse_imaging::{ManagedImage, ManagedImageImageChannels};
use libremetaverse_types::Color4;

fn gray_alpha_image(value: u8) -> ManagedImage {
    let channels = ManagedImageImageChannels(
        ManagedImageImageChannels::GRAY.0 | ManagedImageImageChannels::ALPHA.0,
    );
    let mut image = ManagedImage::new(4, 4, channels).unwrap();
    image.red.fill(value);
    image.alpha.fill(255);
    image
}

fn texture_data(
    image: ManagedImage,
    color: Color4,
    index: AvatarTextureIndex,
) -> AppearanceManagerTextureData {
    let mut data = AppearanceManagerTextureData::new().unwrap();
    data.texture = Some(assets::AssetTexture::new_with_managed_image(image).unwrap());
    data.color = color;
    data.texture_index = index;
    data
}

// parity-case: LibreMetaverse.Tests/BakerTests.cs::BakerTests.Bake_GrayscaleLayerTexture_DoesNotThrow::test c3fc1d2c16b5f8d69678a6cf05a3a275e0b657f3a6e7f6788f350f2a107200b3 translated
#[test]
fn bake_grayscale_layer_texture_does_not_throw() {
    let color = Color4::new_with_single_single_single_single(0.5, 0.25, 0.75, 1.0).unwrap();
    let data = texture_data(gray_alpha_image(128), color, AvatarTextureIndex::UpperShirt);
    let mut baker = imaging::Baker::new(BakeType::UpperBody).unwrap();
    baker.add_texture(data).unwrap();
    baker.bake().unwrap();
    assert!(baker.baked_texture().is_some());
}

// parity-case: LibreMetaverse.Tests/BakerTests.cs::BakerTests.Bake_GrayscaleHairTextureNoSkin_DoesNotThrow::test e48b737d9138e63cda98b5e80bf561a80d1dab5de78901ac9e1bb320ca281eb6 translated
#[test]
fn bake_grayscale_hair_texture_no_skin_does_not_throw() {
    let data = texture_data(
        gray_alpha_image(200),
        Color4::white(),
        AvatarTextureIndex::Hair,
    );
    let mut baker = imaging::Baker::new(BakeType::Head).unwrap();
    baker.add_texture(data).unwrap();
    baker.bake().unwrap();
    assert!(baker.baked_texture().is_some());
}

// parity-case: LibreMetaverse.Tests/BakerTests.cs::BakerTests.LoadResourceLayer_HeadColorTga_LoadsFromCharacterDirectory::test 5a9b80005a51f04a227ca392218725d50704d038783e04fc49bb955997c22e76 translated
#[test]
fn load_resource_layer_head_color_tga_loads_from_character_directory() {
    assert!(
        imaging::Baker::load_resource_layer("head_color.tga".into())
            .unwrap()
            .is_some()
    );
}

// parity-case: LibreMetaverse.Tests/BakerTests.cs::BakerTests.LoadResourceLayer_CachedResourceNotCorruptedByCallerMutation::test 18b458bbd7bb13d2b68711f7d3495193f1b4edac9c1f776930ad2c172f97cd49 translated
#[test]
fn load_resource_layer_cached_resource_not_corrupted_by_caller_mutation() {
    imaging::Baker::load_resource_layer("head_color.tga".into()).unwrap();
    let mut second = imaging::Baker::load_resource_layer("head_color.tga".into())
        .unwrap()
        .unwrap();
    let original_width = second.width;
    let original_height = second.height;
    second
        .resize_nearest_neighbor(original_width / 2, original_height / 2)
        .unwrap();
    let third = imaging::Baker::load_resource_layer("head_color.tga".into())
        .unwrap()
        .unwrap();
    assert_eq!(third.width, original_width);
    assert_eq!(third.height, original_height);
}

// parity-case: LibreMetaverse.Tests/BakerTests.cs::BakerTests.ResizeToBakeDimensions_SourceSmallerInOneAxisOnly_ScalesRatherThanTiles::test ce5bd346b48cb19dbc0248403eafefb07309d4e05e675e9a02db5a46d7cd65ff translated
#[test]
fn resize_to_bake_dimensions_source_smaller_in_one_axis_only_scales_rather_than_tiles() {
    let mut source = ManagedImage::new(2, 8, ManagedImageImageChannels::COLOR).unwrap();
    for y in 0..8 {
        for x in 0..2 {
            source.red[y * 2 + x] = u8::try_from(y * 10).unwrap();
        }
    }
    let result = imaging::Baker::resize_to_bake_dimensions(source, 4, 4)
        .unwrap()
        .unwrap();
    assert_eq!(result.width, 4);
    assert_eq!(result.height, 4);
    assert!(result.red[3 * 4] > 50);
}
