#include "VrConfig.h"
#include <iostream>
#include <vector>
#include <direct.h>
#include <string>

using namespace tinyxml2;

CVrConfig::CVrConfig()
{
    // 构造函数
}

CVrConfig::~CVrConfig()
{
    // 析构函数
}

ConfigResult CVrConfig::LoadConfig(const std::string& filePath)
{
    ConfigResult result;
    
    // 使用tinyxml2库加载XML文件
    XMLDocument doc;
    XMLError err = doc.LoadFile(filePath.c_str());
    if (err != XML_SUCCESS)
    {
        std::cerr << "open config file failed: " << filePath << std::endl;
        return result;
    }

    // 获取根元素
    XMLElement* root = doc.RootElement();
    if (!root || std::string(root->Name()) != "VrConfig")
    {
        std::cerr << "config file format error: root element is not VrConfig" << std::endl;
        return result;
    }

    // 解析摄像头列表
    XMLElement* camerasElement = root->FirstChildElement("Cameras");
    if (camerasElement)
    {
        XMLElement* cameraElement = camerasElement->FirstChildElement("Camera");
        while (cameraElement)
        {
            DeviceInfo camera;
            if (cameraElement->Attribute("name"))
                camera.name = cameraElement->Attribute("name");
            
            if (cameraElement->Attribute("ip"))
                camera.ip = cameraElement->Attribute("ip");
            
            result.cameraList.push_back(camera);
            cameraElement = cameraElement->NextSiblingElement("Camera");
        }
    }

    // 解析设备列表
    XMLElement* devicesElement = root->FirstChildElement("Devices");
    if (devicesElement)
    {
        XMLElement* deviceElement = devicesElement->FirstChildElement("Device");
        while (deviceElement)
        {
            DeviceInfo device;
            if (deviceElement->Attribute("name"))
                device.name = deviceElement->Attribute("name");
            
            if (deviceElement->Attribute("ip"))
                device.ip = deviceElement->Attribute("ip");
            
            result.deviceList.push_back(device);
            deviceElement = deviceElement->NextSiblingElement("Device");
        }
    }

    return result;
}

bool CVrConfig::SaveConfig(const std::string& filePath, ConfigResult& configResult)
{
    // 创建XML文档
    XMLDocument doc;
    
    // 添加声明
    XMLDeclaration* declaration = doc.NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
    doc.InsertFirstChild(declaration);

    // 创建根元素
    XMLElement* root = doc.NewElement("VrConfig");
    doc.InsertEndChild(root);

    // 添加摄像头列表
    XMLElement* camerasElement = doc.NewElement("Cameras");
    root->InsertEndChild(camerasElement);
    
    for (const auto& camera : configResult.cameraList)
    {
        XMLElement* cameraElement = doc.NewElement("Camera");
        cameraElement->SetAttribute("name", camera.name.c_str());
        cameraElement->SetAttribute("ip", camera.ip.c_str());
        camerasElement->InsertEndChild(cameraElement);
    }

    // 添加设备列表
    XMLElement* devicesElement = doc.NewElement("Devices");
    root->InsertEndChild(devicesElement);
    
    for (const auto& device : configResult.deviceList)
    {
        XMLElement* deviceElement = doc.NewElement("Device");
        deviceElement->SetAttribute("name", device.name.c_str());
        deviceElement->SetAttribute("ip", device.ip.c_str());
        devicesElement->InsertEndChild(deviceElement);
    }

    // 保存到文件
    XMLError err = doc.SaveFile(filePath.c_str());
    if (err != XML_SUCCESS)
    {
        std::cerr << "无法保存配置文件: " << filePath << std::endl;
        return false;
    }

    return true;
}


/**
 * @brief 创建实例
 * @return 实例
 */
bool IVrConfig::CreateInstance(IVrConfig** ppVrConfig)
{
    *ppVrConfig = new CVrConfig()   ;
    return *ppVrConfig != nullptr;
}
