#include "QVideoPanelLabel.h"
#include <QtGui/QCursor>
#include "multi_ipnc_demo.h"

QVideoPanelLabel::QVideoPanelLabel(QWidget *parent,const NsstChannel& nsst_channel)
    : QLabel(parent),
      delegate_(NULL),
      nsst_channel_(nsst_channel),
      can_draw_grid_(false)
{
}

QVideoPanelLabel::~QVideoPanelLabel()
{

}

void QVideoPanelLabel::mousePressEvent( QMouseEvent *ev )
{
    if (delegate_)
    {
        delegate_->OnVideoPanelMousePress(this);
    }

    if (ev->button() == Qt::RightButton)
    {
        draw_rect_params_.draw_rect_start_pos = ev->pos();
        draw_rect_params_.draw_rect_now = true;
    }

    if (ev->button() == Qt::LeftButton)
    {
        QPoint pos = ev->pos();
        int index = GetActiveText(pos);
        if (index >= 0)
        {
            DrawTextInfo* info = &text_info_[index];
            if (info->draw)
            {
                info->begin_pos = pos;
                text_active_index_ = index;
            }
        }

        if (can_draw_grid_)
        {
            StartDrawAlarmGrid(ev);
        }
    }
}

void QVideoPanelLabel::mouseDoubleClickEvent( QMouseEvent * )
{
    if (delegate_)
    {
        delegate_->OnVideoPanelDblClick(this);
    }
}

void QVideoPanelLabel::mouseMoveEvent( QMouseEvent *ev )
{
    if (ev->buttons() == Qt::LeftButton)
    {
        QPoint cur_pos = ev->pos();
        RECT base_rect;
        base_rect.left = this->geometry().left();
        base_rect.top = this->geometry().top();
        base_rect.right = this->geometry().right();
        base_rect.bottom = this->geometry().bottom();

        if (draw_rect_params_.draw_rect_now || can_draw_grid_)
        {
            RECT rc;
            if (draw_rect_params_.draw_rect_now)
            {
                rc.left = draw_rect_params_.draw_rect_start_pos.x();
                rc.top = draw_rect_params_.draw_rect_start_pos.y();
            }
            else
            {
                rc.left = draw_grid_start_pos_.x();
                rc.top = draw_grid_start_pos_.y();
            }
            
            rc.right = cur_pos.x();
            rc.bottom = cur_pos.y();
            if (rc.right > this->geometry().right())
                rc.right = this->geometry().right();
            if (rc.bottom > this->geometry().bottom())
                rc.bottom = this->geometry().bottom();
            if (rc.right < 0 )
                rc.right = 0;
            if (rc.bottom < 0)
                rc.bottom = 0;

            RECT scale_rect;
            NSSTCommonCalcRectScale(&base_rect,&rc,&scale_rect);

            if ( multi_ipnc_demo::instance()->device_id() >= 0 )
            {
                if (draw_rect_params_.draw_rect_now)
                {
                    NSSTDrawRect(&nsst_channel_,&scale_rect,0xffff0000);
                }
                else
                {
                    NSSTSetDrawAlarmArea(&nsst_channel_,&scale_rect,true);
                }
            }
        }

        if (text_active_index_ >= 0)
        {
            DrawTextInfo* info = &text_info_[text_active_index_];
            if (info->draw)
            {
                QPoint old_pos(info->sub_rc.left,info->sub_rc.top);
                QPoint delta = cur_pos - info->begin_pos;
                info->begin_pos = cur_pos;
                QPoint to = old_pos + delta ;
                if (to.x() <= base_rect.right - 10
                    && to.x() >= base_rect.left
                    && to.y() >= base_rect.top
                    && to.y() <= base_rect.bottom - 10)
                {
                    SetRect(&info->sub_rc,to.x(),to.y(),info->sub_rc.right + delta.x(),info->sub_rc.bottom + delta.y());
                    NSSTCommonCalcRectScale(&base_rect,&info->sub_rc,&info->scale_rc);
                    POINT pos;
                    pos.x = info->scale_rc.left;
                    pos.y = info->scale_rc.top;
                    NSSTDrawText(&nsst_channel_,
                        text_active_index_,
                        info->text.toUtf8().data(),
                        pos,
                        0xf0ff0000);
                }
            }
        }
    }
}

void QVideoPanelLabel::mouseReleaseEvent( QMouseEvent *ev )
{
    NSSTClearDrawRect(&nsst_channel_);
    draw_rect_params_.draw_rect_now = false;

    text_active_index_ = -1;
}

void QVideoPanelLabel::ResetDeviceId( DEVICE_ID id )
{
    nsst_channel_.dev_id = id;
}

void QVideoPanelLabel::StartDrawText( int index,POINT* start,const QString& text )
{
    text_info_[index].draw = true;
    if (start->x != -1 && start->y != -1)
    {
        SetRect(&text_info_[index].scale_rc,start->x,start->y,0,0);
    }
    text_info_[index].text = text;

    NSSTDrawText(&nsst_channel_,index,text.toUtf8().data(),*start,0xf0ff0000);
}

void QVideoPanelLabel::StopDrawText( int index )
{
    text_active_index_ = -1;
    if (index >= TEXT_COUNT)
    {
        return;
    }
    text_info_[index].draw = false;

    POINT p;
    memset(&p,0,sizeof(POINT));
    NSSTDrawText(&nsst_channel_,index,"",p,0xf0ff0000);
}

int QVideoPanelLabel::GetActiveText( const QPoint& pos )
{
    for (unsigned i = 0;i<TEXT_COUNT;++i)
    {
        if (text_info_[i].draw)
        {
            NSSTGetTextRealRect(&nsst_channel_,i,&text_info_[i].scale_rc);
            RECT base_rect;
            base_rect.left = this->geometry().left();
            base_rect.top = this->geometry().top();
            base_rect.right = this->geometry().right();
            base_rect.bottom = this->geometry().bottom();
            NSSTCommonCalcRectBase(&text_info_[i].scale_rc,&base_rect,&text_info_[i].sub_rc);
            RECT& sr = text_info_[i].sub_rc;

            QRect r(QPoint(sr.left,sr.top),QPoint(sr.right,sr.bottom));
            if (r.contains(pos))
            {
                return i;
            }
        }
    }
    return -1;
}

bool QVideoPanelLabel::GetTextPosition( int index,RECT* rc )
{
    if (text_info_[index].draw)
    {
        NSSTGetTextRealRect(&nsst_channel_,index,&text_info_[index].scale_rc);
        CopyRect(rc,&text_info_[index].scale_rc);
        return true;
    }
    else
    {
        return false; 
    }
}

void QVideoPanelLabel::ShowAlarmGrid()
{
    can_draw_grid_ = true;
    NSSTShowDrawAlarmArea(&nsst_channel_,true);
}

void QVideoPanelLabel::StopDrawAlarmGrid()
{
    if (can_draw_grid_)
    {
        can_draw_grid_ = false;
        NSSTClearDrawAlarmArea(&nsst_channel_);
        NSSTShowDrawAlarmArea(&nsst_channel_,false);
    }
}

void QVideoPanelLabel::StartDrawAlarmGrid( QMouseEvent* ev )
{
    can_draw_grid_ = true;
    draw_grid_start_pos_ = ev->pos();
    NSSTShowDrawAlarmArea(&nsst_channel_,true);
}

void QVideoPanelLabel::SetAllAlarmGrid( int* area )
{
    ShowAlarmGrid();
    NSSTSetAllDrawAlarmArea(&nsst_channel_,area);
}

void QVideoPanelLabel::ClearDrawAlarmGrid()
{
    NSSTClearDrawAlarmArea(&nsst_channel_);
}
