#include "bluetooth.h"

Environment envPC = Environment::NOMAL;

Bluetooth::Bluetooth() : mFirstLoad(true) {

    qDebug() << Q_FUNC_INFO << "start" << "++ukccBluetooth========================" << __LINE__;
    ukccbluetoothconfig::setEnvPCValue();
    qDebug() << Q_FUNC_INFO << envPC << __LINE__;

    QTranslator * translator = new QTranslator(this);;
    translator->load("/usr/share/ukui-bluetooth/translations/ukcc-bluetooth_" + QLocale::system().name() + ".qm");
    QApplication::installTranslator(translator);

    pluginName = tr("Bluetooth");
    pluginType = DEVICES;

    QList <quint64> btServiceProcessId;
    bool m_bluetooth_service_process_flag = ukccbluetoothconfig::checkProcessRunning(BluetoothServiceExePath/*"bluetoothService"*/,btServiceProcessId);

    for(quint64 tempId : btServiceProcessId)
    {
        qDebug () << Q_FUNC_INFO << "========================" << tempId;
    }

    if (m_bluetooth_service_process_flag)
    {
        qDebug () << Q_FUNC_INFO << BluetoothServiceExePath << "is Running" ;
    }
    else
    {
        qDebug () << Q_FUNC_INFO << BluetoothServiceExePath << "is not Running" ;

        QList <quint64> btTrayProcessId;
        bool m_ukui_bluetooth_process_flag = ukccbluetoothconfig::checkProcessRunning(BluetoothTrayExePath,btTrayProcessId);
        if (m_ukui_bluetooth_process_flag)
        {
            qDebug () << Q_FUNC_INFO << BluetoothTrayExePath << "is Running" ;
            for (quint64 processId:btTrayProcessId)
            {
                qDebug () << Q_FUNC_INFO << "ProcessId:" << btTrayProcessId << "is Running" ;
                ukccbluetoothconfig::killAppProcess(processId);
            }
        }
        else
        {
            qDebug () << Q_FUNC_INFO << BluetoothTrayExePath << "is not Running" ;
        }
        ukccbluetoothconfig::launchBluetoothServiceStart(BluetoothServiceExePath);
    }

}

Bluetooth::~Bluetooth() {
    if (!mFirstLoad) {
//        delete pluginWidget;
    }
}

QString Bluetooth::plugini18nName() {
    return pluginName;
}

int Bluetooth::pluginTypes() {
    return pluginType;
}

QWidget *Bluetooth::pluginUi() {

    if (mFirstLoad) {
        mFirstLoad = false;
        pluginWidget = new BlueToothMain;
//        pluginWidget = new BlueToothMainWindow;
    }
    if (!mFirstLoad && (nullptr != pluginWidget))
    {
        QTimer::singleShot(2000,this,[=]
        {
            pluginWidget->setbluetoothAdapterDiscoveringStatus(true);
        });
    }

    return pluginWidget;
}

const QString Bluetooth::name() const {
    return QStringLiteral("Bluetooth");
}

bool Bluetooth::isShowOnHomePage() const
{
    return true;
}

QIcon Bluetooth::icon() const
{
    return QIcon::fromTheme("bluetooth-active-symbolic");
}


bool Bluetooth::isEnable() const
{
    QString m_defaultAdapterAddr = BlueToothDBusService::getDefaultAdapterAddr();
    if (m_defaultAdapterAddr.isEmpty() || 6 != m_defaultAdapterAddr.split(":").size())
    {
        qInfo() << Q_FUNC_INFO << m_defaultAdapterAddr << endl;
        return false;
    }
    else
        return true;
}

void Bluetooth::plugin_leave()
{
    if (nullptr != pluginWidget)
    {
        pluginWidget->setbluetoothAdapterDiscoveringStatus(false);
        //pluginWidget->update();
    }
}

