/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2024 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include "settinggroup.h"
#include <QApplication>
#include <QPainter>

SettingGroup::SettingGroup(QWidget *parent, Shape shape, UkccFrame::BorderRadiusStyle style) : UkccFrame(parent, style, false)
{
    mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(0);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(mainLayout);
    this->setFrameShape(shape);
    qApp->installEventFilter(this);
    this->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
}

SettingGroup::~SettingGroup()
{
}

void SettingGroup::addWidget(QFrame *item, bool autoUpdateShape, bool visible)
{
    mainLayout->addWidget(item, autoUpdateShape);
    item->setVisible(visible);
    if (autoUpdateShape) {
        updateShape();
    }
}

void SettingGroup::insertWidget(int index, QFrame *item, bool autoUpdateShape, bool visible)
{
    mainLayout->insertWidget(index, item);
    item->setVisible(visible);
    if (autoUpdateShape) {
        updateShape();
    }
}

void SettingGroup::removeWidget(QWidget *w, bool autoUpdateShape)
{
    mainLayout->removeWidget(w);
    if (autoUpdateShape) {
        updateShape();
    }
}

void SettingGroup::updateAllItemList(UkccFrame *frame)
{
    if (frame->iscontainer() && frame->frameShape() == QFrame::NoFrame && frame->layout()) {
        for (int i = 0; i < frame->layout()->count(); i++) {
            QLayoutItem *it = frame->layout()->itemAt(i);
            UkccFrame *item = qobject_cast<UkccFrame *>(it->widget());
            if (!item) {
                continue;
            }
            updateAllItemList(item);
        }
    } else {
        allItemList.append(frame);
        return;
    }
}

void SettingGroup::updateShowItemList(UkccFrame *frame)
{
    if (frame->iscontainer() && frame->frameShape() == QFrame::NoFrame && frame->layout()) {
        for (int i = 0; i < frame->layout()->count(); i++) {
            QLayoutItem *it = frame->layout()->itemAt(i);
            UkccFrame *item = qobject_cast<UkccFrame *>(it->widget());
            if (!item) {
                continue;
            }
            updateShowItemList(item);
        }
    } else {
        if (frame->isVisibleTo(this)) {
            showItemList.append(frame);
        }
        return;
    }
}

void SettingGroup::updateShape()
{
    showItemList.clear();
    for (int i = 0; i < mainLayout->count(); i++) {
        QLayoutItem *it1 = mainLayout->itemAt(i);
        KHLineFrame *itemLine = qobject_cast<KHLineFrame *>(it1->widget());
        if (itemLine) {
            // 如果布局中第一个或最后一个是分割线,移除分割线
            if (i == 0 || i == mainLayout->count() -1) {
                itemLine->setVisible(false);
            }

            // 如果下一个UkccFrame不可见,隐藏此分割线
            if (i+1 < mainLayout->count()) {
                QLayoutItem *itnext = mainLayout->itemAt(i+1);
                UkccFrame *itemnext = qobject_cast<UkccFrame *>(itnext->widget());
                if (itemnext) {
                    if (!itemnext->isVisibleTo(this)) {
                        itemLine->setVisible(false);
                    } else {
                        if (!itemLine->isVisibleTo(this)) {
                            itemLine->setVisible(true);
                        }
                    }
                }
            }

            if (i-1 >= 0) {
                QLayoutItem *itpre = mainLayout->itemAt(i-1);
                UkccFrame *itempre = qobject_cast<UkccFrame *>(itpre->widget());
                // 如果是第一个不可见,隐藏该分割线
                if (i - 1 == 0) {
                    if (!itempre->isVisibleTo(this)) {
                        itemLine->setVisible(false);
                    } else {
                        // 如果第一个可见，则判断下一个是否可见，下一个可见才显示此分割线
                        if (i+1 < mainLayout->count()) {
                            QLayoutItem *itnext = mainLayout->itemAt(i+1);
                            UkccFrame *itemnext = qobject_cast<UkccFrame *>(itnext->widget());
                            if (itemnext) {
                                if (itemnext->isVisibleTo(this)) {
                                    if (!itemLine->isVisibleTo(this)) {
                                        itemLine->setVisible(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        QLayoutItem *it = mainLayout->itemAt(i);
        UkccFrame *item = qobject_cast<UkccFrame *>(it->widget());
        if (!item) {
            continue;
        }

        // 如果当前frame与下一个frame都是UkccFrame，则在中间添加一个KHLineFrame
        if (i+1 < mainLayout->count()) {
            QLayoutItem *it1 = mainLayout->itemAt(i+1);
            UkccFrame *item1 = qobject_cast<UkccFrame *>(it1->widget());
            if (item && item1) {
                KHLineFrame *line = new KHLineFrame(this);
                mainLayout->insertWidget(i+1, line);
            }
        }

        updateShowItemList(item);
    }

    for (int i = 0; i < showItemList.size(); i++) {
        UkccFrame *item = showItemList.at(i);
        if (showItemList.size() == 1) {
            item->setRadiusType(m_OneRadiusStyle);
        } else if (i == 0) {
            item->setRadiusType(UkccFrame::Top);
        } else if (i == showItemList.size() - 1) {
            item->setRadiusType(m_lastRadiusStyle);
        } else {
            item->setRadiusType(UkccFrame::None);
        }
    }
}

QVBoxLayout *SettingGroup::layout()
{
    return mainLayout;
}

void SettingGroup::setLastRadiusStyle(UkccFrame::BorderRadiusStyle style)
{
    m_lastRadiusStyle = style;
}

void SettingGroup::setOneRadiusStyle(UkccFrame::BorderRadiusStyle style)
{
    m_OneRadiusStyle = style;
}

bool SettingGroup::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::Hide || event->type() == QEvent::Show || event->type() == QEvent::HideToParent || event->type() == QEvent::ShowToParent) {
        allItemList.clear();
        for (int i = 0; i < mainLayout->count(); i++) {
             QLayoutItem *it = mainLayout->itemAt(i);
             UkccFrame *item = qobject_cast<UkccFrame *>(it->widget());
             if (!item) {
                 continue;
             }
             updateAllItemList(item);
        }

        for (int i = 0; i < allItemList.size(); i++) {
            if (allItemList.at(i) == watched) {
               updateShape();
            }
        }
    }
    return UkccFrame::eventFilter(watched, event);
}

int SettingGroup::showItemsCount()
{
    showItemList.clear();
    for (int i = 0; i < mainLayout->count(); i++) {
        QLayoutItem *it = mainLayout->itemAt(i);
        UkccFrame *item = qobject_cast<UkccFrame *>(it->widget());
        if (!item) {
            continue;
        }
        updateShowItemList(item);
    }
    return showItemList.size();
}

void SettingGroup::removeAndDeleteAllWidget()
{
    while (mainLayout->count() > 0) {
        QLayoutItem *it = mainLayout->itemAt(0);
        UkccFrame *item = qobject_cast<UkccFrame *>(it->widget());
        if (!item) {
            continue;
        }
        mainLayout->removeWidget(item);
        item->deleteLater();
        item = nullptr;
    }
    return;
}


void SettingGroup::paintEvent(QPaintEvent *event)
{
    UkccFrame::paintEvent(event);
}


