#include "districtarealistModel.h"


#include <QPixmap>
#include <QImageReader>
#include <QDebug>
#include <QUrl>

districtAreaListModel::districtAreaListModel(const QStringList &nameString, const QStringList &nameEnString, const QStringList &ppsString,
                                             const QStringList &percentString, const QStringList &typeString, const QPixmap p[], QObject *parent) :
        QAbstractListModel(parent), nameString(nameString), nameEnString(nameEnString), ppsString(ppsString), percentString(percentString),typeString(typeString)
{

    for(int i=0;i<151;i++){_p[i]=p[i];}
    arrow = QPixmap::fromImage(QImage(":/new/prefix1/Image/arrow_right.png"));
    up = QPixmap::fromImage(QImage(":/new/prefix1/Image/arrow_up.png"));
    down = QPixmap::fromImage(QImage(":/new/prefix1/Image/arrow_down.png"));

}

int districtAreaListModel::rowCount(const QModelIndex &parent) const
{

    return nameString.count();
}

QVariant districtAreaListModel::data(const QModelIndex &index, int role) const
{
    if ( (!index.isValid()) || (index.row() >= nameString.size()))
        return QVariant();


    if(role == Qt::UserRole)
    {
        qDebug()<<"1";
        QString string = nameString.at(index.row());
        string.replace("‧",".");
        return string;
    }
    else if( role == Qt::UserRole+1)
    {
        qDebug()<<"2";
        return _p[index.row()];
    }
    else if( role == Qt::UserRole+2)
    {
//        QImageReader reader;
//        reader.setFileName(":/new/prefix1/Image/arrow_right.png");
//        QSize imageSize = reader.size();
//        imageSize.scale(QSize(20,20), Qt::KeepAspectRatio);
//        reader.setScaledSize(imageSize);
//        QImage image = reader.read();
//        QPixmap pixmap = QPixmap::fromImage(image);

        return arrow;
    }
    else if( role == Qt::UserRole+3)
    {
        qDebug()<<"3";
        return nameEnString.at(index.row());
    }

    else if( role == Qt::UserRole+4)
    {
        //return "";
        qDebug()<<"4";
        if(ppsString.size() > 0)
            return ppsString.at(index.row());
        else
            return "";
    }

    else if( role == Qt::UserRole+5)
    {
        //return "";
        qDebug()<<"5";

        if(percentString.size() > 0 )
        {
            QString tmp = percentString.at(index.row());
            if(tmp.length() > 0)
                return percentString.at(index.row());
            else
                return "";
        }
        else
            return "";
    }

    else if( role == Qt::UserRole+6)
    {
        qDebug()<<"6";
        if(typeString.size() > 0 )
        {
            QString tmp = typeString.at(index.row());
            if(tmp.length()>0)
            {
            if(typeString.at(index.row()) == "1")
                return up;
            else if(typeString.at(index.row()) == "-1")
                return down;
            else
                return "";
            }
            else
                return "";
        }
        else
            return "";
    }



    return QVariant();
}



void districtAreaListModel::setStrings(const QStringList & strings)
{

    nameString = strings;
    reset();
}
