#include "globalinfo.h"

GlobalInfo::GlobalInfo(QObject *parent) : QObject(parent)
{
    _isNoLine = false;
    _config.load();
    packmanger = nullptr;
    _printer = nullptr;
    _serial = nullptr;
}


GlobalInfo * GlobalInfo::this_()
{
    static GlobalInfo glo;
    return  & glo;
}


QString GlobalInfo::packNum()
{
//    auto ret = _config.packId;
    if(pack_num.length() == 4) return  pack_num;
    pack_num = QString::asprintf("%04d",_config.packId);
    if(pack_num.length() > 4)
        pack_num = pack_num.right(4);
    return pack_num;
}

bool GlobalInfo::addCacheInfo(QSharedPointer<ProjectInfo> & info)
{
    auto v = project(info->batch_no);
    if(!v.isNull()) return false;
    _cacheInfo.append(info);
    _project.insert(info->batch_no,info);
    return packmanger->syncLocalProjectInfo(_cacheInfo);
    packmanger->syncLocalProjectInfo(_cacheInfo);
    return true;
}



void GlobalInfo::start(UserInfo & user,bool online)
{
    _users = user;
    _isNoLine = online;
    packmanger  = nullptr;
    initInfo();
    _remoteConfig->refConfig();
    selectProject(online);
    selectBoxModel();
    selectPackModel();
}

void GlobalInfo::selectProject(bool online)
{
    _projectmanger->setStatus();
    if(online){
        _projectmanger->syncLocalList();
    } else {
        _projectmanger->loadServerList(false);
    }
}

void GlobalInfo::selectBoxModel()
{
    boxModelList =  packmanger->getBoxModel();
    emit boxModelUped();
}

void GlobalInfo::selectPackModel()
{
    packModelList = packmanger->getPackModel();
//    for(int i = 0;i<packModelList.size();i++){
//        qDebug()<<packModelList.at(i)->toObject();
//    }
    emit packModelUpde();
}

void GlobalInfo::initInfo()
{
    if(packmanger != nullptr) return;
    packmanger = new PackInfoManger(this);
    _printer = new PrinterThreadHandle();
    _printer->start();
    _serial = new SerialThreadHandle();
    _serial->start();
    _remoteConfig = new RemotePackConfig(this);
    connect(_remoteConfig,&RemotePackConfig::configUpdate,this,&GlobalInfo::configUpdate);
    connect(_remoteConfig,&RemotePackConfig::dengJIConfigUp,this,&GlobalInfo::dengJIConfigUp);
    connect(_remoteConfig,&RemotePackConfig::wareConfigUp,this,&GlobalInfo::wareConfigUp);
    _projectmanger = new ProjectInfoManger(packmanger,this);
    connect(_projectmanger,&ProjectInfoManger::inited,this,&GlobalInfo::syncProject);
    connect(_projectmanger,&ProjectInfoManger::synced,this,&GlobalInfo::syncProject);
    _syncPack = new AutoSyncPackInfo();
    connect(this,&GlobalInfo::doInited,_syncPack,&AutoSyncPackInfo::start);
    connect(this,&GlobalInfo::logOuted,_syncPack,&AutoSyncPackInfo::loadOut);
}

void GlobalInfo::printerBox(QSharedPointer<BoxInfo> info)
{
    if(_printer){
        emit _printer->printerBox(info);
    }
}

void GlobalInfo::printerBox1(QSharedPointer<BoxInfo> info)
{
    if(_printer){
        emit _printer->printerBox1(info);
    }
}

void GlobalInfo::printerBoxModel(QSharedPointer<BoxInfo> info,int ModelIndex)
{
    if(_printer){
        emit _printer->printerBoxModel(info,ModelIndex);
    }
}

void GlobalInfo::doSync(QDate & tm)
{
    if(_syncPack == nullptr || isNoLine()) return;
    _syncPack->sync(tm);
}

void GlobalInfo::printerPack(QSharedPointer<PackInfo> info)
{
    if(_printer){
        emit _printer->printerPack(info);
    }
}

void GlobalInfo::printerPackModel(QSharedPointer<PackInfo> info,int ModelIndex)
{
    if(_printer){
        emit _printer->printerPackModel(info,ModelIndex);
    }
}


void GlobalInfo::syncProject(ProjectInfoManger * manger, bool )
{
    _cacheInfo = manger->cacheInfo();
    _serverInfo = manger->serverInfo();
    _project.clear();
    for(int i = 0; i < _cacheInfo.size(); ++i){
        auto info = _cacheInfo.at(i);
        _project.insert(info->batch_no,info);
    }

    for(int i = 0; i < _serverInfo.size(); ++i){
        auto info = _serverInfo.at(i);
        _project.insert(info->batch_no,info);
    }
    if(!islogin){
        emit doInited();
        islogin = true;
    }
        emit selectproject();
}

void GlobalInfo::configUpdate(const DaBaoConfig & config)
{
    _dbConfig = config;
    emit dbConfigUpdated();
}
void GlobalInfo::dengJIConfigUp(const QList<DengJiConfigItem> & dengJis)
{
    _dengJiList = dengJis;
    emit dengJiUped();
}

void GlobalInfo::wareConfigUp(const QList<WarehouseConfig> ware)
{
    _wareList = ware;
    emit wareUped();
}


QList<QSharedPointer<ProjectInfo>> GlobalInfo::searchProject(const QString & batchno,const QString &specs)
{
    if(!batchno.isEmpty()&&!specs.isEmpty()){
        QList<QSharedPointer<ProjectInfo>> ret;
        for(int i = 0; i < _serverInfo.size(); ++i){
            auto info = _serverInfo.at(i);
            bool doIn =false;
            if(info->batch_no.contains(batchno)){
                doIn = true;
            }
            if(doIn){
                if(info->specs.contains(specs)){
                    ret.append(info);
                }
            }
        }
        return ret;
    }

    QList<QSharedPointer<ProjectInfo>> ret;
    for(int i = 0; i < _cacheInfo.size(); ++i){
        auto info = _cacheInfo.at(i);
        if(batchno != ""){
            if(info->batch_no.contains(batchno)){
                ret.append(info);
                continue;
            }
        }
        if(specs != ""){
            if(info->specs.contains(specs)){
                ret.append(info);
            }
        }
    }

    for(int i = 0; i < _serverInfo.size(); ++i){
        auto info = _serverInfo.at(i);
        if(batchno != ""){
            if(info->batch_no.contains(batchno)){
                ret.append(info);
                continue;
            }
        }
        if(specs != ""){
            if(info->specs.contains(specs)){
                ret.append(info);
            }
        }
    }
    return ret;
}
