#include "camp.h"
#include <QString>

cAmp::cAmp()
{
    m_numAmpModes=0;
    m_name="untitled";
    m_sensitvity=1;
    m_sensivityUnit=volt;
    m_numAmpModes=0;
    m_pAmpModes = nullptr;
}

cAmp::~cAmp()
{
    delete [] m_pAmpModes;
}

cAmp::cAmp(std::string name, double sensitivity, sensitivityUnit sensUnit)
{
    m_name=name;
    m_sensitvity=sensitivity;
    m_sensivityUnit=sensUnit;
    m_numAmpModes=0;
    m_pAmpModes = nullptr;
}

std::string cAmp::getName()
{
    return m_name;
}

void cAmp::setName(std::string name)
{
    m_name=name;
}

void cAmp::setName(QString name)
{
    m_name=name.toStdString();
}

void cAmp::setSensivity(double sens)
{
    m_sensitvity=sens;
}


void cAmp::setSensUnit(sensitivityUnit unit)
{
    m_sensivityUnit=unit;
}


double cAmp::getSensitivity()
{
    return m_sensitvity;
}

sensitivityUnit cAmp::getSensivityUnit()
{
    return m_sensivityUnit;
}

cAmpMode *cAmp::getAmpModes()
{
    return m_pAmpModes;
}

cAmpMode cAmp::getAmpMode(unsigned int idx)
{
    return m_pAmpModes[idx];
}

unsigned int cAmp::getAmpModesCount()
{
    return m_numAmpModes;
}

void cAmp::addAmpMode(cAmpMode mode)
{
    m_numAmpModes+=1;
    cAmpMode* tempAmpModes = new cAmpMode[m_numAmpModes];
    for (unsigned int i=0;i<m_numAmpModes-1;i++){
        tempAmpModes[i]=m_pAmpModes[i];
    }
    delete [] m_pAmpModes;
    m_pAmpModes=tempAmpModes;
    m_pAmpModes[m_numAmpModes-1]=mode;
}

bool cAmp::modifyAmpMode(unsigned int idx, cAmpMode mode)
{
    if (idx>=m_numAmpModes){
        return false;
    }else{
        m_pAmpModes[idx]=mode;
        return true;
    }
}

void cAmp::deleteAmpMode(unsigned int idx)
{
    if (m_numAmpModes>=1){
        m_numAmpModes-=1;
        cAmpMode* tempAmpModes = new cAmpMode[m_numAmpModes];
        for (unsigned int i=0;i<idx;i++){
            tempAmpModes[i]=m_pAmpModes[i];
        }
        for (unsigned int i=idx+1;i<m_numAmpModes+1;i++){
            tempAmpModes[i-1]=m_pAmpModes[i];
        }
        delete [] m_pAmpModes;
        m_pAmpModes=tempAmpModes;
    }

}

void cAmp::setAmpMode(unsigned int idx, cAmpMode mode)
{
    m_pAmpModes[idx]=mode;
}

void cAmp::print()
{
    std::cout << "Amplifier: "<<m_name<<std::endl<<"Input Sensitivity: "<<m_sensitvity<<std::endl<<"with " <<m_numAmpModes<<" Modes:"<<std::endl;
    for (unsigned int i=0; i<m_numAmpModes;i++){
        m_pAmpModes[i].print();
    }
    std::cout <<std::endl;
}

QDataStream& operator <<(QDataStream& out, cAmp& amp){
    //out << (quint64)amp.m_name.size();
    out << QString::fromStdString(amp.m_name);
    out << (qreal)amp.m_sensitvity;
    out << (qint32)amp.m_sensivityUnit;
    out << (uint)amp.m_numAmpModes;
    for (unsigned int i=0;i<amp.m_numAmpModes;i++){
        out << amp.m_pAmpModes[i];
    }
    return out;
}

QDataStream& operator >>(QDataStream& in, cAmp& amp){
   //quint64 size;
   //in >> size;
   QString name;
   in >> name;
   amp.setName(name);

   qreal sensitiviy;
   in >>sensitiviy;
   amp.setSensivity((double)sensitiviy);

   qint32 unit;
   in >> unit;
   amp.setSensUnit(sensitivityUnit(unit));

   uint numAmpModes;
   in >> numAmpModes;

   for (uint i=0;i<numAmpModes;i++){
        cAmpMode Mode;
        in >> Mode;
        amp.addAmpMode(Mode);
   }
   return in;
}
