#include "threadexportprogrampro.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <QDataStream>
#include <globaldefine.h>

ThreadExportProgramPro::ThreadExportProgramPro()
{
    m_createfile = new QDir();

}
ThreadExportProgramPro::~ThreadExportProgramPro()
{
    if(m_createfile) {
         m_createfile = Q_NULLPTR;
         delete m_createfile;
     }

}
void ThreadExportProgramPro::SetSourceDirAndDestDir(QString strSource,QString strDest)
{

    m_strSource=strSource;
    m_strDest=strDest;

}
void ThreadExportProgramPro::run()
{
    totalCopySize=0;
    m_strSource.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
    m_strDest.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);


    copyDirectoryFiles(m_strSource,m_strDest,true);
}
bool ThreadExportProgramPro::copyFileToPath(QString sourceDir, QString toDir, bool coverFileIfExist)
{
    toDir.replace("\\","/");
    if (sourceDir == toDir){
        return true;
    }
    if (!QFile::exists(sourceDir)){
        return false;
    }
    bool exist = m_createfile->exists(toDir);
    if (exist){
        if(coverFileIfExist){
            m_createfile->remove(toDir);
        }
    }


    if(!QFile::copy(sourceDir, toDir)) {
        return false;
    }
    return true;
}


bool ThreadExportProgramPro::copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
{
    QDir sourceDir(fromDir);
    QDir targetDir(toDir);
    qDebug() << "copyDirectoryFiles:" << fromDir << toDir;
    if(!targetDir.exists()){
        if(!targetDir.mkdir(targetDir.absolutePath())) {
            return false;
        }
    }
    QFileInfoList fileInfoList = sourceDir.entryInfoList();


    if(m_firstRead) {
        int isfileTMP = 0;
        qDebug() << "a copyDirectoryFiles:" << fileInfoList.count();
        foreach(QFileInfo fileInfo, fileInfoList){
            if(fileInfo.isFile()) {
                isfileTMP++;
            }
        }
        m_total = fileInfoList.count() - 2 - isfileTMP; // 2为.和..
        m_value = 0;
        m_firstRead = false;
        qDebug() << "a copyDirectoryFiles:" << fileInfoList.count() <<m_total << isfileTMP;
        emit sigCopyDirStation(m_value/m_total);
        if(m_value == m_total) {
            m_firstRead = true;
            emit sigCopyDirStation(1);
            emit sigCopyDirOver();
        }
    }
    else {
        m_value++;
        qDebug() << "a copyDirectoryFiles:" << m_value <<m_total;
        emit sigCopyDirStation(m_value/m_total);
        if((m_value/m_total == 1) || (m_value > m_total) || (m_value == m_total)) {
            m_firstRead = true;
            emit sigCopyDirOver();
        }
    }
    foreach(QFileInfo fileInfo, fileInfoList){
        if(fileInfo.fileName() == "." || fileInfo.fileName() == "..") {
            continue;
        }
        if(fileInfo.isDir()){    //< 当为目录时，递归的进行copy
            if(!copyDirectoryFiles(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()), coverFileIfExist)) {
                return false;
            }
        } else{            //当允许覆盖操作时，将旧文件进行删除操作
            if(coverFileIfExist && targetDir.exists(fileInfo.fileName())){
                targetDir.remove(fileInfo.fileName());
            }
            /// 进行文件copy
            if(!QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))){
                return false;
            }
            else {
                totalCopySize+=fileInfo.size();
                emit sigSendExportProgressValue(totalCopySize);
            }
//            if(!copyFile(fromDir+MACRO_FENGEFU+fileInfo.fileName(),toDir+MACRO_FENGEFU+fileInfo.fileName())){
//                 return false;
//            }
        }
    }
    return true;
}
bool ThreadExportProgramPro::copyFile(const QString &fromFIleName, const QString &toFileName)
{
    char* byteTemp = new char[4096];//字节数组
    int fileSize = 0;
    QFile tofile;
    int iCurFileSize=0;
    tofile.setFileName(toFileName);
    QDataStream out(&tofile);
    out.setVersion(QDataStream::Qt_4_8);

    QFile fromfile;
    fromfile.setFileName(fromFIleName);
    if(!fromfile.open(QIODevice::ReadOnly)){
        qDebug() << "open fromfile failed！！！";
        return false;
    }
    fileSize = fromfile.size();
    QDataStream in(&fromfile);

    in.setVersion(QDataStream::Qt_4_8);
    while(!in.atEnd())
    {
        int readSize = 0;
        //读入字节数组,返回读取的字节数量，如果小于4096，则到了文件尾
        readSize = in.readRawData(byteTemp, 4096);
        out.writeRawData(byteTemp, readSize);
        totalCopySize += readSize;
        iCurFileSize+=readSize;
        emit sigSendExportProgressValue(totalCopySize);
    }
    if(iCurFileSize == fileSize){
        tofile.setPermissions(QFile::ExeUser);
        fromfile.close();
        tofile.close();
        return true;
    }
    else
        return false;
}
