# Copyright 2026 The HuggingFace Inc. team. All rights reserved.
#
# 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.

import json
import unittest

import numpy as np

from transformers.image_utils import PILImageResampling
from transformers.testing_utils import require_torch, require_torchvision, require_vision
from transformers.utils import is_torch_available, is_torchvision_available, is_vision_available

from ...test_image_processing_common import ImageProcessingTester, ImageProcessingTestMixin


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers.models.hunyuan_vl.image_processing_pil_hunyuan_vl import HunYuanVLImageProcessorPil, smart_resize

    if is_torchvision_available():
        from transformers.models.hunyuan_vl.image_processing_hunyuan_vl import HunYuanVLImageProcessor


class HunYuanVLImageProcessingTester(ImageProcessingTester):
    def __init__(self, **kwargs):
        # Random test inputs kwargs
        kwargs.setdefault("min_resolution", 32)
        kwargs.setdefault("max_resolution", 64)

        # Image processor init kwargs
        kwargs.setdefault("patch_size", 16)
        kwargs.setdefault("temporal_patch_size", 1)
        kwargs.setdefault("min_pixels", 32 * 32)
        kwargs.setdefault("max_pixels", 32 * 32)
        kwargs.setdefault("merge_size", 1)

        super().__init__(**kwargs)


@require_torch
@require_vision
@require_torchvision
class HunYuanVLImageProcessorTest(ImageProcessingTestMixin, unittest.TestCase):
    image_processor_tester_class = HunYuanVLImageProcessingTester

    def assert_image_processor_output(self, output, batch_size):
        grid_h = grid_w = 2
        patches_per_image = grid_h * grid_w
        expected_output_shape = (
            batch_size * patches_per_image,
            self.image_processor_tester.num_channels
            * self.image_processor_tester.temporal_patch_size
            * self.image_processor_tester.patch_size
            * self.image_processor_tester.patch_size,
        )
        expected_grid_thw = torch.tensor([[1, grid_h, grid_w]] * batch_size)

        self.assertEqual(tuple(output.pixel_values.shape), expected_output_shape)
        self.assertTrue((output.image_grid_thw == expected_grid_thw).all())

    def test_image_processor_to_json_string(self):
        for image_processing_class in self.image_processing_classes.values():
            image_processor = image_processing_class(**self.image_processor_dict)
            obj = json.loads(image_processor.to_json_string())
            for key, value in self.image_processor_dict.items():
                if key not in ["min_pixels", "max_pixels"]:
                    self.assertEqual(obj[key], value)

    def test_call_pil(self):
        for image_processing_class in self.image_processing_classes.values():
            image_processing = image_processing_class(**self.image_processor_dict)
            image_inputs = self.image_processor_tester.prepare_image_inputs(equal_resolution=True)
            for image in image_inputs:
                self.assertIsInstance(image, Image.Image)

            self.assert_image_processor_output(image_processing(image_inputs[0], return_tensors="pt"), batch_size=1)
            self.assert_image_processor_output(
                image_processing(image_inputs, return_tensors="pt"),
                batch_size=self.image_processor_tester.batch_size,
            )

    def test_call_numpy(self):
        for image_processing_class in self.image_processing_classes.values():
            image_processing = image_processing_class(**self.image_processor_dict)
            image_inputs = self.image_processor_tester.prepare_image_inputs(equal_resolution=True, numpify=True)
            for image in image_inputs:
                self.assertIsInstance(image, np.ndarray)

            self.assert_image_processor_output(image_processing(image_inputs[0], return_tensors="pt"), batch_size=1)
            self.assert_image_processor_output(
                image_processing(image_inputs, return_tensors="pt"),
                batch_size=self.image_processor_tester.batch_size,
            )

    def test_call_pytorch(self):
        for image_processing_class in self.image_processing_classes.values():
            image_processing = image_processing_class(**self.image_processor_dict)
            image_inputs = self.image_processor_tester.prepare_image_inputs(equal_resolution=True, torchify=True)
            for image in image_inputs:
                self.assertIsInstance(image, torch.Tensor)

            self.assert_image_processor_output(image_processing(image_inputs[0], return_tensors="pt"), batch_size=1)
            self.assert_image_processor_output(
                image_processing(image_inputs, return_tensors="pt"),
                batch_size=self.image_processor_tester.batch_size,
            )

    @unittest.skip(reason="HunYuanVL image processors are designed for 3-channel RGB images")
    def test_call_numpy_4_channels(self):
        pass

    def test_torchvision_image_processor_outputs_image_only_inputs(self):
        processor = HunYuanVLImageProcessor(
            min_pixels=32 * 32,
            max_pixels=32 * 32,
            patch_size=16,
            temporal_patch_size=1,
            merge_size=1,
        )
        image = Image.new("RGB", (32, 32), color="white")

        inputs = processor(images=[image], return_tensors="np")

        self.assertSetEqual(set(inputs.keys()), {"pixel_values", "image_grid_thw"})
        self.assertEqual(inputs["image_grid_thw"].shape, (1, 3))
        self.assertGreater(inputs["pixel_values"].shape[0], 0)

    def test_patch_count_contract_matches_pil_processor(self):
        torchvision_processor = HunYuanVLImageProcessor(
            min_pixels=32 * 32,
            max_pixels=64 * 64,
            patch_size=16,
            temporal_patch_size=1,
            merge_size=1,
        )
        pil_processor = HunYuanVLImageProcessorPil(
            min_pixels=32 * 32,
            max_pixels=64 * 64,
            patch_size=16,
            temporal_patch_size=1,
            merge_size=1,
        )

        image_kwargs = {"min_pixels": 32 * 32, "max_pixels": 64 * 64, "patch_size": 16, "merge_size": 1}
        self.assertEqual(
            torchvision_processor.get_number_of_image_patches(32, 64, image_kwargs),
            pil_processor.get_number_of_image_patches(32, 64, image_kwargs),
        )


@require_vision
class HunYuanVLImageProcessorPilTest(unittest.TestCase):
    def test_pil_image_processor_outputs_image_only_inputs(self):
        processor = HunYuanVLImageProcessorPil(
            min_pixels=32 * 32,
            max_pixels=32 * 32,
            patch_size=16,
            temporal_patch_size=1,
            merge_size=1,
        )
        image = Image.new("RGB", (32, 32), color="white")

        inputs = processor(images=[image], return_tensors="np")

        self.assertSetEqual(set(inputs.keys()), {"pixel_values", "image_grid_thw"})
        self.assertEqual(inputs["image_grid_thw"].shape, (1, 3))
        self.assertGreater(inputs["pixel_values"].shape[0], 0)

    def test_pil_image_processor_matches_reference_resize_with_hub_config(self):
        processor = HunYuanVLImageProcessorPil(
            min_pixels=32 * 32,
            max_pixels=32 * 32,
            patch_size=16,
            temporal_patch_size=1,
            merge_size=1,
            do_rescale=False,
            do_normalize=False,
            resample=PILImageResampling.LANCZOS,
        )
        image = (np.arange(3 * 17 * 19, dtype=np.uint16) % 256).astype(np.uint8).reshape(3, 17, 19)

        resized_height, resized_width = smart_resize(
            image.shape[-2], image.shape[-1], factor=16, min_pixels=32 * 32, max_pixels=32 * 32
        )
        reference_image = np.asarray(
            Image.fromarray(image.transpose(1, 2, 0)).resize((resized_width, resized_height))
        ).transpose(2, 0, 1)
        outputs = processor(image, return_tensors="np")
        reference_outputs = processor(reference_image, do_resize=False, return_tensors="np")

        np.testing.assert_array_equal(outputs.pixel_values, reference_outputs.pixel_values)
