#include "eweather.h"
#include "eweatherattr.h"
#include "QSettings"
eWeather::eWeather(QRectF rectInit,InteractiveType type, QGraphicsItem *parent) :
    eObject(rectInit, type, parent)
{
    m_iType=eObject::Weather;
    m_attr.country       = 0;
    m_attr.region1       = 0;
    m_attr.region2       = 0;
    m_attr.weather       = true;
    m_attr.temp          = true;
    m_attr.wind          = true;
    m_attr.humidity      = true;
    m_attr.curTemp       = true;
    m_attr.tempType      = 0;
    m_attr.tempUnitType  = 0;
    m_attr.labelWeather  = tr("Weather:");
    m_attr.labelTemp     = tr("Temperature:");
    m_attr.labelWind     = tr("Wind:");
    m_attr.labelHumidity = tr("Humidity:");
    m_attr.labelCurTemp  = tr("Current Temperature:");
    m_attr.lineStyle     = 0;
    m_attr.fontFamily    = "Arial";
    m_attr.fontSize      = 9;
    m_attr.fontBold      = false;
    m_attr.fontItalics   = false;
    m_attr.fontUnderline = false;
    m_attr.textColor     = Qt::red;
    m_attr.playRefresh   = 0;
    m_attr.playDuration  = MACRO_DEFAULT_PLAYDURATION;
    init();
}

eWeather::eWeather(const QJsonObject &json, InteractiveType type, QGraphicsItem *parent) :
    eObject(json["geometry"].toObject(), type, parent)
{
    m_iType=eObject::Weather;
    setElement(json, m_attr);
    init();
}

void eWeather::init()
{
}

void eWeather::setElement(const QJsonObject &json, Data &attr)
{
    auto at = LoAppTools::getInstance();
    attr.country       =               json["widget"]["country"]      .toInt();
    attr.region1       =               json["widget"]["region1"]      .toInt();
    attr.region2       =               json["widget"]["region2"]      .toInt();
    attr.weather       =               json["widget"]["weather"]      .toBool();
    attr.temp          =               json["widget"]["temp"]         .toBool();
    attr.wind          =               json["widget"]["wind"]         .toBool();
    attr.humidity      =               json["widget"]["humidity"]     .toBool();
    attr.curTemp       =               json["widget"]["curTemp"]      .toBool();
    attr.tempType      =               json["widget"]["tempType"]     .toInt();
    attr.tempUnitType  =               json["widget"]["tempUnitType"] .toInt();
    attr.labelWeather  =               json["widget"]["labelWeather"] .toString();
    attr.labelTemp     =               json["widget"]["labelTemp"]    .toString();
    attr.labelWind     =               json["widget"]["labelWind"]    .toString();
    attr.labelHumidity =               json["widget"]["labelHumidity"].toString();
    attr.labelCurTemp  =               json["widget"]["labelCurTemp"] .toString();
    attr.lineStyle     =               json["widget"]["lineStyle"]    .toInt();
    attr.fontFamily    =               json["widget"]["fontFamily"]   .toString();
    attr.fontSize      =               json["widget"]["fontSize"]     .toInt();
    attr.fontBold      =               json["widget"]["fontBold"]     .toBool();
    attr.fontItalics   =               json["widget"]["fontItalics"]  .toBool();
    attr.fontUnderline =               json["widget"]["fontUnderline"].toBool();
    attr.textColor     = at->int2Color(json["widget"]["textColor"]    .toInt());
    attr.playRefresh   = json["play"]["refresh"].toInt();
    attr.playDuration  = json["play"]["duration"].toInt();
}

void eWeather::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QString sUnit;
    if(m_attr.tempUnitType == 0) {
        if(m_attr.tempType == 0) {
            sUnit = QStringLiteral("℃");
        } else {
            sUnit = QStringLiteral("℉");
        }
    } else {
        sUnit = QStringLiteral("°");
    }

    QTextOption opt;
    opt.setAlignment(Qt::AlignLeft | Qt:: AlignVCenter);
    QFont font;
    font.setFamily(m_attr.fontFamily);
    font.setPointSize(m_attr.fontSize);
    font.setBold(m_attr.fontBold);
    font.setItalic(m_attr.fontItalics);
    font.setUnderline(m_attr.fontUnderline);
    MACRO_FONT_FANG_JUCHI_2(font)
    painter->save();
    painter->setRenderHint(QPainter::Antialiasing, false);

    painter->setFont(font);
    painter->setPen(m_attr.textColor);

    if(m_attr.lineStyle == 0){
        int cnt = 0;
        if(m_attr.weather)  cnt++;
        if(m_attr.temp)     cnt++;
        if(m_attr.wind)     cnt++;
        if(m_attr.humidity) cnt++;
        if(m_attr.curTemp)  cnt++;
        qreal height = rect().height() / cnt;
        qreal my = 0;

        if(m_attr.weather) {
            QRectF r(0, my, rect().width(), height);
            QString s = QString(m_attr.labelWeather + "%1").arg(tr("Shower"));
            painter->drawText(r, s, opt);
            my += height;
        }
        if(m_attr.temp) {
            QRectF r(0, my, rect().width(), height);
            QString s = QString(m_attr.labelTemp + "%1~%2" + sUnit).arg(15).arg(21);
            painter->drawText(r, s, opt);
            my += height;
        }
        if(m_attr.wind) {
            QRectF r(0, my, rect().width(), height);
            QString s = QString(m_attr.labelWind + "%1").arg(tr("No Continuous, Range 4-5"));
            painter->drawText(r, s, opt);
            my += height;
        }
        if(m_attr.humidity) {
            QRectF r(0, my, rect().width(), height);
            QString s =  QString(m_attr.labelHumidity + "%1%").arg(21);
            painter->drawText(r, s, opt);
            my += height;
        }
        if(m_attr.curTemp) {
            QRectF r(0, my, rect().width(), height);
            QString s = QString(m_attr.labelCurTemp + "%1" + sUnit).arg(21);
            painter->drawText(r, s, opt);
            my += height;
        }
    } else {
        QString s;
        if(m_attr.weather)  s += QString(m_attr.labelWeather + "%1").arg(tr("Shower"));
        if(m_attr.temp)     s += QString(m_attr.labelTemp + "%1~%2" + sUnit).arg(15).arg(21);
        if(m_attr.wind)     s += QString(m_attr.labelWind + "%1").arg(tr("No Continuous, Range 4-5"));
        if(m_attr.humidity) s += QString(m_attr.labelHumidity + "%1%").arg(21);
        if(m_attr.curTemp)  s += QString(m_attr.labelCurTemp + "%1" + sUnit).arg(21);
        painter->drawText(rect(), s, opt);
    }
    painter->restore();


    eObject::paint(painter, option, widget);
}

QWidget* eWeather::wAttr()
{
    QWidget *wObj = eObject::wAttr();
    QWidget *w = wAttrElement();
    static_cast<QBoxLayout*>(w->layout())->insertWidget(0, wObj);
    return w;
}

QWidget* eWeather::wAttrElement()
{
    eWeatherAttr *w = new eWeatherAttr(m_attr);
    connect(w, SIGNAL(sAttrChanged(const eWeather::Data &)), this, SLOT(onAttrChanged(const eWeather::Data &)));
    return w;
}

QJsonObject eWeather::elementJson() const
{
    QJsonObject oRoot;
    QJsonObject oWidget;
    QJsonObject oPlay;
    auto at = LoAppTools::getInstance();

    oRoot["elementType"]   = "Weather";
    oRoot["elementTypeId"] = type();
    oRoot["geometry"]      = eObject::elementJson();

    oWidget["country"]       = m_attr.temp;
    oWidget["region1"]       = m_attr.region1;
    oWidget["region2"]       = m_attr.region2;
    oWidget["weather"]       = m_attr.weather;
    oWidget["temp"]          = m_attr.temp;
    oWidget["wind"]          = m_attr.wind;
    oWidget["humidity"]      = m_attr.humidity;
    oWidget["curTemp"]       = m_attr.curTemp;
    oWidget["tempType"]      = m_attr.tempType;
    oWidget["tempUnitType"]  = m_attr.tempUnitType;
    oWidget["labelWeather"]  = m_attr.labelWeather;
    oWidget["labelTemp"]     = m_attr.labelTemp;
    oWidget["labelWind"]     = m_attr.labelWind;
    oWidget["labelHumidity"] = m_attr.labelHumidity;
    oWidget["labelCurTemp"]  = m_attr.labelCurTemp;
    oWidget["lineStyle"]     = m_attr.lineStyle;
    oWidget["fontFamily"]    = m_attr.fontFamily;
    oWidget["fontSize"]      = m_attr.fontSize;
    oWidget["fontBold"]      = m_attr.fontBold;
    oWidget["fontItalics"]   = m_attr.fontItalics;
    oWidget["fontUnderline"] = m_attr.fontUnderline;
    oWidget["textColor"]     = at->color2Int(m_attr.textColor);
    oRoot["widget"]          = oWidget;

    oPlay["refresh"]  = m_attr.playRefresh;
    oPlay["duration"] = m_attr.playDuration;
    oRoot["play"]     = oPlay;

    return  oRoot;
}

void eWeather::onAttrChanged(const eWeather::Data &data)
{
    m_attr = data;
    updateGeometry();
}
