#define NODE_ADDON_API_DISABLE_CPP_EXCEPTIONS
#include "include.h"

Napi::Value cpu_name(const Napi::CallbackInfo& info){
    Napi::Env env = info.Env();
    for (Hardware hardware:computer->getHardware()){

        if(hardware.getHardwareType()==HardwareType::Cpu){
            Napi::String str2= Napi::String::New(env, hardware.Name());
            return str2;
        }
    }
    return env.Null();
}

Napi::Array _cpu_data(const Napi::CallbackInfo& info,SensorType type){
    Napi::Env env = info.Env();
    Napi::Array vec; 
    int i=0;
    for (Hardware hardware:computer->getHardware()){
        if(hardware.getHardwareType()==HardwareType::Cpu){
            for(Hardware sub:hardware.getSubHardware()){
                for(Sensor sen:sub.getSensors()){
                    if(sen.getType()==type){
                        vec.Set<Napi::Number>(i,Napi::Number::New(env, sen.getValue()));
                    }
                }
                i++;
            }
        }
    }
    return vec;
}
Napi::Array cpu_speed(const Napi::CallbackInfo& info){
    return _cpu_data(info,SensorType::Clock);
}
Napi::Value cpu_power(const Napi::CallbackInfo& info){
    Napi::Env env = info.Env();
    for (Hardware hardware:computer->getHardware()){
        if(hardware.getHardwareType()==HardwareType::Cpu){
            for(Sensor sen:hardware.getSensors()){
                    if(sen.getType()==SensorType::Power){
                       return Napi::Number::New(env, sen.getValue()); 
                    }
                }
        }
    }
    return env.Null();
}
Napi::Value cpu_voltage(const Napi::CallbackInfo& info){
    Napi::Env env = info.Env();
    for (Hardware hardware:computer->getHardware()){
        if(hardware.getHardwareType()==HardwareType::Cpu){
            for(Sensor sen:hardware.getSensors()){
                    if(sen.getType()==SensorType::Voltage){
                       return Napi::Number::New(env, sen.getValue()); 
                    }
                }
        }
    }
    return env.Null();
}
Napi::Value cpu_load(const Napi::CallbackInfo& info){
    return _cpu_data(info,SensorType::Load);
}


Napi::Value cpu_temperature(const Napi::CallbackInfo& info){
    return _cpu_data(info,SensorType::Temperature);
}
