#include "sambaclient.h"

#include "mainwindow.h"

#include <QDebug>

SambaClient::SambaClient()
{

    m_bError = 0;

    connect(&m_Process1, SIGNAL(finished(int)), this, SLOT(Process1Finish(int)));
    connect(&m_Process1, SIGNAL(readyReadStandardOutput()), this, SLOT(Process1ReadyReadStandardOutput()));
    connect(&m_Process1, SIGNAL(readyReadStandardError()), this, SLOT(Process1ReadyReadStandardError()));

    connect(&m_Process2, SIGNAL(finished(int)), this, SLOT(Process2Finish(int)));
    connect(&m_Process2, SIGNAL(readyReadStandardOutput()), this, SLOT(Process2ReadyReadStandardOutput()));
    connect(&m_Process2, SIGNAL(readyReadStandardError()), this, SLOT(Process2ReadyReadStandardError()));

    connect(&m_Process3, SIGNAL(finished(int)), this, SLOT(Process3Finish(int)));
    connect(&m_Process3, SIGNAL(readyReadStandardOutput()), this, SLOT(Process3ReadyReadStandardOutput()));
    connect(&m_Process3, SIGNAL(readyReadStandardError()), this, SLOT(Process3ReadyReadStandardError()));
}


SambaClient::~SambaClient()
{

}


void SambaClient::Process1Finish(int nRet)
{
    QByteArray buffer;
    buffer.append(m_Process1.readAll());

}

void SambaClient::Process1ReadyReadStandardOutput()
{
    QByteArray buffer;
    buffer.append(m_Process1.readAllStandardOutput());

    m_strInfo = buffer.toStdString().c_str();
}

void SambaClient::Process1ReadyReadStandardError()
{
    QByteArray buffer;
    buffer.append(m_Process1.readAllStandardError());

    m_strInfo = buffer.toStdString().c_str();

    if(m_strInfo.contains("password for")==true)
    {
        m_bError = 0;
        return;
    }

    m_bError = 1;
}

void SambaClient::Process2Finish(int nRet)
{
    QByteArray buffer;
    buffer.append(m_Process2.readAll());

}

void SambaClient::Process2ReadyReadStandardOutput()
{
    QByteArray buffer;
    buffer.append(m_Process2.readAllStandardOutput());

    m_strInfo = buffer.toStdString().c_str();
}

void SambaClient::Process2ReadyReadStandardError()
{
    QByteArray buffer;
    buffer.append(m_Process2.readAllStandardError());

    m_strInfo = buffer.toStdString().c_str();

    if(m_strInfo.contains("not mounted")==false)
    {
        m_bError = 1;
    }
    else
    {

    }


}



void SambaClient::Process3Finish(int nRet)
{
    QByteArray buffer;
    buffer.append(m_Process3.readAll());

}

void SambaClient::Process3ReadyReadStandardOutput()
{
    QByteArray buffer;
    buffer.append(m_Process3.readAllStandardOutput());

    m_strInfo = buffer.toStdString().c_str();
}

void SambaClient::Process3ReadyReadStandardError()
{
    QByteArray buffer;
    buffer.append(m_Process3.readAllStandardError());

    m_strInfo = buffer.toStdString().c_str();

    if(m_strInfo.contains("password for")==true)
    {
        m_bError = 0;
        return;
    }

    m_bError = 1;
}


void SambaClient::Mount()
{
    m_bError = false;
    m_strInfo.clear();
    QProcess process1;
    process1.setStandardOutputProcess(&m_Process1);

    process1.start("echo 1");

    NetworkShareInfo* pNetworkShareInfo = MainWindow::GetCommonData()->GetNetworkShareInfo();

    QString strUserID = pNetworkShareInfo->m_strID;
    QString strUserPassword = pNetworkShareInfo->m_strPassword;
    QString strIP = pNetworkShareInfo->m_strIP;
    QString strFolder = pNetworkShareInfo->m_strShareFolder;
    QString strSMBVersion = pNetworkShareInfo->m_strVersion;

    //QString strConnect = QString("username=%1,password=%2 //%3/%4 /home/birdhead/samba").arg(strUserID).arg(strUserPassword).arg(strIP).arg(strFolder);
    //QString strCommand = QString("sudo -S mount -t cifs -o uid=1000,gid=1000,%1").arg(strConnect);

    QString strCommand = QString("sudo -S mount -t cifs //%1/%2 /home/birdhead/samba").arg(strIP).arg(strFolder);
    QString strOption = QString(" -o username=%1,password=%2,uid=1000,gid=1000,echo_interval=5").arg(strUserID).arg(strUserPassword);

    if(strSMBVersion.size()>0)
    {
        strOption += QString(",vers=%1").arg(strSMBVersion);
    }

    strCommand += strOption;

    qDebug() << strCommand;

    //m_Process.start("sudo -S mount -t cifs -o uid=1000,gid=1000,username=ftpadmin,password=ftpadmin? //192.168.1.19/test1 /home/birdhead/samba");
    m_Process1.start(strCommand.toStdString().c_str());
    m_Process1.setProcessChannelMode(QProcess::ForwardedChannels);

    if(!process1.waitForStarted())
        return;

    while(m_Process1.isOpen()==false || m_Process1.isReadable()==false)
    {
        QThread::usleep(100);
    }


    bool retval = false;
    QByteArray buffer;
    // To be fair: you only need to wait here for a bit with shutdown,
    // but I will still leave the rest here for a generic solution
    while ((retval = m_Process1.waitForFinished()))
    {
        buffer.append(m_Process1.readAll());
    }


    m_Process1.close();
    if(m_Process1.isOpen()==true)
    {
        m_Process1.terminate();
        m_Process1.waitForFinished(100);
        m_Process1.deleteLater();
    }

    process1.close();
    if(process1.isOpen()==true)
    {
        process1.terminate();
        process1.waitForFinished(100);
        process1.deleteLater();
    }
    buffer.clear();

    QString strInfo;

    if(m_bError==false)
    {
        //SetExportEnable(true);
        //strInfo = "Ready to Export 'Shared Network'";

        //ui->pushButton_Export->click();
    }
    else
    {
        //SetExportEnable(false);
        //strInfo = m_strInfo;
        //strInfo = "Check Your Network Server!!";
    }
    //SetInfoString(strInfo);
}

void SambaClient::Umount()
{
    if(m_bError==true)
    {
        return;
    }
    m_strInfo.clear();

    QProcess process1;

    process1.setStandardOutputProcess(&m_Process2);

    process1.start("echo 1");
    m_Process2.start("sudo -S umount /home/birdhead/samba");
    m_Process2.setProcessChannelMode(QProcess::ForwardedChannels);

    // Wait for it to start
    if(!process1.waitForStarted())
        return;

    bool retval = false;
    QByteArray buffer;
    // To be fair: you only need to wait here for a bit with shutdown,
    // but I will still leave the rest here for a generic solution
    while ((retval = m_Process2.waitForFinished()))
    {
        buffer.append(m_Process2.readAll());
    }


    m_Process2.close();
    if(m_Process2.isOpen()==true)
    {
        m_Process2.terminate();
        m_Process2.waitForFinished(100);
        m_Process2.deleteLater();
    }



    process1.close();
    if(process1.isOpen()==true)
    {
        process1.terminate();
        process1.waitForFinished(100);
        process1.deleteLater();
    }



    buffer.clear();

    if(m_bError==true)
    {
        //SetInfoString(m_strInfo);
    }
}



bool SambaClient::IsError()
{
    if(m_bError>0)
    {
        return true;
    }

    return false;
}


bool SambaClient::IsMount()
{
    m_bError = false;
    m_strInfo.clear();

    QString strCommand = QString("mount");

    m_Process3.start(strCommand.toStdString().c_str());

    bool retval = false;
    QByteArray buffer;
    // To be fair: you only need to wait here for a bit with shutdown,
    // but I will still leave the rest here for a generic solution
    while ((retval = m_Process3.waitForFinished()))
    {
        buffer.append(m_Process3.readAll());
    }

    m_Process3.close();
    if(m_Process3.isOpen()==true)
    {
        m_Process3.terminate();
    }


    buffer.clear();


    QStringList listLine = m_strInfo.split("\n");

    int i=0;
    for(i=0 ; i<listLine.size() ; i++)
    {
        QString strTmp = listLine[i];
        if(strTmp.contains("/home/birdhead/samba")==true)
        {
            return true;
        }
    }
    return false;
}
