/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2019 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 "areaui.h"
#include <kysdk/applications/accessinfohelper.h>

AreaUi::AreaUi(QWidget *parent, Qt::WindowFlags f)
    : QWidget(parent, f)
{
    setAttribute(Qt::WA_DeleteOnClose);
    uiLayout       = new QVBoxLayout(this);
    formatLayout   = new QVBoxLayout(this);
    LanguageLayout = new QVBoxLayout(this);
    uiLayout->addLayout(formatLayout);
    uiLayout->addLayout(LanguageLayout);
    uiLayout->addStretch();
    uiLayout->setSpacing(40);
    uiLayout->setMargin(0);
    formatLayout->setSpacing(8);
    LanguageLayout->setSpacing(1);
    formatLayout->setMargin(0);
    LanguageLayout->setMargin(0);
    initUi();
}

AreaUi::~AreaUi()
{

}

void AreaUi::initUi()
{
    initLanguageFormat();
    initSystemLanguage();
}

void AreaUi::initLanguageFormat()
{
    formatTitleLabel = new KLabel(this);
    KDK_EXTEND_ALL_INFO_FORMAT(formatTitleLabel, "Area", "", "");
    formatTitleLabel->setContentsMargins(16, 0, 0, 0);
    formatGroup      = new SettingGroup(this);
    //~ contents_path /Area/Regional Format
    countryWidget    = new ComboxWidget(tr("Regional Format"), formatGroup, UkccFrame::BorderRadiusStyle::Top);
    KDK_EXTEND_ALL_INFO_FORMAT(countryWidget, "Area", "", "");
    //~ contents_path /Area/Calendar
    calendarWidget   = new ComboxWidget(tr("Calendar"), formatGroup, UkccFrame::BorderRadiusStyle::None);
    KDK_EXTEND_ALL_INFO_FORMAT(calendarWidget, "Area", "", "");
    //~ contents_path /Area/First Day Of Week
    dayWidget        = new ComboxWidget(tr("First Day Of Week"), formatGroup, UkccFrame::BorderRadiusStyle::None);
    KDK_EXTEND_ALL_INFO_FORMAT(dayWidget, "Area", "", "");

    //~ contents_path /Area/Short Foramt Date
    shortDateWidget  = new ComboxWidget(tr("Short Foramt Date"), formatGroup, UkccFrame::BorderRadiusStyle::None);
    KDK_EXTEND_ALL_INFO_FORMAT(shortDateWidget, "Area", "", "");

    //~ contents_path /Area/Long Format Date
    longDateWidget   = new ComboxWidget(tr("Long Format Date"), formatGroup, UkccFrame::BorderRadiusStyle::None);
    KDK_EXTEND_ALL_INFO_FORMAT(longDateWidget, "Area", "", "");

    //~ contents_path /Area/Time
    timeWidget       = new ComboxWidget(tr("Time"), formatGroup, UkccFrame::BorderRadiusStyle::None);
    KDK_EXTEND_ALL_INFO_FORMAT(timeWidget, "Area", "", "");

    formatLayout->addWidget(formatTitleLabel);
    formatLayout->addWidget(formatGroup);
    formatGroup->addWidget(countryWidget);
    formatGroup->addWidget(calendarWidget);
    formatGroup->addWidget(dayWidget);
    formatGroup->addWidget(shortDateWidget);
#ifdef Nile
    formatGroup->addWidget(longDateWidget);
#endif
    formatGroup->addWidget(timeWidget);
    //~ contents_path /Area/Language Format
    formatTitleLabel->setText(tr("Language Format"));

    if (UkccCommon::isOpenkylinNile() || UkccCommon::isV11()) {
        m_formatDateFrame = new UkccFrame(formatGroup);
        m_formatDateFrame->setMinimumSize(QSize(550, 0));
        m_formatDateFrame->setMaximumSize(QSize(16777215, 16777215));
        m_formatDateFrame->setFrameShape(QFrame::Box);

        QHBoxLayout* formatDateLayout = new QHBoxLayout();
        formatDateLayout->setContentsMargins(16, 0, 16, 0);
        //~ contents_path /Area/Language Format Example
        m_formatDateNameLabel = new KLabel(m_formatDateFrame);
        KDK_EXTEND_ALL_INFO_FORMAT(m_formatDateNameLabel, "Area", "", "");
        m_formatDateNameLabel->setText(tr("Language Format Example"));
        m_formatDateNameLabel->setFixedSize(214, 59);

        m_formatDateValueLabel = new KLabel(m_formatDateFrame);
        KDK_EXTEND_ALL_INFO_FORMAT(m_formatDateValueLabel, "Area", "", "");

        formatDateLayout->addWidget(m_formatDateNameLabel);
        formatDateLayout->setSpacing(16);
        formatDateLayout->addWidget(m_formatDateValueLabel);

        m_formatDateFrame->setLayout(formatDateLayout);
        formatGroup->addWidget(m_formatDateFrame);
    }
}

void AreaUi::initSystemLanguage()
{
    languageTitleLable   = new KLabel(this);
    KDK_EXTEND_ALL_INFO_FORMAT(languageTitleLable, "Area", "", "");
    languageTitleLable->setContentsMargins(16, 0, 0, 0);
    languageGroup        = new SettingGroup(this);
    addLanguageBtn       = new AddButton(this, UkccFrame::BorderRadiusStyle::Bottom);
    KDK_EXTEND_ALL_INFO_FORMAT(addLanguageBtn, "Area", "", "");
    addLanguageDialog    = new AddLanguageDialog(this);
    addInputMehtodDialog = new AddInputMethodDialog(this);

    LanguageLayout->addWidget(languageTitleLable);
    LanguageLayout->addSpacerItem(new QSpacerItem(8, 7, QSizePolicy::Fixed, QSizePolicy::Fixed));
    LanguageLayout->addWidget(languageGroup);
    LanguageLayout->addWidget(addLanguageBtn);
    languageGroup->setOneRadiusStyle(UkccFrame::BorderRadiusStyle::Top);
    languageGroup->setLastRadiusStyle(UkccFrame::BorderRadiusStyle::None);
    //~ contents_path /Area/System Language
    languageTitleLable->setText(tr("System Language"));
    addLanguageDialog->setHidden(true);
    addInputMehtodDialog->setHidden(true);
}

void AreaUi::addShowLanguage(QString languageCode, QString languageName, bool selected)
{
    bool isAdded = false;
    for (LanguageFrame *f : languageFrameList) {
        if (f->getShowName() == languageName) {
            isAdded = true;
        }
    }
    if (isAdded) {
        return;
    } else {
        LanguageFrame *languageF = new LanguageFrame(languageName, languageGroup);
        languageF->showSelectedIcon(selected);
        languageGroup->addWidget(languageF);
        languageFrameList.append(languageF);
        connect(languageF, &LanguageFrame::clicked, this, [=](){
            for (LanguageFrame *f : languageFrameList) {
                if (f->getShowName() != languageName) {
                    f->showSelectedIcon(false);
                }
            }
            languageF->showSelectedIcon(true);
            Q_EMIT languageChanged(languageCode);
        });
        connect(languageF, &LanguageFrame::deleted, this, [=]() {
            Q_EMIT showLanguageListRemoved(languageCode);
            languageGroup->removeWidget(languageF);
            languageFrameList.removeOne(languageF);
        });
    }
}

void AreaUi::clearShowLanguage()
{
    qDeleteAll(languageFrameList);
    languageFrameList.clear();
}

void AreaUi::reloadLanguageFrameIcon()
{
    for (LanguageFrame *f : languageFrameList) {
        f->reloadIcon();
    }
}

void AreaUi::settingForIntel()
{
    countryWidget->hide();
    dayWidget->hide();
    languageTitleLable->hide();
    addLanguageBtn->hide();
    languageGroup->hide();
}
