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 com.kingdee.eas.custom.integration.base.util.AppSecretDecryptor;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 单个应用的 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;

    /**
     * 用户映射字段
     */
    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(AppSecretDecryptor decryptor, Map<String, String> item) {

        String userMappingField = item.get("userMappingField");
        String mobileRedirectType = item.get("mobileRedirectType");
        String desktopRedirectType = item.get("desktopRedirectType");

        this.platform = item.get("platform");
        this.appType = item.get("appType");
        this.appId = item.get("appId");
        this.appSecret = decryptor.decrypt(item.get("appSecret"));
        this.callbackUrl = item.get("callbackUrl");
        this.authEntry = item.get("authEntry");
        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 = Boolean.TRUE.equals(item.get("isPushBusinessMessage"));
        this.isPrimaryApp = Boolean.TRUE.equals(item.get("isPrimaryApp"));
        this.mBosEid = item.get("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 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 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 + "'}";
    }
}
