/*
 * Copyright (C) 2018 Tianjin KYLIN Information Technology 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 3, 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, see <http://www.gnu.org/licenses/>.
 *
**/
#ifndef AUTHDIALOG_H
#define AUTHDIALOG_H

#ifndef QT_NO_KEYWORDS
#define QT_NO_KEYWORDS
#endif

#include <QWidget>
#include <QTimer>
#include <QMap>
#include "auth-pam.h"
#include "types.h"
#include "users.h"
#include "biometricdeviceinfo.h"
#include "pam-tally.h"
#include "uniauthservice.h"

namespace Ui {
class AuthDialog;
}

class QLabel;
class KLabel;
class QPushButton;
class Configuration;
class IconEdit;
class Auth;
class BiometricProxy;
class PamTally;
class LoginOptionsWidget;

enum FOCUS {
    REMOVE = 0,
    IN_LIGIN,
    BIO_RIGHT,
    BIO_LEFT,
    IN_LINEEDIT,
    ON_MESSAGEBTN,
};

extern float scale;
class AuthDialog : public QWidget
{
    Q_OBJECT

public:
    explicit AuthDialog(const UserItem &user, QWidget *parent = 0);
    void resizeEvent(QResizeEvent *event);
    void closeEvent(QCloseEvent *event);
    void setUserOfAuth();
    void setX11Focus();
    void setFocusin(int target);
    void setClick();
    void checkPassword();
    int getBioNum();
    bool getLineeditStatus();
private:
    void initUI();
    void startWaiting();
    void stopWaiting();
    void clearMessage();
    void performBiometricAuth();
    void skipBiometricAuth();
    void initBiometricWidget();
    void setChildrenGeometry();
    void setBiometricWidgetGeometry();
    void startBioAuth(unsigned uTimeout = 1000);
    void show_authenticated (bool successful = true);
    void setLoginTypeTip(QString strLoginTypeTip);
    void setUkeyTypeTip(QString text);
    void updatePixmap();
    void startLoadingUkey();
    void stopLoadingUkey();
    void updateLoadingPixmap();
    QPixmap makeRoundLogo(QString logo, int wsize, int hsize, int radius);

private Q_SLOTS:
    void onShowMessage(const QString &message, Auth::MessageType type);
    void onShowPrompt(const QString &prompt, Auth::PromptType type);
    void onAuthComplete();
    void onRespond(const QString &text);
    void onRespondUkey(const QString &text);
//    void onBioAuthStart();
//    void onBioAuthStop();
//    void setBioMovieImage();
//    void updateIcon();
    void onDeviceChanged(unsigned uCurLoginOptType, const DeviceInfoPtr &deviceInfo, bool keyNavigation);
    void onBiometricAuthComplete(bool result, int nStatus);
    void onBiometricButtonClicked();
    void onPasswordButtonClicked();
    void onOtherDevicesButtonClicked();
    void onRetryButtonClicked();
    void pamBioSuccess();
    void onMessageButtonClicked();
    void switchLoginOptType(unsigned uLoginOptType);
    void onLoginOptsCount(unsigned uCount, bool is_bioBtn);
    void onLoginOptImage(QImage img);
    void setLoginMsg(QString strLoginMsg);
    void setQRCode(QImage& imgQRCode);
    void setFaceImg(QImage& imgFace, int nStatus = 0);
    void onM_passwordEditClicked();
    void setQRCodeMsg(QString strMsg);
    void onBiometricDbusChanged(bool bActive);
    void onLoadingImage();

public Q_SLOTS:
//    void switchToBiometric();
//    void switchToPassword();
//    void switchToDevices();
//    void onCapsLockChanged();
    void startAuth();
    void stopAuth();

Q_SIGNALS:
    void authenticateCompete(bool result);
    void clickPassword(bool clicked);
    void loginOptionClicked();
    void showMessageBtn(bool is_show);
private:
    UserItem            user;
    Auth                *auth;

    enum AuthMode { PASSWORD, BIOMETRIC, UNKNOWN };

    AuthMode            authMode;
    Configuration       *configuration;

    // biometric auth
    QString                 m_deviceName;
    DeviceInfoPtr           m_deviceInfo = nullptr;
    BiometricProxy          *m_biometricProxy;
    QWidget                 *m_buttonsWidget;
    QPushButton             *m_biometricButton;
    QPushButton             *m_passwordButton;
    QPushButton             *m_otherDeviceButton;
    QPushButton             *m_retryButton;

    LoginOptionsWidget      *m_widgetLoginOpts = nullptr;

    // UI
//    QPushButton     *m_backButton;         //返回用户列表
    QWidget         *m_userWidget;         //放置用户信息Label
    QLabel          *m_labelHeadImg = nullptr;         //头像
    KLabel          *m_nameLabel;          //用户名
//    QLabel          *m_isLoginLabel;       //提示是否已登录

    QWidget         *m_passwdWidget;        //放置密码输入框和信息列表
    IconEdit        *m_passwordEdit;       //密码输入框
    KLabel          *m_messageLabel;         //PAM消息显示
    QPushButton     *m_messageButton;
    bool            is_showMessageBtn = false;

    QWidget         *m_ukeyPasswdWidget = nullptr;        //放置密码输入框和信息列表
    IconEdit        *m_ukeyPasswordEdit = nullptr;       //密码输入框
    KLabel          *m_ukeyMessageLabel = nullptr;         //PAM消息显示

    QWidget         *m_loadingWidget = nullptr;
    QPushButton     *m_loadingButton = nullptr;
    KLabel          *m_loadingText = nullptr;
    QTimer          *m_loadingTimer = nullptr;
    QPixmap         m_loadingPixmap;

    QLabel          *m_labelFace = nullptr;
    KLabel          *m_labelLoginTypeTip = nullptr;    // 登录类型提示
    QLabel          *m_labelQRCode = nullptr;   // 二维码图标
    KLabel          *m_labelQRCodeMsg = nullptr; // 二维码状态消息提示
    QLabel          *m_labelQRCodeTip = nullptr;

    bool            isBioPassed;        // 生物认证是否成功
    int             maxFailedTimes;
    bool            isHiddenSwitchButton;
    QMap<int,int>   m_failedTimes;
    QTimer          *m_bioTimer;
    bool            isLockingFlg;   //判断当前是否正在锁定倒计时
    int             m_nCurLockMin;  //当前锁定的分钟数
    bool prompted = false;
    bool unacknowledged_messages = false;
    bool direct_login = false;
    bool isretry = true;
    QString m_preStrMessage = "";
    int m_preStrMessageType = -1;

    void root_unlock_countdown();
    void unlock_countdown();
    QTimer *m_timer;

    QImage m_imgQRCode;
    unsigned m_uCurLoginOptType = LOGINOPT_TYPE_PASSWORD;   // 当前登录验证方式
    QString m_strLoginTypeTip = "";
    int m_nLastDeviceId = -1;

    QPixmap         m_waitingPixmap;
    QTimer          *w_timer;
    UniAuthService  *m_uniauthService = nullptr;
    double             curFontSize = 0;
    double             m_ptToPx = 1.0;
    bool	    isLoadingUkey = false;
};

#endif // AUTHDIALOG_H
