/* -*- 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 "Util.h"
#include <iostream>

namespace UkuiAudioFramwork
{
std::string getModuleName(const Module& module)
{
    switch (module) {
        CASE(Module::ControlCenter);
        CASE(Module::Service);
        CASE(Module::Tray);
        DEFAULT(module);
    }
}

std::string getBackendName(const BackendType& type)
{
    switch (type) {
        CASE(BackendType::PULSEAUDIO);
        CASE(BackendType::PIPEWIRE);
        CASE(BackendType::ALSA);
        CASE(BackendType::JACK);
        DEFAULT(type);
    }
}

std::string getNodeTypeName(const NodeType& type)
{
    switch (type) {
        CASE(NodeType::INPUT);
        CASE(NodeType::OUTPUT);
        CASE(NodeType::SINK_INPUT);
        CASE(NodeType::SOURCE_OUTPUT);
        DEFAULT(type);
    }
}

bool hasKeyword(const std::vector<std::string>& stringList, const std::string& keyword)
{
    for (const auto& str : stringList) {
        if (str.find(keyword) != std::string::npos) {
            return true;
        }
    }
    return false;
}

DevicePortType getDevicePortType(const std::string& cardName, const std::string& portName)
{
    std::vector<std::string> stringList = {cardName, portName};

    if (hasKeyword(stringList, "multichannel")) {
        return DevicePortType::PORT_TYPE_MULTICHANNEL;
    }

    if (hasKeyword(stringList, "bluez") ||
        hasKeyword(stringList, "bluetooth")) {
        return DevicePortType::PORT_TYPE_BLUETOOTH;
    }

    if (hasKeyword(stringList, "linein") ||
        hasKeyword(stringList, "lineout")) {
        return DevicePortType::PORT_TYPE_LINEIO;
    }

    if (hasKeyword(stringList, "rear-mic") ||
        hasKeyword(stringList, "front-mic") ||
        hasKeyword(stringList, "[In] Mic2") ||
        hasKeyword(stringList, "headphone") ||
        hasKeyword(stringList, "Headphone") ||
        hasKeyword(stringList, "headset")) {
        return DevicePortType::PORT_TYPE_HEADSET;
    }

    if (hasKeyword(stringList, "usb")) {
        return DevicePortType::PORT_TYPE_USB;
    }

    if (hasKeyword(stringList, "hdmi") ||
        hasKeyword(stringList, "HDMI")) {
        return DevicePortType::PORT_TYPE_HDMI;
    }

    if (hasKeyword(stringList, "speaker") ||
        hasKeyword(stringList, "input-mic") ||
        hasKeyword(stringList, "[In] Mic1")) {
        return DevicePortType::PORT_TYPE_BUILTIN;
    }

    return DevicePortType::PORT_TYPE_UNKNOWN;
}

int calaPriorityByName(const std::string& name)
{
    if (!std::string::npos != name.find("bluez")) {
        return 2;
    }
    else if (!std::string::npos != name.find("usb")) {
        return 1;
    }

    return 0;
}

std::string DeviceInfo::toString() const
{
    return FORMAT("cardName: {}, cardDesc: {}, portName: {}, portLabel: {}, priority: {}, direction: {}, available: {}, enabled: {}, type: {}",
                  cardName, cardDesc, portName, portLabel, priority, direction, available, enabled, EnumToInt(type));
}

std::string DefaultDeviceInfo::toString() const
{
    return FORMAT("idx: {}, name: {}, cardName: {}, activePortName: {}, activePortLabel: {}",
                  idx, name, cardName, activePortName, activePortLabel);
}

std::string StreamMediaInfo::toString() const
{
    return FORMAT("name: {}, index: {}, volume: {}, mediaRole: {}, iconName: {}, binary: {}",
                  name, index, volume, mediaRole, iconName, binary);
}
}
