#include "imageinfo.h"
#include <QSettings>
#include <QHeaderView>

QMap<QString, QColor> headerHighlight;

ImageInfo::ImageInfo(QWidget *parent) : QTreeWidget(parent)
{
    setColumnCount(3);
    setHeaderLabels({tr("Property"), tr("Value"), tr("Comment")});
    setIndentation(5);
    QSettings settings;
    header()->restoreState(settings.value("imageinfo/headerstate").toByteArray());
    setSortingEnabled(true);
    header()->setSortIndicatorClearable(true);
}

ImageInfo::~ImageInfo()
{
    QSettings settings;
    settings.setValue("imageinfo/headerstate", header()->saveState());
}

void ImageInfo::setInfo(const ImageInfoData &info)
{
    clear();
    if(info.fitsHeader.size())
    {
        QTreeWidgetItem *fitsHeader = new QTreeWidgetItem({tr("FITS Header")});
        for(const FITSRecord &record : info.fitsHeader)
        {
            QTreeWidgetItem *item = new QTreeWidgetItem(fitsHeader, {record.key, record.value.toString().left(1024), record.comment});
            if(headerHighlight.contains(record.key))
            {
                QColor color = headerHighlight[record.key];
                item->setBackground(0, color);
                item->setBackground(1, color);
                item->setBackground(2, color);
            }
        }
        addTopLevelItem(fitsHeader);
    }
    if(info.info.size())
    {
        QTreeWidgetItem *infoHeader = new QTreeWidgetItem({tr("Image info")});
        for(auto &item : info.info)
        {
            new QTreeWidgetItem(infoHeader, {item.first, item.second});
        }
        addTopLevelItem(infoHeader);
    }
    expandAll();
}
