#include "OnLineControl.h"
#include "HeaderControl.h"
#include "LogControl.h"
#include <ClientCore.h>

OnlineControl::OnlineControl(wxWindow* parent, std::shared_ptr<ClientCore>& clientCore) : wxPanel(parent), clientCore_(clientCore)
{
    Init();
    InitCall();
}

OnlineControl::~OnlineControl()
{
}

void OnlineControl::SetHeaderControl(HeaderControl* headerControl)
{
    headerControl_ = headerControl;
}

void OnlineControl::SetLogControl(LogControl* logControl)
{
    logControl_ = logControl;
}

void OnlineControl::Init()
{
    btnFresh_ = new wxButton(this, wxID_ANY, _("Refresh"));
    lbCurState_ = new wxStaticText(this, wxID_ANY, _("Current State => "));
    elbCurState_ = new wxStaticText(this, wxID_ANY, _("Disconnected"));
    elbCurState_->SetForegroundColour(*wxBLUE);
    onLineList_ = new wxListBox(this, wxID_ANY);

    lbCurPoint_ = new wxStaticText(this, wxID_ANY, _("Commnunicate Point => "));
    elbCurPoint_ = new wxStaticText(this, wxID_ANY, _("None"));
    elbCurPoint_->SetForegroundColour(*wxBLUE);

    auto* leveSizerA = new wxBoxSizer(wxHORIZONTAL);
    leveSizerA->Add(lbCurState_, 0, wxEXPAND | wxALL, gBorder + 1);
    leveSizerA->Add(elbCurState_, 0, wxEXPAND | wxALL, gBorder + 1);

    auto* leveSizerB = new wxBoxSizer(wxHORIZONTAL);
    leveSizerB->Add(lbCurPoint_, 1, wxALL | wxCENTER, gBorder + 1);
    leveSizerB->Add(btnFresh_, 0, wxALL | wxCENTER, gBorder + 1);

    auto* topSizer = new wxBoxSizer(wxVERTICAL);
    topSizer->Add(leveSizerA, 0, wxEXPAND | wxALL, gBorder + 1);
    topSizer->Add(leveSizerB, 0, wxALL, gBorder + 1);
    topSizer->Add(elbCurPoint_, 0, wxEXPAND | wxALL, gBorder + 1);
    topSizer->Add(onLineList_, 1, wxEXPAND | wxALL, gBorder + 1);
    SetSizer(topSizer);
    Layout();

    Bind(wxEVT_BUTTON, &OnlineControl::OnFreshClients, this, btnFresh_->GetId());
}

void OnlineControl::SetConnectState(const wxString& state)
{
    elbCurState_->SetLabel(state);
}

void OnlineControl::SetConnectServer(const wxString& server)
{
    elbCurPoint_->SetLabel(server);
}

void OnlineControl::ClearClientsShow()
{
    std::unique_lock<std::mutex> lock(mutex_);
    onLineList_->Clear();
}

void OnlineControl::InitCall()
{
    clientCore_->ReqOnlineCallback([this](const InfoClientVec& infoClientVec) { OnFreshClientsCall(infoClientVec); });
}

void OnlineControl::OnFreshClients(wxCommandEvent& event)
{
    if (!clientCore_->IsOk()) {
        logControl_->AddLog(_("You have not established a connection with the server."));
        return;
    }
    InfoClientVec vec;
    if (!clientCore_->ReqOnline()) {
        logControl_->AddLog(_("The request to obtain the client list was failed."));
        return;
    }
    logControl_->AddLog(_("The request to obtain the client list was successful."));
}

void OnlineControl::OnFreshClientsCall(const InfoClientVec& infoClientVec)
{
    std::unique_lock<std::mutex> lock(mutex_);
    onLineList_->Clear();
    for (auto& info : infoClientVec.vec) {
        onLineList_->Append(info.id);
    }
    logControl_->AddLog(_("Get online list success."));
}