#pragma once

#include <vulkan/vulkan.h>
#include "SceneObject.h"


/**
 * Represents a house object within a Vulkan-rendered scene.
 * Inherits from SceneObject to leverage common object properties and functionalities.
 */
class RockObject : public SceneObjectWithPhysics{
    public:
    
     /**
     * Constructor for the HouseObject.
     * Initializes the house geometry and invokes the base SceneObject constructor.
     * Scalability
     * 
     * @param dev The Vulkan device.
     * @param phyDev The physical Vulkan device.
     * @param descPool The descriptor pool for allocating descriptor sets.
     * @param descSetLayout The layout of the descriptor set.
     * @param queue The Vulkan queue to which rendering commands will be submitted.
     */
        RockObject (
            VkDevice dev, 
            VkPhysicalDevice phyDev, 
            VkDescriptorPool descPool, 
            VkDescriptorSetLayout descSetLayout, 
            VkQueue queue, 
            Physics::GamePhysicsManager& inputPhysicsManager, 
            const glm::vec3& initialCollisionBoxPosition, 
            const glm::vec3& initialCollisionBoxHalfSidelengths,
            VkSampler sampler,
            VkSampler normalSampler,
            VkCommandPool cmdPool,
            bool sync,
            PFN_vkCmdPipelineBarrier2KHR pfn,
            glm::mat4 transformation,
            ShadowMap& shadowMap,
            const float& inputMass = 0.f)
        : SceneObjectWithPhysics(
        dev, 
        phyDev, 
        descPool, 
        descSetLayout, 
        queue, 
        {
            {{-1.0f, -1.0f, -1.0f}}, // Bottom-left back corner
            {{1.0f, -1.0f, -1.0f}},  // Bottom-right back corner
            {{1.0f, -1.0f, 1.0f}},   // Bottom-right front corner
            {{-1.0f, -1.0f, 1.0f}},  // Bottom-left front corner
            {{-1.0f, 1.0f, -1.0f}}, // Top-left back corner
            {{1.0f, 1.0f, -1.0f}},  // Top-right back corner
            {{1.0f, 1.0f, 1.0f}},   // Top-right front corner
            {{-1.0f, 1.0f, 1.0f}},  // Top-left front corner
        }, 
        {
            // Cube indices
            0, 1, 2, 2, 3, 0, // Front face
            1, 5, 6, 6, 2, 1, // Right face
            5, 4, 7, 7, 6, 5, // Back face
            4, 0, 3, 3, 7, 4, // Left face
            4, 5, 1, 1, 0, 4, // Bottom face
            3, 2, 6, 6, 7, 3  // Top face
        },
        inputPhysicsManager, 
        initialCollisionBoxPosition, 
        initialCollisionBoxHalfSidelengths,
        shadowMap,
        inputMass
        ){
        
        // glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
        // glm::mat4 scalingMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f));
        // glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
        uniformBuffer = vklCreateHostCoherentBufferWithBackingMemory(
            sizeof(UniformBuffer), VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
        );

        if (!load_from_obj("assets/meshes/rocks.obj")) {
            throw std::runtime_error("Failed to load rock.obj");
        }
        applyTransformationsLocal(this->data.positions, transformation);
        this->sceneObjectGeometry = createAndUploadIntoGpuMemory(this->data);

        for (size_t i = 0; i < materials.size(); ++i) {
            const auto& material = materials[i];

            ubo.ambientConstant = material.ambient[0];
            ubo.diffuseConstant = material.diffuse[0];
            ubo.specularConstant = material.specular[0];
            ubo.shininessConstant = material.shininess;

            if (!material.diffuse_texname.empty()) {
                std::string texture_path = "assets/textures/" + material.diffuse_texname;
                ImageAndView texture = loadImage(dev, queue, cmdPool, texture_path, sync, pfn);

                VkDescriptorSet descriptorSet = allocDescriptorSet(dev, descPool, descriptorSetLayout);

                ImageAndView normalImageAndView = loadImage(device, queue, cmdPool, "assets/textures/rocks_Normal.dds", sync, pfn);
                writeDescriptorSet(device, descriptorSet, uniformBuffer, texture.view, sampler, normalImageAndView.view, normalSampler);

                // Write shadowmap to correct binding in the descriptorset (default binding = 4)
                writeDescriptorSet(device, descriptorSet, shadowMap.m_image_view, shadowMap.m_image_sampler, shadowMap.m_descriptorBinding);

                materialDescriptorSets[i] = descriptorSet;
            }
        }
       
        ubo.color = glm::vec4(0.4f, 0.26f, 0.13f, 1.0f); //brown
        setObjectState(ObjectState::PhysicsControlled);

    }

    /**
     * Records the Vulkan rendering commands for the house object.
     * Binds the necessary Vulkan resources and issues the draw call.
     * 
     * @param commandBuffer The command buffer to record the rendering commands.
     * @param pipeline_layout The pipeline layout used for descriptor binding.
     * @param pipeline The graphics pipeline to use for rendering the house.
     */
    void recordRenderingCommands(VkCommandBuffer commandBuffer, VkPipeline hudPipeline, VkPipeline specularPipeline, VkPipeline normalPipeline, VkPipeline normalheightPipeline, const FrameTimer& frameTimer){        
        drawObject(normalPipeline);
    }
    


};

