#include "bluetoothonlinedevice.h"

QMap <QString,BluetoothOnlineDevice* > BluetoothOnlineMan;      //通过QString可以查找与之关联的BluetooothOnlineDevice的值

BluetoothOnlineDevice *BON;

bool BluetoothScanDataDeal(QByteArray tem_MAC,QByteArray tem_Name,QString TemNameStatus)
{
    bool return_value;

    if(BluetoothOnlineMan.contains(tem_MAC.toHex()))
    {
        //Uupdata the signal value
        return_value = false;
    }
    else
    {
        BON = new BluetoothOnlineDevice();

        BON->MAC = tem_MAC;

        BON->Name = tem_Name;

        BON->NameStatus =TemNameStatus;

        BON->QListIndexLoc = 255;

        BluetoothOnlineMan.insert(tem_MAC.toHex(),BON);

        return_value =true;
    }
    return return_value;
}

bool BluetoothSignalUpdata(QByteArray tem_MAC,QByteArray tem_Signal)
{
    bool return_value;

    if(BluetoothOnlineMan.contains(tem_MAC.toHex()))
    {
        //Uupdata the signal value
        BluetoothOnlineMan[tem_MAC.toHex()]->Singal = tem_Signal;

        return_value = true;
    }
    else
    {
        return_value = false;
    }
    return return_value;
}

bool BluetoothBoradNameIsChanged(QByteArray tem_MAC,QByteArray temName)
{
    if(BluetoothOnlineMan.contains(tem_MAC.toHex()))
    {
        QMap <QString,BluetoothOnlineDevice* >::iterator tem;
        for(tem = BluetoothOnlineMan.begin();tem!= BluetoothOnlineMan.end();++tem)
        {
            if(tem.value()->MAC == tem_MAC)
            {
                if(tem.value()->Name != temName)
                {
                  tem.value()->Name = temName;
                  return true;
                }
                else
                {
                   return false;
                }
            }
        }
    }
    else
    {
        return  false;
    }
    return  false;
}

bool BluetoothIsExist(QByteArray tem_MAC)
{
    bool return_value;

    if(BluetoothOnlineMan.contains(tem_MAC.toHex()))
    {
        return_value = true;
    }
    else
    {
        return_value = false;
    }

    return return_value;
}

void BluetoothAddTheQListIndex(QByteArray tem_MAC,int QListIndex)
{
    QMap <QString,BluetoothOnlineDevice* >::iterator tem;
    for(tem = BluetoothOnlineMan.begin();tem!= BluetoothOnlineMan.end();++tem)
    {
        if(tem.value()->MAC == tem_MAC)
        {
            tem.value()->QListIndexLoc = QListIndex;
            qDebug()<<"QMap中begin是:"<<BluetoothOnlineMan.begin().value();
            qDebug()<<"QMap中end是:"<<BluetoothOnlineMan.end().value();
            qDebug()<<"QMap中tem.value是:"<<tem.value();
            qDebug()<<"QMap中tem.key是:"<<tem.key();
            return;
        }
    }
}

int BluetoothGetTheQListIndex(QByteArray tem_MAC)
{
    QMap <QString,BluetoothOnlineDevice* >::iterator tem;
    for(tem = BluetoothOnlineMan.begin();tem!= BluetoothOnlineMan.end();++tem)
    {
        if(tem.value()->MAC == tem_MAC)
        {
            return tem.value()->QListIndexLoc;
        }
    }
    return 255;
}

QString BluetoothGetShowString(QByteArray tem_MAC)
{
    QString returnstring;
    QMap <QString,BluetoothOnlineDevice* >::iterator tem;
    for(tem = BluetoothOnlineMan.begin();tem!= BluetoothOnlineMan.end();++tem)
    {
        if(tem.value()->MAC == tem_MAC)
        {
            returnstring = tem.value()->NameStatus+tem.value()->Name+"  信号强度:";
            if(tem.value()->Singal.toInt(NULL,16) == 0)
            {
               returnstring += "Unkown";
            }
            else
            {
                returnstring +=QString::number(tem.value()->Singal.toInt(NULL,16),10) ;
            }
            returnstring +="  dbm";
            return returnstring;
        }
    }
    return NULL;
}

void BluetoothBoradDataDeal(QByteArray tem_MAC,QByteArray tem_Signal,QByteArray tem_boarddata)
{
    if(!BluetoothOnlineMan.contains(tem_MAC.toHex()))
    {//
        return;
    }
    else
    {
        BluetoothOnlineMan[tem_MAC.toHex()]->Singal = tem_Signal;
    }
}

QStringList GetBluetoothList()
{
    QStringList Strlist;
    QMap <QString,BluetoothOnlineDevice* >::iterator tem;
    for(tem = BluetoothOnlineMan.begin();tem!= BluetoothOnlineMan.end();++tem)
    {
        Strlist << BluetoothGetShowString(tem.value()->MAC);
    }
    return Strlist;
}

void ClearTheBuletoothOnlineDeciveQmap()
{
    BluetoothOnlineMan.clear();
}

QByteArray GetBluetoothMAC(int QListIndex)
{
    QByteArray returnvalue;

    QMap <QString,BluetoothOnlineDevice* >::iterator tem;
    for(tem = BluetoothOnlineMan.begin();tem!= BluetoothOnlineMan.end();++tem)
    {
        if(tem.value()->QListIndexLoc==QListIndex)
        {
            returnvalue = tem.value()->MAC;
            break;
        }
    }
    return returnvalue;
}
