#include "areaui.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 TitleLabel(this);
    formatGroup      = new SettingGroup(this);
    //~ contents_path /Area/Regional Format
    countryWidget    = new ComboxWidget(tr("Regional Format"), formatGroup, UkccFrame::BorderRadiusStyle::Top);
    //~ contents_path /Area/Calendar
    calendarWidget   = new ComboxWidget(tr("Calendar"), formatGroup, UkccFrame::BorderRadiusStyle::None);
    //~ contents_path /Area/First Day Of Week
    dayWidget        = new ComboxWidget(tr("First Day Of Week"), formatGroup, UkccFrame::BorderRadiusStyle::None);
    //~ contents_path /Area/Date
#ifdef Nile
    shortDateWidget  = new ComboxWidget(tr("Short Foramt Date"), formatGroup, UkccFrame::BorderRadiusStyle::None);
    longDateWidget   = new ComboxWidget(tr("Long Format Date"), formatGroup, UkccFrame::BorderRadiusStyle::None);
#else
    shortDateWidget  = new ComboxWidget(tr("Date"), formatGroup, UkccFrame::BorderRadiusStyle::None);
#endif

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

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

    if (UkccCommon::isOpenkylinNile()) {
        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 FixLabel(tr("Language Format Example"), m_formatDateFrame);
        m_formatDateNameLabel->setFixedSize(214, 59);

        m_formatDateValueLabel = new FixLabel(m_formatDateFrame);

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

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

void AreaUi::initSystemLanguage()
{
    languageTitleLable   = new TitleLabel(this);
    languageGroup        = new SettingGroup(this);
    addLanguageBtn       = new AddButton(this, UkccFrame::BorderRadiusStyle::Bottom);
    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();
}
