#include "cmwidget.h"
#include <QShowEvent>
#include "gui.h"
#include <QLayout>
#include <QPainter>
CMWidget::CMWidget(QWidget *parent) : QWidget(parent)
{
    setFont(QFont(M_FONT, M_FONT_SIZE));
    setAttribute(Qt::WA_StyledBackground,true);
    m_sName="";
    m_bHide=false;
    m_bReadOnly=false;

    m_iTimerID=0;
    m_iTimerInterval=100;
}

CMWidget::~CMWidget()
{
}

void CMWidget::MInit()
{
    for(WIDGETMAP::iterator ii=mchildwidgets.begin();
        ii!=mchildwidgets.end();ii++)
    {
        (*ii)->MInit();
    }
}

void CMWidget::MExit()
{
	for(WIDGETMAP::iterator ii=mchildwidgets.begin();
		ii!=mchildwidgets.end();ii++)
	{
		(*ii)->MExit();
	}
    if(m_iTimerID!=0)
    {
        killTimer(m_iTimerID);
    }
}

void CMWidget::MTimeFresh()
{
    for(WIDGETMAP::iterator ii=mchildwidgets.begin();
        ii!=mchildwidgets.end();ii++)
    {
        if((*ii)->isMWidgetHidden()) continue;
        (*ii)->MTimeFresh();
    }
}

void CMWidget::MFresh()
{
    if(isMWidgetHidden())return;

    if (!HasPermission()) {
        QString str = tr("Need %1 permission").arg(GetPermissionName().c_str());
        showShieldWindow(true,str);
        return;
    }
    showShieldWindow(false,QString());
    for(WIDGETMAP::iterator ii=mchildwidgets.begin();
        ii!=mchildwidgets.end();ii++)
	{
		if((*ii)->isMWidgetHidden()) continue;
        (*ii)->MFresh();
    }

}

void CMWidget::MApply()
{
    for(WIDGETMAP::iterator ii=mchildwidgets.begin();
        ii!=mchildwidgets.end();ii++)
	{
		if((*ii)->isMWidgetHidden()||(*ii)->m_bHide) continue;
        (*ii)->MApply();
    }
}


void CMWidget::showEvent(QShowEvent* )
{
    MFresh();
    if(m_iTimerInterval)
    {
        if(m_iTimerID==0)
        {
            m_iTimerID=startTimer(m_iTimerInterval);
        }
    }
}

void CMWidget::hideEvent(QHideEvent*  )
{
    if(m_iTimerID!=0)
    {
        killTimer(m_iTimerID);
        m_iTimerID=0;
    }
}

void CMWidget::paintEvent(QPaintEvent *pEvent)
{
	Q_UNUSED(pEvent);
	QPainter painter(this);
	if(m_bHide)
	{
        painter.setPen(Qt::red);
		painter.drawText(rect(),
						 Qt::AlignVCenter|Qt::AlignHCenter,
                         m_strHide);
    }
}

void CMWidget::timerEvent(QTimerEvent * pEvent)
{
    if(pEvent->timerId()==m_iTimerID)
    {
        MTimeFresh();
    }
}

bool CMWidget::isMWidgetHidden()
{
	return !isVisible();
}

void CMWidget::SetPermission(std::string sPermissionName, NoPermissionBehaviour noPermissionBehaviour/* = kReadOnly*/)
{
    SetPermission(this, PermissionInfo(sPermissionName, noPermissionBehaviour));
}

void CMWidget::SetControlPermission(QWidget* object, const PermissionInfo& permissionInfo)
{
    if (m_mapControlPermission.find(object) != m_mapControlPermission.end()) {
        m_mapControlPermission.emplace(std::make_pair(object, permissionInfo));
    } else {
        (m_mapControlPermission.find(object)->second) = permissionInfo;
    }
}

void CMWidget::InitPermissions()
{
    //RegisterObjectPermission(this, PermissionInfo(sPermissionName, noPermissionBehaviour));
}

void CMWidget::UpdatePermissionUI(bool bParentHasPermission /*= true*/)
{
    if (!bParentHasPermission) {
        hide();
    } else {
        
    }

    for (auto iter = m_mapObjectsPermission.begin(); iter != m_mapObjectsPermission.end(); ++iter) {
        QWidget* widget = iter->first;
        PermissionInfo& permissions = iter->second;
        if (HasPermission()) {
            //
        }
    }

    for (WIDGETMAP::iterator ii = mchildwidgets.begin();
         ii != mchildwidgets.end(); ii++) {
        if ((*ii)->isMWidgetHidden() || (*ii)->m_bHide) continue;
        (*ii)->UpdatePermissionUI();
    }
}

void CMWidget::SetPermission(QWidget* object,const PermissionInfo& permissionInfo)
{
   if (m_mapObjectsPermission.find(object) != m_mapObjectsPermission.end()) {
        m_mapObjectsPermission.emplace(std::make_pair(object, permissionInfo));
    } else {
       (m_mapObjectsPermission.find(object)->second) = permissionInfo;
    }
}

static void hide_layout_child(QLayout* pLayout,bool bhide)
{
    if(!pLayout)return ;
    int isize= pLayout->count();
    for(int i=0;i<isize;i++)
    {
        QLayoutItem* pitem=pLayout->itemAt(i);
        if(pitem->widget())
        {
            if(bhide)
            {
               pitem->widget()->hide();//setHidden(true);
            }
            else
            {
                pitem->widget()->show();//setHidden(false);
            }
            hide_layout_child(pitem->widget()->layout(),  bhide);
        }
        else if(pitem->layout())
        {
            hide_layout_child(pitem->layout(),  bhide);
        }
    }

}
void CMWidget::showShieldWindow(bool bshow, QString &str)
{
	m_bHide=bshow;
	hide_layout_child(layout(),bshow);
	m_strHide=str;
    UpdatePermissionUI();

    update();
}

void CMWidget::SetReadOnly(bool bReadOnly)
{
    m_bReadOnly=bReadOnly;
    for(WIDGETMAP::iterator ii=mchildwidgets.begin();
        ii!=mchildwidgets.end();ii++)
    {
        (*ii)->SetReadOnly(bReadOnly);
    }
}


