package com.yunfeiyun.agmp.common.framework.web.service;

import com.yunfeiyun.agmp.common.constant.CacheConstants;
import com.yunfeiyun.agmp.common.constant.Constants;
import com.yunfeiyun.agmp.common.core.domain.model.PortalInfo;
import com.yunfeiyun.agmp.common.framework.manager.RedisCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Portal的公共库
 *
 * @author 杨晓辉
 */
@Component
public class PortalService
{
    @Value("${application.name:}")
    private String appName;

    @Value("${portal.ssoEnabled:false}")
    private boolean ssoEnabled;

    @Value("${portal.ssoLoginUrl:}")
    private String ssoLoginUrl;

    @Value("${portal.ssoTokenUrl:}")
    private String ssoTokenUrl;

    @Value("${portal.ssoSessionUrl:}")
    private String ssoSessionUrl;

    @Value("${portal.ssoLogoutUrl:}")
    private String ssoLogoutUrl;

    @Value("${portal.appLogoutUrl:}")
    private String appLogoutUrl;


    @Value("${portal.syncEnabled:false}")
    private boolean syncEnabled;

    @Value("${portal.orgEnabled:true}")
    private boolean orgEnabled;

    @Autowired
    private RedisCacheManager redisCacheManager;


    /**
     * 获取sso的基本信息
     */
    public PortalInfo getSsoInfo()
    {
        PortalInfo portal = new PortalInfo();
        portal.setAppName(appName);
        portal.setSsoEnabled(ssoEnabled ? "1":"0");
        portal.setSsoLoginUrl(!ssoEnabled ? "/index": ssoLoginUrl);
        portal.setSsoTokenUrl(ssoTokenUrl);
        portal.setSsoSessionUrl(ssoSessionUrl);
        portal.setSsoLogoutUrl(ssoLogoutUrl);
        portal.setAppLogoutUrl(appLogoutUrl);
        portal.setSyncEnabled(syncEnabled ? "1":"0");
        portal.setOrgEnabled(orgEnabled ? "1":"0");
        return portal;
    }

    /**
     * 获取portal的主题信息
     */
    public PortalInfo getThemeInfo()
    {
        PortalInfo portal = new PortalInfo();
        portal.setAppName(appName);
        portal.setSsoEnabled(ssoEnabled ? "1":"0");
        portal.setSsoLoginUrl(!ssoEnabled ? "/index": ssoLoginUrl);
        portal.setSsoTokenUrl(ssoTokenUrl);
        portal.setSsoSessionUrl(ssoSessionUrl);
        portal.setSsoLogoutUrl(ssoLogoutUrl);
        portal.setAppLogoutUrl(appLogoutUrl);
        portal.setSyncEnabled(syncEnabled ? "1":"0");

        return portal;
    }


    /**
     *
     */
    public String getAppName()
    {
        return appName;
    }

    public String getClaimsKey()
    {
        return getAppName() + "_" + Constants.LOGIN_USER_KEY;
    }

    public String getTokenKey()
    {
        return getAppName() + "_" + CacheConstants.LOGIN_TOKEN_KEY;
    }

    public boolean getSSoEnabled()
    {
        return ssoEnabled;
    }
    public String getSSoTokenUrl()
    {
        return ssoTokenUrl;
    }
}
