PaintChatWindow::PaintChatWindow(QWidget *parent, std::string peerId, ChatWidget *chatWidget) :
    QMainWindow(parent), peerId(peerId), chatType(ChatWidget::CHATTYPE_UNKNOWN), chatWidget(chatWidget),
    ui(new Ui::PaintChatWindow)
{
    ui->setupUi(this);
    connect(ui->paintWidget,SIGNAL(haveUpdate()),SLOT(on_haveUpdate()));
    connect(ui->penWidthSpinBox,SIGNAL(valueChanged(int)),this,SLOT(penChanged()));

    ui->pushButton1px->setChecked(true);
    ui->pushButtonPen->setChecked(true);

    ui->paintWidget->color=Qt::black;
    ui->paintWidget->penWidth=1;

    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),SLOT(on_timer()));
    timer->start(500);

    QIcon icon ;
    icon.addPixmap(QPixmap(":/images/colors.png"));
    setWindowIcon(icon);
    
    QPixmap pix(24, 24);
    pix.fill(currentColor);
    ui->pushButtonPrimaryColor->setIcon(pix);

    chatType = chatWidget->chatType();
    if(chatType == ChatWidget::CHATTYPE_PRIVATE)
    {
        ui->label->setText(QString::fromStdString(std::string("Paintchat with ")+rsPeers->getPeerName(peerId)));
        setWindowTitle(QString::fromStdString(rsPeers->getPeerName(peerId)+" - PaintChat"));

        ImageResource res;
        res.fromQImage(ui->paintWidget->getImage());
        paintChatService->init(peerId,res);

        ui->progressBar->hide();
    }
    if(chatType == ChatWidget::CHATTYPE_LOBBY)
    {
        ui->progressBar->setRange(0,100);
        ui->progressBar->setValue(0);

        ChatLobbyId id;
        rsMsgs->isLobbyId(peerId, id);
        std::string nick, lobby;
        std::list<ChatLobbyInfo> cil;
        rsMsgs->getChatLobbyList(cil);
        std::list<ChatLobbyInfo>::iterator it;
        for(it = cil.begin(); it != cil.end(); it++)
        {
            ChatLobbyInfo ci = *it;
            if(ci.lobby_id == id)
            {
                nick = ci.nick_name;
                lobby = ci.lobby_name;
                break;
            }
        }
        std::string label = nick + "@" + lobby;
        ui->label->setText(QString::fromStdString(label));
        setWindowTitle(QString::fromStdString(label + " - PaintChat"));
    }
}
PaintChatWindow::PaintChatWindow(QWidget *parent, ChatId chatId, ChatWidget *chatWidget) :
	QMainWindow(parent), chatId(chatId), chatType(ChatWidget::CHATTYPE_UNKNOWN), chatWidget(chatWidget),
    ui(new Ui::PaintChatWindow)
{
    ui->setupUi(this);
    connect(ui->paintWidget,SIGNAL(haveUpdate()),SLOT(on_haveUpdate()));
    connect(ui->penWidthSpinBox,SIGNAL(valueChanged(int)),this,SLOT(penChanged()));

    ui->pushButton1px->setChecked(true);
    ui->pushButtonPen->setChecked(true);

    ui->paintWidget->color=Qt::black;
    ui->paintWidget->penWidth=1;

    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),SLOT(on_timer()));
    timer->start(500);

    QIcon icon ;
    icon.addPixmap(QPixmap(":/images/colors.png"));
    setWindowIcon(icon);
    
    QPixmap pix(24, 24);
    pix.fill(currentColor);
    ui->pushButtonPrimaryColor->setIcon(pix);

    chatType = chatWidget->chatType();
// Disabled
//    if(chatType == ChatWidget::CHATTYPE_PRIVATE)
//    {
//        ui->label->setText(QString::fromStdString(std::string("Paintchat with ")+rsPeers->getPeerName(peerId)));
//        setWindowTitle(QString::fromStdString(rsPeers->getPeerName(peerId)+" - PaintChat"));

//        ImageResource res;
//        res.fromQImage(ui->paintWidget->getImage());
//        paintChatService->init(peerId,res);

//        ui->progressBar->hide();
//    }
    if(chatType == ChatWidget::CHATTYPE_LOBBY)
    {
        ui->progressBar->setRange(0,100);
        ui->progressBar->setValue(0);

        ChatLobbyId id;
		id = chatId.toLobbyId();
        std::string nick, lobby;

		RsGxsId gxsid;
		RsIdentityDetails details;
		rsMsgs->getIdentityForChatLobby(id, gxsid);
		for(int i=0;i<3;++i)
			if(rsIdentity->getIdDetails(gxsid,details))
				break ;
			else
				usleep(1000*300) ;
		nick = details.mNickname;

		ChatLobbyInfo info;
		rsMsgs->getChatLobbyInfo(id, info);
		lobby = info.lobby_name;

        std::string label = nick + "@" + lobby;
        ui->label->setText(QString::fromStdString(label));
        setWindowTitle(QString::fromStdString(label + " - PaintChat"));
    }

	ui->lblWarning->setVisible(false);
}