//=============================================================================
//  MuseScore
//  Music Composition & Notation
//  $Id:$
//
//  Copyright (C) 2002-2011 Werner Schweer
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENCE.GPL
//=============================================================================

#include "libmscore/score.h"
#include "libmscore/element.h"
#include "libmscore/note.h"
#include "libmscore/rest.h"
#include "libmscore/measure.h"
#include "libmscore/system.h"
#include "libmscore/segment.h"
#include "libmscore/page.h"
#include "libmscore/image.h"
#include "libmscore/text.h"
#include "libmscore/spanner.h"
#include "libmscore/chord.h"
#include "libmscore/icon.h"
#include "libmscore/xml.h"
#include "musescore.h"
#include "scoreview.h"
#include "continuouspanel.h"

namespace Ms {

//---------------------------------------------------------
//   setDropTarget
//---------------------------------------------------------

void ScoreView::setDropTarget(const Element* el)
      {
      if (dropTarget != el) {
            if (dropTarget) {
                  dropTarget->setDropTarget(false);
//                  _score->addRefresh(dropTarget->canvasBoundingRect());
                  dropTarget = 0;
                  }
            dropTarget = el;
            if (dropTarget) {
                  dropTarget->setDropTarget(true);
//                  _score->addRefresh(dropTarget->canvasBoundingRect());
                  }
            }
      if (!dropAnchor.isNull()) {
            QRectF r;
            r.setTopLeft(dropAnchor.p1());
            r.setBottomRight(dropAnchor.p2());
//            _score->addRefresh(r.normalized());
            dropAnchor = QLineF();
            }
      if (dropRectangle.isValid()) {
//            _score->addRefresh(dropRectangle);
            dropRectangle = QRectF();
            }
      update();
      }

//---------------------------------------------------------
//   setDropRectangle
//---------------------------------------------------------

void ScoreView::setDropRectangle(const QRectF& r)
      {
      if (dropRectangle.isValid())
            _score->addRefresh(dropRectangle);
      dropRectangle = r;
      if (dropTarget) {
            dropTarget->setDropTarget(false);
            _score->addRefresh(dropTarget->canvasBoundingRect());
            dropTarget = 0;
            }
      else if (!dropAnchor.isNull()) {
            QRectF r;
            r.setTopLeft(dropAnchor.p1());
            r.setBottomRight(dropAnchor.p2());
            _score->addRefresh(r.normalized());
            dropAnchor = QLineF();
            }
//      _score->addRefresh(r);
      update();
      }

//---------------------------------------------------------
//   setDropAnchor
//---------------------------------------------------------

void ScoreView::setDropAnchor(const QLineF& l)
      {
      if (!dropAnchor.isNull()) {
            qreal w = 2 / _matrix.m11();
            QRectF r;
            r.setTopLeft(dropAnchor.p1());
            r.setBottomRight(dropAnchor.p2());
            r = r.normalized();
            r.adjust(-w, -w, 2*w, 2*w);
//            _score->addRefresh(r);
            }
      if (dropRectangle.isValid()) {
//            _score->addRefresh(dropRectangle);
            dropRectangle = QRectF();
            }
      dropAnchor = l;
      if (!dropAnchor.isNull()) {
            qreal w = 2 / _matrix.m11();
            QRectF r;
            r.setTopLeft(dropAnchor.p1());
            r.setBottomRight(dropAnchor.p2());
            r = r.normalized();
            r.adjust(-w, -w, 2*w, 2*w);
//            _score->addRefresh(r);
            }
      update();
      }

//---------------------------------------------------------
//   setViewRect
//---------------------------------------------------------

void ScoreView::setViewRect(const QRectF& r)
      {
      QRectF rr = _matrix.mapRect(r);
      QPoint d = rr.topLeft().toPoint();
      int dx   = -d.x();
      int dy   = -d.y();
      QApplication::sendPostedEvents(this, 0);
      _matrix.setMatrix(_matrix.m11(), _matrix.m12(), _matrix.m13(), _matrix.m21(),
         _matrix.m22(), _matrix.m23(), _matrix.dx()+dx, _matrix.dy()+dy, _matrix.m33());
      imatrix = _matrix.inverted();
      scroll(dx, dy, QRect(0, 0, width(), height()));
      emit offsetChanged(_matrix.dx(), _matrix.dy());
      if (_continuousPanel->visible())
            update();
      }

//---------------------------------------------------------
//   dragTimeAnchorElement
//    pos is in canvas coordinates
//    return true if there is a valid target
//---------------------------------------------------------

bool ScoreView::dragTimeAnchorElement(const QPointF& pos)
      {
      int staffIdx;
      Segment* seg;
      MeasureBase* mb = _score->pos2measure(pos, &staffIdx, 0, &seg, 0);
      int track  = staffIdx * VOICES;

      if (mb && mb->isMeasure() && seg->element(track)) {
            Measure* m = toMeasure(mb);
            System* s  = m->system();
            qreal y    = s->staff(staffIdx)->y() + s->pos().y() + s->page()->pos().y();
            QPointF anchor(seg->canvasBoundingRect().x(), y);
            setDropAnchor(QLineF(pos, anchor));
            dragElement->score()->addRefresh(dragElement->canvasBoundingRect());
            dragElement->setTrack(track);
            dragElement->score()->addRefresh(dragElement->canvasBoundingRect());
            return true;
            }
      dragElement->score()->addRefresh(dragElement->canvasBoundingRect());
      setDropTarget(0);
      return false;
      }

//---------------------------------------------------------
//   dragMeasureAnchorElement
//---------------------------------------------------------

bool ScoreView::dragMeasureAnchorElement(const QPointF& pos)
      {
      Measure* m = _score->searchMeasure(pos);
      if (m) {
            QRectF b(m->canvasBoundingRect());

            QPointF anchor;
            if (pos.x() < (b.x() + b.width() * .5) || m == _score->lastMeasureMM())
                  anchor = m->canvasBoundingRect().topLeft();
            else
                  anchor = m->canvasBoundingRect().topRight();
            setDropAnchor(QLineF(pos, anchor));
            return true;
            }
      dragElement->score()->addRefresh(dragElement->canvasBoundingRect());
      setDropTarget(0);
      return false;
      }

//---------------------------------------------------------
//   dragEnterEvent
//---------------------------------------------------------

void ScoreView::dragEnterEvent(QDragEnterEvent* event)
      {
      double _spatium = score()->spatium();
      dragElement = 0;

      const QMimeData* data = event->mimeData();

      if (data->hasFormat(mimeSymbolListFormat) || data->hasFormat(mimeStaffListFormat)) {
            event->accept();
            return;
            }

      if (data->hasFormat(mimeSymbolFormat)) {
            event->accept();

            QByteArray a = data->data(mimeSymbolFormat);

            if (MScore::debugMode)
                  qDebug("ScoreView::dragEnterEvent Symbol: <%s>", a.data());

            XmlReader e(_score, a);
            dragOffset = QPoint();
            Fraction duration;  // dummy
            ElementType type = Element::readType(e, &dragOffset, &duration);

            Element* el = Element::create(type, score());
            if (el) {
                  if (type == ElementType::BAR_LINE || type == ElementType::ARPEGGIO || type == ElementType::BRACKET)
                        el->setHeight(_spatium * 5);
                  dragElement = el;
                  dragElement->setParent(0);
                  dragElement->read(e);
                  dragElement->layout();
                  }
            return;
            }

      if (data->hasUrls()) {
            QList<QUrl>ul = data->urls();
            for (const QUrl& u : ul) {
                  if (MScore::debugMode)
                        qDebug("drag Url: %s", qPrintable(u.toString()));
                  if (u.scheme() == "file" || u.scheme() == "http") {
                        QFileInfo fi(u.path());
                        QString suffix = fi.suffix().toLower();
                        if (suffix == "svg"
                           || suffix == "jpg"
                           || suffix == "jpeg"
                           || suffix == "png"
                           ) {
                              event->accept();
                              break;
                              }
                        }
                  }
            return;
            }
      qDebug("unknown drop format: formats:");
      for (const QString& s : data->formats())
            qDebug("  <%s>", qPrintable(s));
      event->ignore();
      }

//---------------------------------------------------------
//   dragMoveEvent
//---------------------------------------------------------

void ScoreView::dragMoveEvent(QDragMoveEvent* event)
      {
      // we always accept the drop action
      // to get a "drop" Event:

      if (mscore->state() == STATE_PLAY) {  // no editing during play
            event->ignore();
            return;
            }

      // convert window to canvas position
      QPointF pos(imatrix.map(QPointF(event->pos())));

      DropData dropData;
      dropData.view       = this;
      dropData.pos        = pos;
      dropData.dragOffset = dragOffset;
      dropData.element    = dragElement;
      dropData.modifiers  = event->keyboardModifiers();

      if (dragElement) {
            switch(dragElement->type()) {
                  case ElementType::IMAGE:
                  case ElementType::SYMBOL:
                        {
                        // dragSymbol(pos);
                        const QList<Element*> el = elementsAt(pos);
                        const Element* e = el.isEmpty() ? 0 : el[0];
                        if (e && (e->isNote() || e->isSymbol() || e->isImage() || e->isText())) {
                              DropData dropData;
                              dropData.view       = this;
                              dropData.pos        = pos;
                              dropData.element    = dragElement;
                              dropData.modifiers  = 0;

                              if (e->acceptDrop(dropData)) {
                                    setDropTarget(e);
                                    event->accept();
                                    return;
                                    }
                              else {
                                    setDropTarget(0);
                                    event->ignore();
                                    return;
                                    }
                              }
                        }
                        // fall through

                  case ElementType::VOLTA:
                  case ElementType::PEDAL:
                  case ElementType::DYNAMIC:
                  case ElementType::OTTAVA:
                  case ElementType::TRILL:
                  case ElementType::HAIRPIN:
                  case ElementType::TEXTLINE:
                  case ElementType::FRET_DIAGRAM:
                        if (dragTimeAnchorElement(pos))
                              event->accept();
                        else
                              event->ignore();
                        break;
                  case ElementType::KEYSIG:
                  case ElementType::CLEF:
                  case ElementType::TIMESIG:
                  case ElementType::BAR_LINE:
                  case ElementType::ARPEGGIO:
                  case ElementType::BREATH:
                  case ElementType::GLISSANDO:
                  case ElementType::BRACKET:
                  case ElementType::ARTICULATION:
                  case ElementType::CHORDLINE:
                  case ElementType::BEND:
                  case ElementType::ACCIDENTAL:
                  case ElementType::TEXT:
                  case ElementType::FINGERING:
                  case ElementType::TEMPO_TEXT:
                  case ElementType::STAFF_TEXT:
                  case ElementType::SYSTEM_TEXT:
                  case ElementType::NOTEHEAD:
                  case ElementType::TREMOLO:
                  case ElementType::LAYOUT_BREAK:
                  case ElementType::MARKER:
                  case ElementType::STAFF_STATE:
                  case ElementType::INSTRUMENT_CHANGE:
                  case ElementType::REHEARSAL_MARK:
                  case ElementType::JUMP:
                  case ElementType::REPEAT_MEASURE:
                  case ElementType::ICON:
                  case ElementType::CHORD:
                  case ElementType::SPACER:
                  case ElementType::SLUR:
                  case ElementType::HARMONY:
                  case ElementType::BAGPIPE_EMBELLISHMENT:
                  case ElementType::AMBITUS:
                  case ElementType::TREMOLOBAR:
                  case ElementType::FIGURED_BASS:
                  case ElementType::LYRICS:
                  case ElementType::STAFFTYPE_CHANGE:
                        {
                        QList<Element*> el = elementsAt(pos);
                        bool found = false;
                        setDropTarget(0);
                        for (const Element* e : el) {
                              if (e->acceptDrop(dropData)) {
                                    if (!e->isMeasure())
                                          setDropTarget(e);
                                    found = true;
                                    break;
                                    }
                              }
                        if (found)
                              event->accept();
                        else
                              event->ignore();
                        }
                        break;
                  default:
                        event->ignore();
                        break;
                  }

            // _score->update();
            return;
            }

      const QMimeData* md = event->mimeData();
      if (md->hasUrls()) {
            QList<QUrl>ul = md->urls();
            QUrl u = ul.front();
            if (u.scheme() == "file" || u.scheme() == "http") {
                  QFileInfo fi(u.path());
                  QString suffix(fi.suffix().toLower());
                  if (suffix != "svg"
                     && suffix != "jpg"
                     && suffix != "jpeg"
                     && suffix != "png"
                     ) {
                        return;
                        }
                  //
                  // special drop target Note
                  //
                  Element* el = elementAt(pos);
                  if (el && (el->isNote() || el->isRest()))
                        setDropTarget(el);
                  else
                        setDropTarget(0);
                  event->accept();
                  }
//            _score->update();
            return;
            }
      QByteArray data;
      ElementType etype;
      if (md->hasFormat(mimeSymbolListFormat)) {
            etype = ElementType::ELEMENT_LIST;
            data = md->data(mimeSymbolListFormat);
            }
      else if (md->hasFormat(mimeStaffListFormat)) {
            etype = ElementType::STAFF_LIST;
            data = md->data(mimeStaffListFormat);
            }
      else {
//            _score->update();
            return;
            }
      Element* el = elementAt(pos);
      if (el == 0 || el->type() != ElementType::MEASURE) {
//            _score->update();
            return;
            }
      else if (etype == ElementType::ELEMENT_LIST) {
            qDebug("accept drop element list");
            }
      else if (etype == ElementType::STAFF_LIST || etype == ElementType::MEASURE_LIST) {
//TODO            el->acceptDrop(this, pos, etype, e);
            }
//      _score->update();
      }

//---------------------------------------------------------
//   dropEvent
//---------------------------------------------------------

void ScoreView::dropEvent(QDropEvent* event)
      {
      QPointF pos(imatrix.map(QPointF(event->pos())));

      DropData dropData;
      dropData.view       = this;
      dropData.pos        = pos;
      dropData.dragOffset = dragOffset;
      dropData.element    = dragElement;
      dropData.modifiers  = event->keyboardModifiers();

      if (dragElement) {
            bool applyUserOffset = false;
            dragElement->styleChanged();
            _score->startCmd();
            Q_ASSERT(dragElement->score() == score());
            _score->addRefresh(dragElement->canvasBoundingRect());
            switch (dragElement->type()) {
                  case ElementType::VOLTA:
                  case ElementType::OTTAVA:
                  case ElementType::TRILL:
                  case ElementType::PEDAL:
                  case ElementType::HAIRPIN:
                  case ElementType::TEXTLINE:
                        {
                        Spanner* spanner = static_cast<Spanner*>(dragElement);
                        score()->cmdAddSpanner(spanner, pos);
                        score()->setUpdateAll();
                        event->acceptProposedAction();
                        }
                        break;
                  case ElementType::SYMBOL:
                  case ElementType::IMAGE:
                        applyUserOffset = true;
                        // fall-thru
                  case ElementType::DYNAMIC:
                  case ElementType::FRET_DIAGRAM:
                  case ElementType::HARMONY:
                        {
                        Element* el = elementAt(pos);
                        if (el == 0 || el->type() == ElementType::MEASURE) {
                              int staffIdx;
                              Segment* seg;
                              QPointF offset;
                              el = _score->pos2measure(pos, &staffIdx, 0, &seg, &offset);
                              if (el && el->type() == ElementType::MEASURE) {
                                    dragElement->setTrack(staffIdx * VOICES);
                                    dragElement->setParent(seg);
                                    if (applyUserOffset)
                                          dragElement->setUserOff(offset);
                                    score()->undoAddElement(dragElement);
                                    }
                              else {
                                    qDebug("cannot drop here");
                                    delete dragElement;
                                    }
                              }
                        else {
                              _score->addRefresh(el->canvasBoundingRect());
                              _score->addRefresh(dragElement->canvasBoundingRect());

                              if (!el->acceptDrop(dropData)) {
                                    qDebug("drop %s onto %s not accepted", dragElement->name(), el->name());
                                    break;
                                    }
                              Element* dropElement = el->drop(dropData);
                              _score->addRefresh(el->canvasBoundingRect());
                              if (dropElement) {
                                    _score->select(dropElement, SelectType::SINGLE, 0);
                                    _score->addRefresh(dropElement->canvasBoundingRect());
                                    }
                              }
                        }
                        event->acceptProposedAction();
                        break;
                  case ElementType::KEYSIG:
                  case ElementType::CLEF:
                  case ElementType::TIMESIG:
                  case ElementType::BAR_LINE:
                  case ElementType::ARPEGGIO:
                  case ElementType::BREATH:
                  case ElementType::GLISSANDO:
                  case ElementType::BRACKET:
                  case ElementType::ARTICULATION:
                  case ElementType::CHORDLINE:
                  case ElementType::BEND:
                  case ElementType::ACCIDENTAL:
                  case ElementType::TEXT:
                  case ElementType::FINGERING:
                  case ElementType::TEMPO_TEXT:
                  case ElementType::STAFF_TEXT:
                  case ElementType::SYSTEM_TEXT:
                  case ElementType::NOTEHEAD:
                  case ElementType::TREMOLO:
                  case ElementType::LAYOUT_BREAK:
                  case ElementType::MARKER:
                  case ElementType::STAFF_STATE:
                  case ElementType::INSTRUMENT_CHANGE:
                  case ElementType::REHEARSAL_MARK:
                  case ElementType::JUMP:
                  case ElementType::REPEAT_MEASURE:
                  case ElementType::ICON:
                  case ElementType::NOTE:
                  case ElementType::CHORD:
                  case ElementType::SPACER:
                  case ElementType::SLUR:
                  case ElementType::BAGPIPE_EMBELLISHMENT:
                  case ElementType::AMBITUS:
                  case ElementType::TREMOLOBAR:
                  case ElementType::FIGURED_BASS:
                  case ElementType::LYRICS:
                  case ElementType::STAFFTYPE_CHANGE:
                        {
                        Element* el = 0;
                        for (const Element* e : elementsAt(pos)) {
                              if (e->acceptDrop(dropData)) {
                                    el = const_cast<Element*>(e);
                                    break;
                                    }
                              }
                        if (!el) {
                              if (!dropCanvas(dragElement)) {
                                    qDebug("cannot drop %s(%p) to canvas", dragElement->name(), dragElement);
                                    delete dragElement;
                                    }
                              break;
                              }
                        _score->addRefresh(el->canvasBoundingRect());

                        // HACK ALERT!
                        if (el->isMeasure() && dragElement->isLayoutBreak()) {
                              Measure* m = toMeasure(el);
                              if (m->isMMRest())
                                    el = m->mmRestLast();
                              }

                        Element* dropElement = el->drop(dropData);
                        _score->addRefresh(el->canvasBoundingRect());
                        if (dropElement) {
                              if (!_score->noteEntryMode())
                                    _score->select(dropElement, SelectType::SINGLE, 0);
                              _score->addRefresh(dropElement->canvasBoundingRect());
                              }
                        event->acceptProposedAction();
                        }
                        break;
                  default:
                        delete dragElement;
                        break;
                  }
            dragElement = 0;
            setDropTarget(0); // this also resets dropRectangle and dropAnchor
            score()->endCmd();
            // update input cursor position (must be done after layout)
            if (noteEntryMode())
                  moveCursor();
            return;
            }

      if (event->mimeData()->hasUrls()) {
            QList<QUrl>ul = event->mimeData()->urls();
            QUrl u = ul.front();
            if (u.scheme() == "file") {
                  QFileInfo fi(u.path());
                  Image* s = new Image(score());
                  _score->startCmd();
                  QString str(u.toLocalFile());
                  s->load(str);
                  qDebug("drop image <%s> <%s>", qPrintable(str), qPrintable(str));

                  Element* el = elementAt(pos);
                  if (el) {
                        dropData.element = s;
                        if (el->acceptDrop(dropData)) {
                              dropData.element = s;
                              el->drop(dropData);
                              }
                        }
                  event->acceptProposedAction();
                  score()->endCmd();
                  setDropTarget(0); // this also resets dropRectangle and dropAnchor
                  return;
                  }
            else if (u.scheme() == "http") {
                  QNetworkAccessManager manager;
                  QNetworkReply* reply = manager.get(QNetworkRequest(u));

                  // TODO:
                  //    feed progress bar in loop
                  //    implement timeout/abort

                  QMutex mutex;
                  QWaitCondition wc;
                  while (!reply->isFinished()) {
                        mutex.lock();
                        wc.wait(&mutex, 100);
                        qApp->processEvents();
                        mutex.unlock();
                        }
                  QByteArray ba = reply->readAll();

                  Image* s = new Image(score());
                  s->loadFromData(u.path(), ba);
                  delete reply;

                  _score->startCmd();

                  Element* el = elementAt(pos);
                  if (el) {
                        dropData.element = s;
                        if (el->acceptDrop(dropData)) {
                              dropData.element = s;
                              el->drop(dropData);
                              }
                        }
                  event->acceptProposedAction();
                  score()->endCmd();
                  setDropTarget(0); // this also resets dropRectangle and dropAnchor
                  return;
                  }
            return;
            }

      dragElement = 0;
      const QMimeData* md = event->mimeData();
      QByteArray data;
      ElementType etype;
      if (md->hasFormat(mimeSymbolListFormat)) {
            etype = ElementType::ELEMENT_LIST;
            data = md->data(mimeSymbolListFormat);
            }
      else if (md->hasFormat(mimeStaffListFormat)) {
            etype = ElementType::STAFF_LIST;
            data = md->data(mimeStaffListFormat);
            }
      else {
            qDebug("cannot drop this object: unknown mime type");
            QStringList sl = md->formats();
            foreach(QString s, sl)
                  qDebug("  %s", qPrintable(s));
            _score->update();
            return;
            }

// qDebug("drop <%s>", data.data());

      Element* el = elementAt(pos);
      if (el == 0 || el->type() != ElementType::MEASURE) {
            setDropTarget(0);
            return;
            }
      Measure* measure = (Measure*) el;

      if (etype == ElementType::ELEMENT_LIST) {
            qDebug("drop element list");
            }
      else if (etype == ElementType::MEASURE_LIST || etype == ElementType::STAFF_LIST) {
            _score->startCmd();
            XmlReader xml(_score, data);
            System* s = measure->system();
            int idx   = s->y2staff(pos.y());
            if (idx != -1) {
                  Segment* seg = measure->first();
                  // assume there is always a ChordRest segment
                  while (!seg->isChordRestType())
                        seg = seg->next();
                  score()->pasteStaff(xml, seg, idx);
                  }
            event->acceptProposedAction();
            _score->endCmd();
            }
      setDropTarget(0); // this also resets dropRectangle and dropAnchor
      }

//---------------------------------------------------------
//   dragLeaveEvent
//---------------------------------------------------------

void ScoreView::dragLeaveEvent(QDragLeaveEvent*)
      {
      if (dragElement) {
            _score->setUpdateAll();
            delete dragElement;
            dragElement = 0;
            _score->update();
            }
      setDropTarget(0);
      }

//---------------------------------------------------------
//   dropCanvas
//---------------------------------------------------------

bool ScoreView::dropCanvas(Element* e)
      {
      if (e->type() == ElementType::ICON) {
            switch(static_cast<Icon*>(e)->iconType()) {
                  case IconType::VFRAME:
                        score()->insertMeasure(ElementType::VBOX, 0);
                        break;
                  case IconType::HFRAME:
                        score()->insertMeasure(ElementType::HBOX, 0);
                        break;
                  case IconType::TFRAME:
                        score()->insertMeasure(ElementType::TBOX, 0);
                        break;
                  case IconType::FFRAME:
                        score()->insertMeasure(ElementType::FBOX, 0);
                        break;
                  case IconType::MEASURE:
                        score()->insertMeasure(ElementType::MEASURE, 0);
                        break;
                  default:
                        return false;
                  }
            delete e;
            return true;
            }
      return false;
      }

}

