package com.kingdee.eas.custom.integration.base.config;

import com.kingdee.eas.custom.integration.base.bizEnum.RedirectTypeEnum;
import com.kingdee.eas.custom.integration.base.bizEnum.UserMappingFieldEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * 单个应用的 OAuth 配置实体
 */
public class AppConfig {

    /**
     * 平台标识：dingtalk / feishu
     */
    private String platform;

    /**
     * 应用类型：pa（管理数字）/ ess（员工自助）
     */
    private String appType;

    /**
     * 平台颁发的 AppId
     */
    private String appId;

    /**
     * AppSecret（JSON 中存储密文，AES 加密）
     * 由 AppSecretDecryptor 解密为明文。
     * 一次解密后内存常驻，避免每次使用时重复解密的 IO/CPU 消耗。
     */
    private String appSecret;

    /**
     * 在平台开发者后台注册的 OAuth 回调地址
     */
    private String callbackUrl;

    /**
     * Controller 认证路径，如 /auth/dingtalk
     */
    private String authEntry;

    /**
     * Controller 回调路径，如 /auth/callback/dingtalk/pa
     */
    private String callbackEntry;

    /**
     * Controller 错误页路径，如 /auth/dingtalk/error
     */
    private String errorEntry;

    /**
     * 用户映射字段
     */
    private UserMappingFieldEnum userMappingField;

    // 移动端跳转类型
    private RedirectTypeEnum mobileRedirectType;

    // 桌面端跳转类型
    private RedirectTypeEnum desktopRedirectType;

    // 是否推送业务消息（应用/管理信息卡片）
    private boolean isPushBusinessMessage = false;

    // 是否主应用
    private boolean isPrimaryApp = false;

    /**
     * 移动端 BOS 企业 ID
     */
    private String mBosEid;

    public AppConfig() {
    }

    public AppConfig(String platform, String appType,
                     String appId, String appSecret, String callbackUrl,
                     String authEntry, String callbackEntry, String userMappingField,
                     String mobileRedirectType, String desktopRedirectType,
                     boolean isPushBusinessMessage, boolean isPrimaryApp,
                     String mBosEid) {
        this.platform = platform;
        this.appType = appType;
        this.appId = appId;
        this.appSecret = appSecret;
        this.callbackUrl = callbackUrl;
        this.authEntry = authEntry;
        this.callbackEntry = callbackEntry;
        if (StringUtils.isBlank(userMappingField)) {
            this.userMappingField = UserMappingFieldEnum.PERSON_NUMBER;
        } else {
            userMappingField = userMappingField.toUpperCase();
            //默认工号
            this.userMappingField = UserMappingFieldEnum.getEnum(userMappingField) == null ?
                    UserMappingFieldEnum.PERSON_NUMBER :
                    UserMappingFieldEnum.getEnum(userMappingField);
        }
        if (StringUtils.isBlank(mobileRedirectType)) {
            this.mobileRedirectType = RedirectTypeEnum.TO_MBOS;
        } else {
            mobileRedirectType = mobileRedirectType.toUpperCase();
            //默认跳转移动bos
            this.mobileRedirectType = RedirectTypeEnum.getEnum(mobileRedirectType) == null ?
                    RedirectTypeEnum.TO_MBOS :
                    RedirectTypeEnum.getEnum(mobileRedirectType);
        }
        if (StringUtils.isBlank(desktopRedirectType)) {
            this.desktopRedirectType = RedirectTypeEnum.TO_SHR;
        } else {
            desktopRedirectType = desktopRedirectType.toUpperCase();
            //默认跳转sHR
            this.desktopRedirectType = RedirectTypeEnum.getEnum(desktopRedirectType) == null ?
                    RedirectTypeEnum.TO_SHR :
                    RedirectTypeEnum.getEnum(desktopRedirectType);
        }
        this.isPushBusinessMessage = isPushBusinessMessage;
        this.isPrimaryApp = isPrimaryApp;
        this.mBosEid = mBosEid;
    }

    public String getPlatform() {
        return platform;
    }

    public String getAppType() {
        return appType;
    }

    public String getAppId() {
        return appId;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public String getCallbackUrl() {
        return callbackUrl;
    }

    public String getAuthEntry() {
        return authEntry;
    }

    public String getCallbackEntry() {
        return callbackEntry;
    }

    public String getErrorEntry() {
        return errorEntry;
    }

    public void setPlatform(String platform) {
        this.platform = platform;
    }

    public void setAppType(String appType) {
        this.appType = appType;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public void setCallbackUrl(String callbackUrl) {
        this.callbackUrl = callbackUrl;
    }

    public void setAuthEntry(String authEntry) {
        this.authEntry = authEntry;
    }

    public void setCallbackEntry(String callbackEntry) {
        this.callbackEntry = callbackEntry;
    }

    public void setErrorEntry(String errorEntry) {
        this.errorEntry = errorEntry;
    }

    public UserMappingFieldEnum getUserMappingField() {
        return userMappingField;
    }

    public void setUserMappingField(UserMappingFieldEnum userMappingField) {
        this.userMappingField = userMappingField;
    }

    public RedirectTypeEnum getMobileRedirectType() {
        return mobileRedirectType;
    }

    public void setMobileRedirectType(RedirectTypeEnum mobileRedirectType) {
        this.mobileRedirectType = mobileRedirectType;
    }

    public RedirectTypeEnum getDesktopRedirectType() {
        return desktopRedirectType;
    }

    public void setDesktopRedirectType(RedirectTypeEnum desktopRedirectType) {
        this.desktopRedirectType = desktopRedirectType;
    }

    public boolean isPushBusinessMessage() {
        return isPushBusinessMessage;
    }

    public void setPushBusinessMessage(boolean isPushBusinessMessage) {
        this.isPushBusinessMessage = isPushBusinessMessage;
    }

    public boolean isPrimaryApp() {
        return isPrimaryApp;
    }

    public void setPrimaryApp(boolean isPrimaryApp) {
        this.isPrimaryApp = isPrimaryApp;
    }

    public String getMBosEid() {
        return mBosEid;
    }

    public void setMBosEid(String mBosEid) {
        this.mBosEid = mBosEid;
    }

    @Override
    public String toString() {
        return "AppConfig{platform='" + platform + "', appType='" + appType +
                "', appId='" + appId + "', callbackUrl='" + callbackUrl +
                "', authEntry='" + authEntry + "', userMappingField='" + userMappingField +
                "', mBosEid='" + mBosEid + "'}";
    }
}
