/*
 * Copyright (C) 2016 ~ 2018 Wuhan Deepin Technology Co., Ltd.
 *
 * Author:     Iceyer <me@iceyer.net>
 *
 * Maintainer: Iceyer <me@iceyer.net>
 *
 * 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
 * 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 <http://www.gnu.org/licenses/>.
 */

#include "searchresult.h"

#include <QDebug>
#include <QVBoxLayout>
#include <QListView>
#include <QPushButton>
#include <QStringListModel>

#include <dlistview.h>
#include <DThemeManager>
#include <QGraphicsDropShadowEffect>

using namespace Dtk::Widget;

SearchResult::SearchResult(QWidget *parent) : QFrame(parent)
{
    DThemeManager::instance()->registerWidget(this);
    setMouseTracking(true);

    auto vlayout = new QVBoxLayout();
    setLayout(vlayout);
    vlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->setSpacing(0);

    m_model = new QStringListModel(this);

    m_searchResult = new QListView;
    m_searchResult->setContentsMargins(0, 0, 0, 0);
    m_searchResult->setMouseTracking(true);
    m_searchResult->setObjectName("SearchResultList");
    m_searchResult->setSelectionMode(QListView::SingleSelection);
    m_searchResult->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_searchResult->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_searchResult->setFocusPolicy(Qt::NoFocus);

    QStringList a;
    a << "1 Music Result" << "2 Music Result";
    a << "3 Music Result" << "4 Music Result";
    a << "5 Music Result" << "6 Music Result";
    a << "7 Music Result" << "8 Music Result";
    a << "9 Music Result" << "10 Music Result";

    m_model->setStringList(a);
    m_searchResult->setModel(m_model);
    vlayout->addWidget(m_searchResult, 0, Qt::AlignCenter);

    this->setMinimumHeight(25);
    m_searchResult->setMinimumHeight(25);
    m_searchResult->adjustSize();

    connect(m_searchResult, &QListView::clicked,
    this, [ = ](const QModelIndex & index) {
        Q_EMIT itemSelected(index.data().toString());
    });

    connect(m_searchResult, &QListView::entered,
    this, [ = ](const QModelIndex & index) {
        m_searchResult->setCurrentIndex(index);
    });
}

void SearchResult::autoResize()
{
    m_searchResult->setFixedHeight(m_model->rowCount() * (25) + 2);
    m_searchResult->setFixedWidth(this->size().width() - 2);

    this->setFixedHeight(m_searchResult->height() + 10);
    m_searchResult->setVisible(!(0 == m_model->rowCount()));
    this->adjustSize();
    m_searchResult->raise();
}


void SearchResult::setResultList(const QStringList &titlelist)
{
    m_model->setStringList(titlelist);
}

bool SearchResult::isSelected()
{
    return (m_searchResult->currentIndex().row() >= 0);
}

void SearchResult::selectUp()
{
    auto newSelectedRow = m_searchResult->currentIndex().row() - 1;
    if (newSelectedRow < 0) {
        m_searchResult->setCurrentIndex(QModelIndex());
        return;
    }
    QModelIndex indexOfTheCellIWant = m_model->index(newSelectedRow, 0);
    m_searchResult->setCurrentIndex(indexOfTheCellIWant);
}

void SearchResult::selectDown()
{
    auto newSelectedRow = m_searchResult->currentIndex().row() + 1;
    if (newSelectedRow >= m_model->rowCount()) {
        m_searchResult->setCurrentIndex(QModelIndex());
        return;
    }
    QModelIndex indexOfTheCellIWant = m_model->index(newSelectedRow, 0);
    m_searchResult->setCurrentIndex(indexOfTheCellIWant);
}

void SearchResult::accpectSelect()
{
    Q_EMIT itemSelected(m_searchResult->currentIndex().data().toString());
}

void SearchResult::leaveEvent(QEvent *event)
{
    m_searchResult->setCurrentIndex(QModelIndex());
    QFrame::leaveEvent(event);
}

void SearchResult::onReturnPressed()
{
    Q_EMIT itemSelected(m_searchResult->currentIndex().data().toString());
}
