/***************************** LICENSE START ***********************************

 Copyright 2014 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

#include <QDebug>
#ifdef METVIEW_QT5
#include <QDataStream>
#endif

#include "MvQIconMimeData.h"
#include "MvQFolderModel.h"
#include "IconClass.h"
#include "IconObject.h"

//=================================
//
//  MvQNewIconMimeData
//
//=================================

MvQNewIconMimeData::MvQNewIconMimeData(QString className,IconDefType iconDefType) :
  className_(className),
  iconDefType_(iconDefType)
{
  	formats_ << "metview/new_icon";
	formats_ << "text/plain" << "text/html";	
}  

QStringList MvQNewIconMimeData::formats () const
{
	return formats_;
}

QVariant MvQNewIconMimeData::retrieveData(const QString& /*format*/,QVariant::Type /*type*/) const
{	
	return QVariant();
	
	//return retrieveData(format,type);
}

//=================================
//
//  MvQIconMimeData
//
//=================================

MvQIconMimeData::MvQIconMimeData(MvQFolderModel* model,IconObject* dragObj,
				 QPointF hotSpot,QList<IconObject*> objects,
				 QList<QPoint> pixDistances,ClipboardAction clipboardAction) :
  model_(model), 
  dragObject_(dragObj),
  hotSpotInVisRect_(hotSpot),
  objects_(objects),
  pixDistances_(pixDistances),
  fromHelper_(false),
  clipboardAction_(clipboardAction)
{
	formats_ << "metview/icon";
	formats_ << "text/plain" << "text/html";	
}  

QStringList MvQIconMimeData::formats () const
{
	return formats_;
}

QVariant MvQIconMimeData::retrieveData(const QString & format,QVariant::Type type) const
{
	if(format == "metview/icon")
	{
		QByteArray encodedData;
		QDataStream stream(&encodedData, QIODevice::WriteOnly);
		stream << QString::number(objects_.count());
		
		foreach(IconObject* obj, objects_)
		{		  
			stream << QString::fromStdString(obj->fullName());
			//stream << QString::fromStdString(obj->className());
			stream << QString::fromStdString(obj->editorClass().name());
		}				
			
		return encodedData;
	}
	else if(format == "text/plain")
	{
	  	QString s= "file://" + QString::fromStdString(objects_.at(0)->path().str());
	  	return s;	
	}
	else if(format == "text/html")
	{
	  	//return "file://" + path_;		
	}
	else if(format == "image/png")
	{
	  	QString s= "file://" + QString::fromStdString(objects_.at(0)->path().str());
	  	qDebug() << "PNG" << s;
	  	return s;	
	}
	
	return QMimeData::retrieveData(format,type);
}
