#pragma once
#include "../component.h"
#include <string>

namespace ecs {

class ModelPathComponent : public Component {
public:
    ModelPathComponent() = default;
    explicit ModelPathComponent(const std::wstring& path) : m_path(path) {}
    ~ModelPathComponent() = default;

    void Initialize() override {}
    void Update(float deltaTime) override {}

    /**
     * Get the path of the model.
     * @return The path as a std::wstring.
     */
    const std::wstring& GetPath() const { return m_path; }
    /**
     * Set the path of the model.
     * @param path The path to set as a std::wstring.
     */
    void SetPath(const std::wstring& path) { m_path = path; }
    
    std::string convert_w_string_to_string(const std::wstring& wstr) const
    {
        if (wstr.empty()) return std::string();

        int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
        std::string str(size_needed, 0);
        WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &str[0], size_needed, NULL, NULL);
        return str;
    }

    std::string Serialize() const override {
        std::stringstream ss;
        ss << "ModelPathComponent:"
           << convert_w_string_to_string(m_path);
        return ss.str();
    }

    bool Deserialize(const std::string& data) override {
        std::stringstream ss(data);
        std::string type;
        std::getline(ss, type, ':');
    
        if (type != "ModelPathComponent") R_FALSE
    
        std::string modelPath;
        std::getline(ss, modelPath);
    
        SetPath(std::wstring(modelPath.begin(), modelPath.end()));
        R_TRUE
    }

    void OnImGuiRender() override {
        char path[260];
        std::string currentPath = convert_w_string_to_string(m_path);
        strcpy_s(path, currentPath.c_str());
        
        if (ImGui::InputText("Model Path", path, sizeof(path))) {
            SetPath(std::wstring(path, path + strlen(path)));
        }
    }

private:
    std::wstring m_path;
};

} // namespace ecs
