/*
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "hotcity.h"

HotCity::HotCity(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(ViewVar::HOT);
    this->setAttribute(Qt::WA_TranslucentBackground);

    m_title = new QLabel(this);
    m_title->setText(tr("Hot City"));
    m_title->setFixedWidth(this->width());
    m_title->move(ViewVar::HOT_TITLE);

    m_hotWid = new QWidget(this);
    m_hotWid->setFixedSize(ViewVar::HOT_LIST);
    m_hotWid->move(ViewVar::HOTLISTX, m_title->y() + m_title->height());
    m_hotWid->setAttribute(Qt::WA_TranslucentBackground);
    //得到热门城市map
    m_btnList = ViewVar::saveHotCity();
    //初始化每个按钮
    QMap<int, QStringList>::iterator iter = m_btnList.begin();
    while (iter != m_btnList.end()) {
        HotcityBtn *m_addcityBtn = new HotcityBtn(m_hotWid, iter.value().at(0), iter.value().at(1));
        QString fullText = m_addcityBtn->text();
        QString smpText = m_addcityBtn->text();
        //长度超过3，...显示省略部分
        if (smpText.length() > ViewVar::ITEM_LENGTHM) {
            smpText = smpText.mid(0, 2) + "…";
            m_addcityBtn->setText(smpText);
            m_addcityBtn->setToolTip(fullText);
        } else {
            m_addcityBtn->setToolTip("");
        }
        m_list.append(m_addcityBtn);
        iter++;
    }

    initPostion();
    initConnect();
}
//设置每个按钮的位置
void HotCity::initPostion()
{
    for (int i = 0; i < m_list.length(); i++) {
        if (i == 0) {
            m_list.at(i)->move(i * ViewVar::ITEM_POS + 16, 0);
        } else if (i <= ViewVar::ITEM_NUM) {
            m_list.at(i)->move(i * ViewVar::ITEM_POS, 0);
        } else if (ViewVar::ITEM_NUM < i && i <= ViewVar::ITEM_NUMM) {
            if (i == 6) {
                m_list.at(i)->move((i - ViewVar::ITEM_DIS0) * (ViewVar::ITEM_POS) + 16,
                                   m_list.at(0)->height() + m_list.at(0)->y());
            } else {
                m_list.at(i)->move((i - ViewVar::ITEM_DIS0) * (ViewVar::ITEM_POS),
                                   m_list.at(0)->height() + m_list.at(0)->y());
            }
        } else {
            if (i == 12) {
                m_list.at(i)->move((i - ViewVar::ITEM_DIS2) * (ViewVar::ITEM_POS) + 16, m_list.at(0)->height() * 2);
            } else {
                m_list.at(i)->move((i - ViewVar::ITEM_DIS2) * (ViewVar::ITEM_POS), m_list.at(0)->height() * 2);
            }
        }
    }
}
//点击发送对应的id值
void HotCity::initConnect()
{
    for (int i = 0; i < m_list.length(); i++) {
        connect(m_list.at(i), &HotcityBtn::hotCityClick, this, &HotCity::setHotCity);
    }
}
