ChainsProxyPage::ChainsProxyPage(QWidget *parent) : ContentWidget(parent)
{
    setTitle(tr("Application Proxy"));

    m_proxyType = new NextPageWidget;
    m_proxyType->setTitle(tr("Proxy Type"));

    m_addr = new LineEditWidget;
    m_addr->setTitle(tr("IP address"));
    m_addr->setPlaceholderText(tr("Required"));

    m_port = new LineEditWidget;
    m_port->setTitle(tr("Port"));
    m_port->setPlaceholderText(tr("Required"));

    m_username = new LineEditWidget;
    m_username->setTitle(tr("Username"));
    m_username->setPlaceholderText(tr("Optional"));

    m_password = new LineEditWidget;
    m_password->setTitle(tr("Password"));
    m_password->setPlaceholderText(tr("Optional"));
    m_password->textEdit()->setEchoMode(QLineEdit::Password);

    SettingsGroup *grp = new SettingsGroup;
    grp->appendItem(m_proxyType);
    grp->appendItem(m_addr);
    grp->appendItem(m_port);
    grp->appendItem(m_username);
    grp->appendItem(m_password);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(10);

    layout->addSpacing(20);
    layout->addWidget(grp);

    QLabel *tip = new QLabel;
    tip->setText(tr("Check \"Use a proxy\" in application context menu in Launcher after configured"));
    tip->setWordWrap(true);
    layout->addWidget(tip);

    ButtonTuple *btns = new ButtonTuple;
    btns->leftButton()->setText(tr("Cancel"));
    btns->rightButton()->setText(tr("OK"));

    layout->addWidget(btns);

    layout->addStretch();

    TranslucentFrame *w = new TranslucentFrame;
    w->setLayout(layout);

    setContent(w);

    connect(btns->leftButton(), &QPushButton::clicked, this, &ChainsProxyPage::back);
    connect(btns->rightButton(), &QPushButton::clicked, this, &ChainsProxyPage::onCheckValue);
    connect(m_proxyType, &NextPageWidget::clicked, this, &ChainsProxyPage::requestShowTypePage);
}
void VpnPage::createVPNSession()
{
//    emit requestCreateConnection("vpn", "/");

    if (!m_vpnTypePage)
    {
        OptionItem *l2tp = new OptionItem;
        l2tp->setContentsMargins(20, 0, 10, 0);
        l2tp->setTitle(tr("L2TP"));
        OptionItem *pptp = new OptionItem;
        pptp->setContentsMargins(20, 0, 10, 0);
        pptp->setTitle(tr("PPTP"));
        OptionItem *vpnc = new OptionItem;
        vpnc->setContentsMargins(20, 0, 10, 0);
        vpnc->setTitle(tr("VPNC"));
        OptionItem *openvpn = new OptionItem;
        openvpn->setContentsMargins(20, 0, 10, 0);
        openvpn->setTitle(tr("OpenVPN"));
        OptionItem *strongswan = new OptionItem;
        strongswan->setContentsMargins(20, 0, 10, 0);
        strongswan->setTitle(tr("StrongSwan"));
        OptionItem *openconnect = new OptionItem;
        openconnect->setContentsMargins(20, 0, 10, 0);
        openconnect->setTitle(tr("OpenConnect"));

        connect(l2tp, &OptionItem::selectedChanged, [=] { createVPN("vpn-l2tp"); });
        connect(pptp, &OptionItem::selectedChanged, [=] { createVPN("vpn-pptp"); });
        connect(vpnc, &OptionItem::selectedChanged, [=] { createVPN("vpn-vpnc"); });
        connect(openvpn, &OptionItem::selectedChanged, [=] { createVPN("vpn-openvpn"); });
        connect(strongswan, &OptionItem::selectedChanged, [=] { createVPN("vpn-strongswan"); });
        connect(openconnect, &OptionItem::selectedChanged, [=] { createVPN("vpn-openconnect"); });

        TranslucentFrame *widget = new TranslucentFrame;
        QVBoxLayout *mainLayout = new QVBoxLayout(widget);
        mainLayout->setMargin(0);

        SettingsGroup *grp = new SettingsGroup;
        grp->appendItem(l2tp);
        grp->appendItem(pptp);
        grp->appendItem(vpnc);
        grp->appendItem(openvpn);
        grp->appendItem(strongswan);
        grp->appendItem(openconnect);

        mainLayout->addSpacing(10);
        mainLayout->addWidget(grp);

        m_vpnTypePage = new ContentWidget;
        m_vpnTypePage->setTitle(tr("New VPN"));
        m_vpnTypePage->setContent(widget);

        connect(m_vpnTypePage, &ContentWidget::back, [=] { m_vpnTypePage = nullptr; });
    }

    emit requestNextPage(m_vpnTypePage);
}
ModifyFullNamePage::ModifyFullNamePage(User *u, QWidget *parent)
    : ContentWidget(parent),

      m_user(u)
{
    m_fullnameWidget = new LineEditWidget;
    m_fullnameWidget->setTitle(tr("Fullname:"));
    m_fullnameWidget->textEdit()->setText(m_user->fullname());

    QPushButton *confirmBtn = new QPushButton;
    confirmBtn->setText(tr("OK"));

    SettingsGroup *grp = new SettingsGroup;
    grp->appendItem(m_fullnameWidget);

    QVBoxLayout *centralLayout = new QVBoxLayout;
    centralLayout->addWidget(grp);
    centralLayout->addWidget(confirmBtn);
    centralLayout->setSpacing(10);
    centralLayout->setContentsMargins(0, 10, 0, 0);

    QWidget *centralWidget = new TranslucentFrame;
    centralWidget->setLayout(centralLayout);

    setContent(centralWidget);
    setTitle(tr("Fullname Settings"));

    connect(m_user, &User::fullnameChanged, this, &ModifyFullNamePage::onFullnameChanged);
    connect(confirmBtn, &QPushButton::clicked, this, [=] { emit requestSetFullname(m_user, m_fullnameWidget->textEdit()->text()); });
}
VpnPage::VpnPage(QWidget *parent)
    : ContentWidget(parent),

      m_vpnSwitch(new SwitchWidget),
      m_vpnGroup(new SettingsGroup),
      m_vpnTypePage(nullptr)
{
    m_vpnSwitch->setTitle(tr("VPN Status"));

    QPushButton *createVpnBtn = new QPushButton;
    createVpnBtn->setText(tr("Create VPN"));

    QPushButton *importVpnBtn = new QPushButton;
    importVpnBtn->setText(tr("Import VPN"));

    SettingsGroup *switchGrp = new SettingsGroup;
    switchGrp->appendItem(m_vpnSwitch);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addSpacing(10);
    mainLayout->addWidget(switchGrp);
    mainLayout->addWidget(m_vpnGroup);
    mainLayout->addWidget(createVpnBtn);
    mainLayout->addWidget(importVpnBtn);
    mainLayout->setSpacing(10);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addSpacing(10);

    QWidget *mainWidget = new TranslucentFrame;
    mainWidget->setLayout(mainLayout);

    setContent(mainWidget);
    setTitle(tr("VPN"));

    connect(m_vpnSwitch, &SwitchWidget::checkedChanged, this, &VpnPage::requestVpnEnabled);
    connect(createVpnBtn, &QPushButton::clicked, this, &VpnPage::createVPNSession);
    connect(importVpnBtn, &QPushButton::clicked, this, &VpnPage::importVPN);
}
PalmDetectSetting::PalmDetectSetting(QWidget *parent)
    : TranslucentFrame(parent)
    , m_detectSwitchBtn(new SwitchWidget(tr("Palm Detect"), this))
    , m_contactSlider(new TitledSliderItem(tr("Minimum contact surface"), this))
    , m_pressureSlider(new TitledSliderItem(tr("Minimum pressure value"), this))
{
    QStringList contactList;
    for (int i(1); i <= 10; ++i) {
        contactList << QString::number(i);
    }

    // NOTE(kirigaya): The contact is only between 1 and 10.
    DCCSlider *contactSlider = m_contactSlider->slider();
    contactSlider->setType(DCCSlider::Vernier);
    contactSlider->setTickPosition(QSlider::TicksBelow);
    contactSlider->setRange(1, 10);
    contactSlider->setTickInterval(1);
    contactSlider->setPageStep(1);

    m_contactSlider->setAnnotations(contactList);

    // NOTE(kirigaya): The range of pressure is between 100 and 200
    DCCSlider *pressureSlider = m_pressureSlider->slider();
    pressureSlider->setType(DCCSlider::Vernier);
    pressureSlider->setTickPosition(QSlider::TicksBelow);
    pressureSlider->setRange(100, 200);
    pressureSlider->setTickInterval(20);
    pressureSlider->setPageStep(20);

    QStringList pressureList;

    int i = 100;
    while (i <= 200) {
        pressureList << QString::number(i);
        i += 20;
    }

    m_pressureSlider->setAnnotations(pressureList);

    SettingsGroup *detectSwitchGrp = new SettingsGroup;
    detectSwitchGrp->appendItem(m_detectSwitchBtn);

    TipsLabel *tip = new TipsLabel(tr("Please disable the option if touchpad doesn't work after enabled"), this);
    tip->setWordWrap(true);
    tip->setContentsMargins(16, 5, 10, 5);

    SettingsGroup *sliderGrp = new SettingsGroup;

    sliderGrp->appendItem(m_contactSlider);
    sliderGrp->appendItem(m_pressureSlider);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);

    layout->addWidget(detectSwitchGrp);
    layout->addWidget(tip);
    layout->addWidget(sliderGrp);

    setLayout(layout);

    connect(m_detectSwitchBtn, &SwitchWidget::checkedChanged, this, &PalmDetectSetting::requestDetectState);
    connect(contactSlider, &DCCSlider::valueChanged, this, &PalmDetectSetting::requestContact);
    connect(pressureSlider, &DCCSlider::valueChanged, this, &PalmDetectSetting::requestPressure);
}