/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2025 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "PipewireSettings.h"
//#include <QDebug>
#include <string>

namespace UkuiAudioFramwork
{
#define LOG_LEVEL_KEY "log.level"
#define MIN_BUFFER_KEY "clock.min-quantum"
#define MAX_BUFFER_KEY "clock.max-quantum"
#define FORCE_SAMPLERATE_KEY "clock.force-rate"
#define FORCE_BUFFER_KEY "clock.force-quantum"
#define RATE_KEY "clock.rate"
#define ALLOWED_RATES_KEY "clock.allowed-rates"
#define QUANTUM_KEY "clock.quantum"

//-----------------------------------------------------------------------------

PipewireSettings::PipewireSettings(Pipewire *parent, uint32_t id, const spa_dict* props)
    :PipewireMetadata(parent, id, props)
{
//    connect(this, &QPipewireMetadata::onKeyUpdated, this, &QPipewireSettings::keyUpdated);
}

//-----------------------------------------------------------------------------

void PipewireSettings::keyUpdated(uint32_t id, const char *key, const char *type, const char *value)
{
//    if (strcmp(key, LOG_LEVEL_KEY)==0){
//        m_logLevel = static_cast<spa_log_level>(std::stoi(value));
//        emit logLevelChanged(m_logLevel);
//    } else if (strcmp(key, FORCE_SAMPLERATE_KEY)==0){
//        m_force_sampleRate = std::stoi(value);
//        emit force_sampleRateChanged(m_force_sampleRate);
//    } else if (strcmp(key, FORCE_BUFFER_KEY)==0) {
//        m_force_buffer = std::stoi(value);
//        emit force_bufferChanged(m_force_buffer);
//    } else if (strcmp(key, MIN_BUFFER_KEY)==0) {
//        m_minBuffer = std::stoi(value);
//        emit minBufferChanged(m_minBuffer);
//    } else if (strcmp(key, MAX_BUFFER_KEY)==0) {
//        m_maxBuffer = std::stoi(value);
//        emit maxBufferChanged(m_maxBuffer);
//    // } else if (strcmp(key, RATE_KEY)==0) {
//    //     //TODO
//    // } else if (strcmp(key, ALLOWED_RATES_KEY)==0) {
//    //     //TODO
//    // } else if (strcmp(key, QUANTUM_KEY)==0) {
//    //     //TODO
//    } else {
//        qWarning() << "Unrecognized pipewire setting \"" << key << "\" with value \"" << value << '"';
//    }
}

//-----------------------------------------------------------------------------

void PipewireSettings::setLogLevel(int newLogLevel)
{
    if (newLogLevel == m_logLevel) return;
    std::string value = std::to_string(newLogLevel);
    this->setProperty(LOG_LEVEL_KEY, value.c_str());
}

void PipewireSettings::setMinBuffer(int newMinBuffer)
{
    if (newMinBuffer == m_minBuffer) return;
    std::string value = std::to_string(newMinBuffer);
    this->setProperty(MIN_BUFFER_KEY, value.c_str());
}

void PipewireSettings::setMaxBuffer(int newMaxBuffer)
{
    if (newMaxBuffer == m_maxBuffer) return;
    std::string value = std::to_string(newMaxBuffer);
    this->setProperty(MAX_BUFFER_KEY, value.c_str());
}

void PipewireSettings::setForce_sampleRate(int newSampleRate)
{
    if (newSampleRate == m_force_sampleRate) return;
    std::string value = std::to_string(newSampleRate);
    this->setProperty(FORCE_SAMPLERATE_KEY, value.c_str());
}

void PipewireSettings::setForce_buffer(int newBuffer)
{
    if (newBuffer == m_force_buffer) return;
    std::string value = std::to_string(newBuffer);
    this->setProperty(FORCE_BUFFER_KEY, value.c_str());
}
}
