// SPDX-FileCopyrightText: 2019 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "monthgraphiview.h"
#include "../widget/monthWidget/monthscheduleview.h"
#include "../dialog/scheduledlg.h"
#include "../dialog/myscheduleview.h"
#include "../widget/touchgestureoperation.h"
#include "constants.h"
#include "commondef.h"
#include "graphicsItem/cmonthschedulenumitem.h"
#include "cscenetabkeydeal.h"
#include "ckeyenabledeal.h"
#include "ckeyleftdeal.h"
#include "ckeyrightdeal.h"
#include "ckeyupdeal.h"
#include "ckeydowndeal.h"
#include "commondef.h"

#include <QShortcut>
#include <QMouseEvent>

CMonthGraphicsview::CMonthGraphicsview(QWidget *parent)
    : DragInfoGraphicsView(parent)
{
    qCDebug(ClientLogger) << "CMonthGraphicsview constructor";
    //设置显示左右下角圆角
    setShowRadius(true, true);

    m_MonthScheduleView = new CMonthScheduleView(this, m_Scene);
    connect(this, &CMonthGraphicsview::sigStateChange, m_MonthScheduleView, &CMonthScheduleView::slotStateChange, Qt::DirectConnection);
    connect(this, &CMonthGraphicsview::signalFontChange, m_MonthScheduleView, &CMonthScheduleView::slotFontChange);

    qCDebug(ClientLogger) << "Creating month day items";
    for (int i = 0; i < DDEMonthCalendar::ItemSizeOfMonthDay; ++i) {
        CMonthDayItem *item = new CMonthDayItem();
        item->setZValue(-1);
        if (m_DayItem.size() > 0) {
            //设置对应左右和下一个                            关系
            m_DayItem.last()->setNextFocusItem(item);
            m_DayItem.last()->setRightItem(item);
            item->setLeftItem(m_DayItem.last());
        }
        int upNum = i - 7;
        //如果对应的上一排编号大于零则设置对应的上下关系
        if (upNum >= 0) {
            m_DayItem.at(upNum)->setDownItem(item);
            item->setUpItem(m_DayItem.at(upNum));
        }
        //设置编号
        item->setBackgroundNum(i);
        m_DayItem.append(item);
        m_Scene->addItem(item);
    }
    updateSize();
    m_Scene->setFirstFocusItem(m_DayItem.first());
    qCDebug(ClientLogger) << "Setting up keyboard event handling";
    //添加键盘事件处理
    CKeyPressPrxy *m_keyPrxy = new CKeyPressPrxy();
    m_keyPrxy->addkeyPressDeal(new CSceneTabKeyDeal(m_Scene));
    m_keyPrxy->addkeyPressDeal(new CKeyEnableDeal(m_Scene));
    m_keyPrxy->addkeyPressDeal(new CKeyLeftDeal(m_Scene));
    m_keyPrxy->addkeyPressDeal(new CKeyRightDeal(m_Scene));
    m_keyPrxy->addkeyPressDeal(new CKeyUpDeal(m_Scene));
    m_keyPrxy->addkeyPressDeal(new CKeyDownDeal(m_Scene));
    m_Scene->setKeyPressPrxy(m_keyPrxy);
}

CMonthGraphicsview::~CMonthGraphicsview()
{
    qCDebug(ClientLogger) << "CMonthGraphicsview destructor";
    m_DayItem.clear();
}

void CMonthGraphicsview::setTheMe(int type)
{
    qCDebug(ClientLogger) << "Setting month view theme" << "type:" << type;
    m_themetype = type;
    DragInfoGraphicsView::setTheMe(type);

    for (int i = 0; i < m_DayItem.size(); ++i) {
        m_DayItem.at(i)->setTheMe(type);
    }
}

void CMonthGraphicsview::setDate(const QVector<QDate> &showDate)
{
    qCDebug(ClientLogger) << "Setting month view dates" << "first date:" << showDate.at(0);
    Q_ASSERT(showDate.size() == 42);
    if (showDate.at(0).day() != 1) {
        m_currentMonth = showDate.at(0).addMonths(1).month();
    } else {
        m_currentMonth = showDate.at(0).month();
    }
    //根据当前时间设置当前场景的第一个焦点item
    int currentIndex = 0;
    for (int i = 0; i < m_DayItem.size(); ++i) {
        m_DayItem.at(i)->setDate(showDate.at(i));
        m_DayItem.at(i)->setCurrentMonth(showDate.at(i).month() == m_currentMonth);
        if (showDate.at(i) == getCurrentDate()) {
            currentIndex = i;
        }
    }
    qCDebug(ClientLogger) << "Setting focus to current date at index" << currentIndex;
    m_Scene->setFirstFocusItem(m_DayItem.at(currentIndex));
    m_schedulelistdata.clear();
    this->scene()->update();
}

void CMonthGraphicsview::setFestival(const QMap<QDate, int> &festivalInfo)
{
    qCDebug(ClientLogger) << "Setting festival info" << "count:" << festivalInfo.size();
    m_festivallist = festivalInfo;
    for (int i = 0; i < m_DayItem.size(); ++i) {
        m_DayItem.at(i)->setStatus(static_cast<CMonthDayItem::HolidayStatus>(m_festivallist[m_DayItem.at(i)->getDate()]));
    }
    this->scene()->update();
}

void CMonthGraphicsview::setLunarInfo(const QMap<QDate, CaHuangLiDayInfo> &lunarCache)
{
    qCDebug(ClientLogger) << "Setting lunar info" << "count:" << lunarCache.size();
    m_lunarCache = lunarCache;
    updateLunar();
}

void CMonthGraphicsview::setLunarVisible(bool visible)
{
    qCDebug(ClientLogger) << "Setting lunar visibility" << "visible:" << visible;
    CMonthDayItem::m_LunarVisible = visible;
}

/**
 * @brief CMonthGraphicsview::setScheduleInfo 设置日程信息
 * @param info
 */
void CMonthGraphicsview::setScheduleInfo(const QMap<QDate, DSchedule::List> &info)
{
    qCDebug(ClientLogger) << "Setting schedule info" << "date count:" << info.size();
    m_schedulelistdata = info;
    updateInfo();
}

/**
 * @brief CMonthGraphicsview::setSelectSearchSchedule     设置选择搜索日程
 * @param scheduleInfo
 */
void CMonthGraphicsview::setSelectSearchSchedule(const DSchedule::Ptr &scheduleInfo)
{
    qCDebug(ClientLogger) << "Setting selected search schedule" 
                         << "summary:" << scheduleInfo->summary() 
                         << "start:" << scheduleInfo->dtStart();
    DragInfoGraphicsView::setSelectSearchSchedule(scheduleInfo);
    //获取所有的日程item
    QVector<QGraphicsRectItem *> mScheduleShowBtn = m_MonthScheduleView->getScheduleShowItem();

    int animatedCount = 0;
    for (int i = 0; i < mScheduleShowBtn.size(); ++i) {
        CMonthScheduleItem *item = dynamic_cast<CMonthScheduleItem *>(mScheduleShowBtn.at(i));

        if (item == nullptr) continue;

        if (scheduleInfo == item->getData()) {
            qCDebug(ClientLogger) << "Found matching schedule item at index" << i;
            item->setStartValue(0);
            item->setEndValue(4);
            item->startAnimation();
            animatedCount++;
        }
    }
    qCDebug(ClientLogger) << "Started animations for" << animatedCount << "matching schedule items";
}

/**
 * @brief CMonthGraphicsview::setSearchScheduleInfo       设置搜索日程信息
 * @param searchScheduleInfo
 */
void CMonthGraphicsview::setSearchScheduleInfo(const DSchedule::List &searchScheduleInfo)
{
    qCDebug(ClientLogger) << "Setting search schedule info" << "count:" << searchScheduleInfo.size();
    DragInfoItem::setSearchScheduleInfo(searchScheduleInfo);
    this->scene()->update();
}

void CMonthGraphicsview::updateSize()
{
    qCDebug(ClientLogger) << "Updating month view size" 
                         << "width:" << viewport()->rect().width() 
                         << "height:" << viewport()->rect().height();
    //场景的大小和位置
    QRectF sceneRect(0, 0, viewport()->rect().width(), viewport()->rect().height());
    m_Scene->setSceneRect(sceneRect);
    qreal w = m_Scene->width() / DDEMonthCalendar::AFewDaysOfWeek;
    qreal h = m_Scene->height() / DDEMonthCalendar::LinesNumOfMonth;
    QRectF rect;
    int w_offset = 0;
    int h_offset = 0;

    for (int i = 0; i < m_DayItem.size(); ++i) {
        h_offset = i / DDEMonthCalendar::AFewDaysOfWeek;
        w_offset = i % DDEMonthCalendar::AFewDaysOfWeek;
        rect.setRect(w * w_offset,
                     h * h_offset,
                     w,
                     h);
        m_DayItem.at(i)->setRect(rect);
    }
}

void CMonthGraphicsview::updateLunar()
{
    qCDebug(ClientLogger) << "Updating lunar info";
    QDate date;
    CaHuangLiDayInfo info;
    QString lunarStr("");

    for (int i = 0; i < m_DayItem.size(); ++i) {
        date = m_DayItem.at(i)->getDate();
        if (m_lunarCache.contains(date)) {
            info = m_lunarCache.value(date);

            if (info.mLunarDayName == "初一") {
                //如果为闰月只显示月份不显示天
                if (info.mLunarMonthName.contains("闰")) {
                    info.mLunarDayName = info.mLunarMonthName;
                } else {
                    info.mLunarDayName = info.mLunarMonthName + info.mLunarDayName;
                }
            }

            if (info.mTerm.isEmpty()) {
                lunarStr = info.mLunarDayName;
            } else {
                lunarStr = info.mTerm;
            }
        } else {
            lunarStr = "";
        }
        m_DayItem.at(i)->setLunar(lunarStr);
    }
}

/**
 * @brief CMonthGraphicsview::updateInfo      更新日程数据显示
 */
void CMonthGraphicsview::updateInfo()
{
    qCDebug(ClientLogger) << "Updating month view info";
    DragInfoGraphicsView::updateInfo();
    int h = m_MonthScheduleView->getScheduleHeight();
    m_MonthScheduleView->setallsize(this->viewport()->width(),
                                    this->viewport()->height(),
                                    0, 0, 0, h);
    m_MonthScheduleView->setData(m_schedulelistdata, 1);

    switch (m_DragStatus) {
    case IsCreate:
        qCDebug(ClientLogger) << "Updating info for new schedule creation";
        upDateInfoShow(IsCreate, m_DragScheduleInfo);
        break;
    case ChangeWhole:
        qCDebug(ClientLogger) << "Updating info for whole schedule change";
        upDateInfoShow(ChangeWhole, m_DragScheduleInfo);
        break;
    default:
        qCDebug(ClientLogger) << "No drag operation in progress";
        break;
    }
    viewport()->update();
    update();
    //更新背景上显示的item
    updateBackgroundShowItem();
}

QPointF CMonthGraphicsview::getItemPos(const QPoint &p, const QRectF &itemRect)
{
    QPointF scenePos = this->mapToScene(p);
    QPointF result = QPointF(scenePos.x() - itemRect.x(), scenePos.y() - itemRect.y());
    // qCDebug(ClientLogger) << "CMonthGraphicsview::getItemPos - point:" << p << "item rect:" << itemRect << "result:" << result;
    return result;
}

CMonthGraphicsview::PosInItem CMonthGraphicsview::getPosInItem(const QPoint &p, const QRectF &itemRect)
{
    QPointF scenePos = this->mapToScene(p);
    QPointF itemPos = QPointF(scenePos.x() - itemRect.x(), scenePos.y() - itemRect.y());
    qreal bottomY = itemRect.width() - itemPos.x();
    PosInItem result;

    if (itemPos.x() < 5) {
        result = LEFT;
    } else if (bottomY < 5) {
        result = RIGHT;
    } else {
        result = MIDDLE;
    }

    // qCDebug(ClientLogger) << "CMonthGraphicsview::getPosInItem - point:" << p 
    //                      << "item rect:" << itemRect 
    //                      << "item pos:" << itemPos
    //                      << "result:" << result;
    return result;
}

QDateTime CMonthGraphicsview::getPosDate(const QPoint &p)
{
    if (!this->sceneRect().contains(p)) {
        qCDebug(ClientLogger) << "Point outside scene rect, returning move date:" << m_MoveDate;
        return m_MoveDate;
    }
    QRectF rect = this->sceneRect();
    qreal x = 0;
    qreal y = 0;

    if (p.x() < 0) {
        x = 0;
    } else if (p.x() > (rect.width() - 10)) {
        x = rect.width() - 10;
    } else {
        x = p.x();
    }

    if (p.y() < 0) {
        y = 0;
    } else if (p.y() > (rect.height() - 10)) {
        y = rect.height() - 10;
    } else {
        y = p.y();
    }

    int xoffset = qFloor(x / (rect.width() / DDEMonthCalendar::AFewDaysOfWeek)) % DDEMonthCalendar::AFewDaysOfWeek;
    int yoffset = qFloor(y / (rect.height() / DDEMonthCalendar::LinesNumOfMonth)) % DDEMonthCalendar::LinesNumOfMonth;

    QDateTime result = QDateTime(m_DayItem[xoffset + yoffset * 7]->getDate(), QTime(0, 0, 0));
    qCDebug(ClientLogger) << "CMonthGraphicsview::getPosDate - point:" << p 
                          << "x:" << x << "y:" << y 
                          << "offsets:" << xoffset << yoffset
                          << "result:" << result;
    return result;
}

void CMonthGraphicsview::upDateInfoShow(const CMonthGraphicsview::DragStatus &status, const DSchedule::Ptr &info)
{
    qCDebug(ClientLogger) << "CMonthGraphicsview::upDateInfoShow - status:" << status
                         << "schedule:" << (info ? info->summary() : "null");
    switch (status) {
    case NONE:
        Q_UNUSED(info);
        qCDebug(ClientLogger) << "Status: NONE, no action needed";
        break;
    case ChangeBegin:
    case ChangeEnd: {
        qCDebug(ClientLogger) << "Changing date in month schedule view";
        m_MonthScheduleView->changeDate(info);
    }
    break;
    case ChangeWhole: {
        qCDebug(ClientLogger) << "Handling whole schedule change";
    }
    break;
    case IsCreate:
        qCDebug(ClientLogger) << "Updating date for new schedule";
        m_MonthScheduleView->updateDate(info);
        break;
    }
}

void CMonthGraphicsview::slideEvent(QPointF &startPoint, QPointF &stopPort)
{
    qCDebug(ClientLogger) << "CMonthGraphicsview::slideEvent - start:" << startPoint << "stop:" << stopPort;
    qreal _movingLine {0};
    touchGestureOperation::TouchMovingDirection _touchMovingDir =
        touchGestureOperation::getTouchMovingDir(startPoint, stopPort, _movingLine);
    //切换月份 0 不切换  1 下一个  -1 上个月
    int delta {0};
    //移动偏移 25则切换月份
    const int moveOffset = 25;
    switch (_touchMovingDir) {
    case touchGestureOperation::T_TOP: {
        if (_movingLine > moveOffset) {
            qCDebug(ClientLogger) << "Slide up detected, moving to previous month";
            delta = -1;
            startPoint = stopPort;
        }
        break;
    }
    case touchGestureOperation::T_BOTTOM: {
        if (_movingLine > moveOffset) {
            qCDebug(ClientLogger) << "Slide down detected, moving to next month";
            delta = 1;
            startPoint = stopPort;
        }
        break;
    }
    default:
        break;
    }
    if (delta != 0) {
        qCDebug(ClientLogger) << "Emitting angle delta signal:" << delta;
        emit signalAngleDelta(delta);
    }
}

void CMonthGraphicsview::mouseDoubleClickEvent(QMouseEvent *event)
{
    qCDebug(ClientLogger) << "CMonthGraphicsview::mouseDoubleClickEvent - pos:" << event->pos() << "button:" << event->button();
    if (event->button() != Qt::LeftButton) {
        qCDebug(ClientLogger) << "Mouse double click event, but not left button";
        return;
    }

    QGraphicsItem *listItem = itemAt(event->pos());
    CMonthScheduleNumItem *item = dynamic_cast<CMonthScheduleNumItem *>(listItem);

    if (item != nullptr) {
        // qCDebug(ClientLogger) << "Double click on schedule num item, switching view to date:" << item->getDate();
        //双击切换视图
        if (item->getDate().year() > DDECalendar::QueryEarliestYear) {
            qCDebug(ClientLogger) << "Double click on schedule num item, switching view to date:" << item->getDate();
            emit signalsViewSelectDate(item->getDate());
        }
        return;
    }

    CMonthScheduleItem *infoitem = dynamic_cast<CMonthScheduleItem *>(listItem);

    if (infoitem != nullptr) {
        qCDebug(ClientLogger) << "Double click on schedule item, opening edit dialog";
        CMyScheduleView dlg(infoitem->getData(), this);
        connect(&dlg, &CMyScheduleView::signalsEditorDelete, this, &CMonthGraphicsview::signalsUpdateSchedule);
        if (dlg.exec() == DDialog::Accepted) {
            qCDebug(ClientLogger) << "Schedule edited successfully";
            emit sigStateChange(true);
        }
        return;
    }

    CMonthDayItem *Dayitem = dynamic_cast<CMonthDayItem *>(listItem);

    if (Dayitem != nullptr) {
        // qCDebug(ClientLogger) << "Double click on day item, getting item pos";
        QPointF point = getItemPos(event->pos(), Dayitem->rect());
        if (point.y() < 38) {
            //双击切换视图
            if (Dayitem->getDate().year() > DDECalendar::QueryEarliestYear) {
                qCDebug(ClientLogger) << "Double click on day item header, switching view to date:" << Dayitem->getDate();
                emit signalsViewSelectDate(Dayitem->getDate());
            }
        } else {
            //双击新建日程
            if (Dayitem->getDate().year() >= DDECalendar::QueryEarliestYear) {
                if (Dayitem->getDate().year() <= DDECalendar::QueryLatestYear) {
                    qCDebug(ClientLogger) << "Double click on day item body, creating new schedule for date:" << Dayitem->getDate();
                    slotCreate(QDateTime(Dayitem->getDate(), QTime(0, 0, 0)));
                }
            }
        }
    }
}

void CMonthGraphicsview::resizeEvent(QResizeEvent *event)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::resizeEvent - size:" << event->size();
    DragInfoGraphicsView::resizeEvent(event);
    updateSize();
    updateInfo();
}
void CMonthGraphicsview::changeEvent(QEvent *event)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::changeEvent - type:" << event->type();
    if (event->type() == QEvent::FontChange) {
        // qCDebug(ClientLogger) << "Font changed, emitting signalFontChange";
        emit signalFontChange();
    }
}

void CMonthGraphicsview::wheelEvent(QWheelEvent *e)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::wheelEvent - angle delta:" << e->angleDelta();
    //如果滚动为上下则发送信号
    if (e->angleDelta().y() != 0) {
        emit signalAngleDelta(e->angleDelta().y());
    }
}

/**
 * @brief CMonthGraphicsview::updateBackgroundShowItem    更新背景上显示的item
 */
void CMonthGraphicsview::updateBackgroundShowItem()
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::updateBackgroundShowItem";
    for (int i = 0; i < m_DayItem.size(); ++i) {
        m_DayItem.at(i)->updateShowItem();
    }
}

void CMonthGraphicsview::setSceneCurrentItemFocus(const QDate &focusDate)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::setSceneCurrentItemFocus - focus date:" << focusDate;
    int offset = static_cast<int>(m_DayItem.first()->getDate().daysTo(focusDate));
    if (offset >= 0 && offset < m_DayItem.size()) {
        qCDebug(ClientLogger) << "Setting scene current item focus" 
                             << "focus date:" << focusDate 
                             << "offset:" << offset;
        m_Scene->setCurrentFocusItem(m_DayItem.at(offset));
        m_Scene->currentFocusItemUpdate();
    } else {
        qCWarning(ClientLogger) << "Invalid focus date offset" 
                               << "focus date:" << focusDate 
                               << "first item date:" << m_DayItem.first()->getDate() 
                               << "offset:" << offset;
    }
}

void CMonthGraphicsview::setDragPixmap(QDrag *drag, DragInfoItem *item)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::setDragPixmap - item:" << item;
    CMonthScheduleItem *infoitem = dynamic_cast<CMonthScheduleItem *>(item);
    drag->setPixmap(infoitem->getPixmap());
}

bool CMonthGraphicsview::MeetCreationConditions(const QDateTime &date)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::MeetCreationConditions - date:" << date;
    return qAbs(date.daysTo(m_PressDate)) < 43;
}

bool CMonthGraphicsview::IsEqualtime(const QDateTime &timeFirst, const QDateTime &timeSecond)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::IsEqualtime - timeFirst:" << timeFirst << "timeSecond:" << timeSecond;
    return timeFirst.date() == timeSecond.date();
}

bool CMonthGraphicsview::JudgeIsCreate(const QPointF &pos)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::JudgeIsCreate - pos:" << pos;
    return qAbs(pos.x() - m_PressPos.x()) > 20 || qAbs(m_PressDate.daysTo(getPosDate(pos.toPoint()))) > 0;
}

void CMonthGraphicsview::RightClickToCreate(QGraphicsItem *listItem, const QPoint &pos)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::RightClickToCreate - listItem:" << listItem << "pos:" << pos;
    Q_UNUSED(pos);
    CMonthDayItem *Dayitem = dynamic_cast<CMonthDayItem *>(listItem);

    if (Dayitem != nullptr) {
        // qCDebug(ClientLogger) << "Right click on day item, showing create action";
        m_rightMenu->clear();
        m_rightMenu->addAction(m_createAction);
        m_createDate.setDate(Dayitem->getDate());
        m_rightMenu->exec(QCursor::pos());
    }
}

void CMonthGraphicsview::MoveInfoProcess(DSchedule::Ptr &info, const QPointF &pos)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::MoveInfoProcess - info:" << info << "pos:" << pos;
    qint64 offset       = m_PressDate.daysTo(m_MoveDate);
    info->setDtStart(info->dtStart().addDays(offset));
    info->setDtEnd(info->dtEnd().addDays(offset));
    qreal y = 0;
    QRectF rect = this->sceneRect();

    if (pos.y() < 0) {
        y = 0;
    } else if (pos.y() > rect.height()) {
        y = rect.height();
    } else {
        y = pos.y();
    }

    int yoffset = qFloor(y / (rect.height() / DDEMonthCalendar::LinesNumOfMonth)) % DDEMonthCalendar::LinesNumOfMonth;
    m_MonthScheduleView->updateDate(yoffset, info);
}

/**
 * @brief CMonthGraphicsview::getDragScheduleInfoBeginTime        获取移动开始时间
 * @param moveDateTime
 * @return
 */
QDateTime CMonthGraphicsview::getDragScheduleInfoBeginTime(const QDateTime &moveDateTime)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::getDragScheduleInfoBeginTime - moveDateTime:" << moveDateTime;
    //获取移动开始时间
    QDateTime _beginTime = moveDateTime.daysTo(m_InfoEndTime) < 0 ?
                           QDateTime(m_InfoEndTime.date(), m_InfoBeginTime.time()) :
                           QDateTime(moveDateTime.date(), m_InfoBeginTime.time());
    //如果开始时间晚与结束时间则减少一天
    _beginTime = _beginTime > m_InfoEndTime ? _beginTime.addDays(-1) : _beginTime;
    return _beginTime;
}

/**
 * @brief CMonthGraphicsview::getDragScheduleInfoEndTime      获取结束时间
 * @param moveDateTime
 * @return
 */
QDateTime CMonthGraphicsview::getDragScheduleInfoEndTime(const QDateTime &moveDateTime)
{
    // qCDebug(ClientLogger) << "CMonthGraphicsview::getDragScheduleInfoEndTime - moveDateTime:" << moveDateTime;
    //获取结束时间
    QDateTime _endTime = m_InfoBeginTime.daysTo(moveDateTime) < 0 ?
                         QDateTime(m_InfoBeginTime.date(), m_InfoEndTime.time()) :
                         QDateTime(moveDateTime.date(), m_InfoEndTime.time());
    //如果结束时间小于开始时间则添加一天
    _endTime = _endTime < m_InfoBeginTime ? _endTime.addDays(1)  : _endTime;
    return _endTime;
}

void CMonthGraphicsview::slotCreate(const QDateTime &date)
{
    qCDebug(ClientLogger) << "Creating new schedule" << "date:" << date;
    CScheduleDlg dlg(1, this);
    QDateTime tDatatime;
    tDatatime.setDate(date.date());

    if (date.date() == QDate::currentDate()) {
        tDatatime.setTime(QTime::currentTime());
    } else {
        tDatatime.setTime(QTime(8, 0));
    }

    dlg.setDate(tDatatime);
    dlg.setAllDay(true);

    if (dlg.exec() == DDialog::Accepted) {
        qCDebug(ClientLogger) << "New schedule created successfully";
        emit signalsUpdateSchedule();
        emit signalsScheduleUpdate(0);
        emit sigStateChange(true);
    }
}
