#include "PvPop.h"
#include "PvInstance.h"
#include "common/Logger.h"

static const string STYLE_LAB = "color:white;font:bold 18px;border:1px solid rgba(120,120,120,0);";

string PvPop::POP_DEFAULT = "";

//PanelPop::PanelPop(PARAM* p, int parent, string title, int w, int h)
//
//{
//	
//}

void PvPop::update(PARAM* p)
{
}

void PvPop::set_pop_cb(std::function<void(PARAM* p, int status)> cb)
{
    cb_pop_ = cb;
};

void PvPop::set_enabled(string title, bool b)
{
    if (!title.empty())
    {
        if (map_w_.find(title) != map_w_.end())
        {
            pvSetEnabled(p_, map_w_[title].pvid, b);
        }
        return;
    }
    for (auto it = map_w_.begin(); it != map_w_.end(); it++)
    {
        pvSetEnabled(p_, it->second.pvid, b);
    }
}

void PvPop::on_click_ok()
{
};

void PvPop::set_status(string status)
{
    status_ = status;
    string s = title_;
    if (!status_.empty())
    {
        s += (":" + status_);
    }
    pvSetText(p_, lab_title_, s.c_str());
}

string PvPop::status()
{
    return status_;
}

void PvPop::set_err(PARAM* p, string err)
{
    err_ = err;
    pvSetText(p, id_err_, err.c_str());
}

string PvPop::err()
{
    return err_;
}

void PvPop::reset(PARAM* p)
{
}

PvPop::PvPop(PARAM* p, int parent, string title, int w /*= 900*/, int h /*= 700*/)
    : PvWidget(p, parent, PvRect(0, 0, 1920, 1080)), width_(w), height_(h), title_(title)
{
    // 半透全屏背景，遮盖后面的控件，禁止操作底层控件
    PvManager::label(p, pvid_, "", PvRect(0, 0, 1920, 1080), "background-color:rgba(50,50,50,0);");

    // 弹框的有效工作区域
    rt_pop_.set((1920 - w) * 0.5, (1080 - h) * 0.5, w, h);
    // 工作区(包含顶部标题和底部按钮)
    w_main_ = PvManager::widget(p, pvid_, rt_pop_);
    // 工作区背景
    PvManager::label(p, w_main_, "", PvRect(0, 0, w, h), "border:1px solid #26dcac;border-radius:10;background-color:rgb(21,68,94);");

    // 顶部标题
    lab_title_ = PvManager::label(p, w_main_, title, PvRect(1, 1, w - 2, 48), "border-radius:10;background-color:rgb(49,132,149);font:bold 20px;color:rgb(255,255,255);");
    pvSetAlignment(p, lab_title_, AlignCenter);

    // 用户工作区
    w_worksace_ = PvManager::widget(p, w_main_, PvRect(1, 48, w - 2, (h - 120)));

    //  关闭按钮
    PvManager::button_image(p, w_main_, "resource/pv.btn.close.png", "", "", PvRect(w - 40, 10, 29, 29), [=](string s)
        {
            this->set_status("");
            this->hide();
        });

    static string style_btn_ok =
        "QPushButton{background-color:rgb(38,220,172);font:bold 20px;color:white;border-radius:8;}"
        "QPushButton:hover{border:2px solid red;}"
        "QPushButton:pressed{border-width:3px 0 0 3px;border-style:inset;}"
        "QPushButton:disabled{color:rgb(150,150,150);}";
    static string style_btn_cannel =
        "QPushButton{background-color:rgb(200,207,216);font:bold 20px;color:black;border-radius:8;}"
        "QPushButton:hover{border:2px solid red;}"
        "QPushButton:pressed{border-width:3px 0 0 3px;border-style:inset;}"
        "QPushButton:disabled{color:rgb(150,150,150);}";

    int btn_ok = PvManager::button(p, w_main_, "确定", PvRect(w * 0.5 - 120 - 20, h - 60, 120, 40), style_btn_ok);
    int btn_cancel = PvManager::button(p, w_main_, "取消", PvRect(w * 0.5 + 20, h - 60, 120, 40), style_btn_cannel);
    PvInstance::bind_event(p, PvEvent::BUTTON_EVENT, btn_ok, [=](string e)
        {
            this->on_click_ok();
            if (cb_pop_)
            {
                cb_pop_(p, 1);
            }
            else
            {
                this->hide();
            }
        });
    PvInstance::bind_event(p, PvEvent::BUTTON_EVENT, btn_cancel, [=](string e)
        {
            this->set_status("");
            this->hide();
        });

    // 错误提示栏
    id_err_ = PvManager::label(p, w_main_, "", PvRect(50, h - 60 - 30, 700, 30), "background-color:rgba(50,50,50,30);color:red;font:bold 16px;");

    // 默认不显示
    this->set_visible(false);

    w_param_ = make_shared<PanelParam>(p, w_worksace_, PvRect(0, 0, w, h));
    w_param_->set_line_callback([=](shared_ptr<PvLineParam> line, string old_v, string v)
        {
            string id = line->get_id();
            data_.set(id, v);
            if (cb_line_changed_)
            {
                cb_line_changed_(line);
            }
        });
}

void PvPop::set_data(DataFields& data)
{
    for (auto& it : data.fields())
    {
        this->set_data(it.first, it.second);
    }
};

void PvPop::set_data(string id, string v)
{
    data_.set(id, v);
    if (w_param_)
    {
        w_param_->set_line_data(id, v);
    }
}

string PvPop::get_data(string k)
{
    return data_.get_str(k);
}

DataFields& PvPop::get_data()
{
    return data_;
};

void PvPop::set_line_callback(function<void(shared_ptr<PvLineParam>)> cb)
{
    cb_line_changed_ = cb;
}

void PvPop::on_show()
{
}

void PvPop::on_hide()
{
    //w_param_->clear();
    this->do_cleanup();
}

void PvPop::do_cleanup()
{
};

void PvPop::foreach_line(std::function<void(string k, string v)> cb)
{
    for (auto& item : data_.fields())
    {
        if (cb)
        {
            cb(item.first, item.second);
        }
    }
}

void PvPop::clear()
{
    data_.clear();
    w_param_->clear();
}

PvPopConfirm::PvPopConfirm(PARAM* p, int parent, string title, int w, int h)
    : PvPop(p, parent, title, w, h)
{
    std::string style = "color:rgb(255,255,255);font:bold 20px;padding-left:10;";
    PvManager::label(p, w_worksace_, "是否确定操作?", PvRect(20, 30, w, 40), style);
}