#ifndef GLOBALINFO_H
#define GLOBALINFO_H

#include <QObject>
#include "configinfo.h"
#include "struct_/projectinfo.h"
#include <QMap>
#include "handle/projectinfomanger.h"
#include "handle/packinfomanger.h"
#include "handle/printerthreadhandle.h"
#include "handle/serialthreadhandle.h"
#include "handle/remotepackconfig.h"
#include "handle/projectinfomanger.h"
#include "handle/autosyncpackinfo.h"
#include "struct_/modelinfo.h"

class GlobalInfo : public QObject
{
    Q_OBJECT
private:
    explicit GlobalInfo(QObject *parent = nullptr);


public:
    static GlobalInfo * this_();

    // 所有配置都从这儿加载和写入，不允许在新建ConfigInfo
    inline ConfigInfo * config() {return &_config;}
    inline const UserInfo & user() const {return _users;}

    inline QMap<QString,QSharedPointer<ProjectInfo>> project() {return _project;}
    inline QList<QSharedPointer<ProjectInfo>> serverInfo() {return _serverInfo;}
    inline QList<QSharedPointer<ProjectInfo>> cacheInfo() {return _cacheInfo;}

    QList<QSharedPointer<ProjectInfo>> searchProject(const QString & batchno , const QString & specs ="");

    bool addCacheInfo(QSharedPointer<ProjectInfo> & info);
    inline PackInfoManger * packInfoManger() {return packmanger;}
    inline ProjectInfoManger * projectInfoManger() {return _projectmanger;}

    QString packNum();

    double currentWidght;

    inline bool isNoLine() {return _isNoLine;}

    inline PackInfoManger * packLocalManger(){return packmanger;}

    inline QSharedPointer<ProjectInfo> project(const QString & name){return _project.value(name);}

    //打包配置
    inline const DaBaoConfig & dbConfig() const {return _dbConfig;}
    // 等级配置
    inline const QList<DengJiConfigItem> & dengJiList() {return _dengJiList;}
    //仓库配置
    inline const QList<WarehouseConfig> & wareList() {return _wareList;}
    //箱单模板
    inline const QList<QSharedPointer<BoxModel>> & BoxModelList() {return boxModelList;}
    //码单模板
    inline const QList<QSharedPointer<PackModel>> & PackModelList() {return packModelList;}

    void printerBox(QSharedPointer<BoxInfo> info);
    void printerBox1(QSharedPointer<BoxInfo> info);
    void printerBoxModel(QSharedPointer<BoxInfo> info,int ModelIndex);
    void printerPack(QSharedPointer<PackInfo> info);
    void printerPackModel(QSharedPointer<PackInfo> info,int ModelIndex);
    void setIslogin(bool login){ islogin = login;}

    // 同步调用
    void doSync(QDate & tm);
    //推出帐号别忘调用
    void logOut();
signals:
    void dbConfigUpdated();
    void dengJiUped();
    void doInited();
    void selectproject();
    void logOuted();
    void wareUped();
    void boxModelUped();
    void packModelUpde();
public slots:
    void start(UserInfo & user,bool online);
    void selectProject(bool online);
    void selectBoxModel();
    void selectPackModel();

private slots:
    void syncProject(ProjectInfoManger * manger, bool inited);
    void configUpdate(const DaBaoConfig & config);
    void dengJIConfigUp(const QList<DengJiConfigItem> & dengJis);
    void wareConfigUp(const QList<WarehouseConfig> ware);
private:
    QMap<QString,QSharedPointer<ProjectInfo>> _project;
    QList<QSharedPointer<ProjectInfo>> _serverInfo;
    QList<QSharedPointer<ProjectInfo>> _cacheInfo;
    friend class ProjectInfoManger;

    DaBaoConfig _dbConfig;
    QList<DengJiConfigItem> _dengJiList;
    QList<WarehouseConfig> _wareList;
    QList<QSharedPointer<BoxModel>> boxModelList;
    QList<QSharedPointer<PackModel>> packModelList;

    void initInfo();
private:
    ConfigInfo _config;
    QString pack_num;
    UserInfo _users;
    bool _isNoLine;
    bool islogin = false;
private:
    PackInfoManger * packmanger;
    PrinterThreadHandle * _printer;
    SerialThreadHandle * _serial;
    RemotePackConfig * _remoteConfig;
    ProjectInfoManger * _projectmanger;
    AutoSyncPackInfo * _syncPack;
};


#endif // GLOBALINFO_H
