package com.uas.account.entity;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 应用
 * 
 * @author yingp
 *
 */
public class App implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final String COPY_ALL = "all";
	public static final String COPY_APP_SPLIT = ",";
	public static final String ENCRY_PARAM_PASSWORD = "$password";
	public static final String ENCRY_PARAM_SALT = "$salt";

	private String uid;

	private String publicKey;

	private String description;

	private String userControl;

	private String backSpaceUrl;

	private String backUserUrl;

	private String backUserPwdUrl;

	private String backPartnerUrl;

	private String loginUrl;

	private String logoutUrl;

	private String encryFormat;

	private String copyApps;

	private String scope;

	private Integer defaultUse;

	private int usCount;

	private Integer dialectEnable;

	private String homePage;

	private Integer personalEnable;

	private String backChangeAdminUrl;

	/**
	 * 唯一标识
	 */
	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	/**
	 * 描述
	 */
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * App的企业注册接口（这个接口里面一定不要又调用账户中心注册企业接口）
	 * 
	 * <pre>
	 * App1往账户中心注册账号时，指定了要同时注册到App2，此时账户中心会调用App2的这个接口；
	 * </pre>
	 * 
	 */
	public String getBackSpaceUrl() {
		return backSpaceUrl;
	}

	public void setBackSpaceUrl(String backSpaceUrl) {
		this.backSpaceUrl = backSpaceUrl;
	}

	/**
	 * App的用户注册接口（这个接口里面一定不要又调用账户中心用户注册接口）
	 * 
	 * <pre>
	 * App1往账户中心注册账号时，指定了要同时注册到App2，此时账户中心会调用App2的这个接口；
	 * 需要注意的是，这个接口接收的用户信息，密码字段是<strong>已加密的</strong>
	 * </pre>
	 * 
	 */
	public String getBackUserUrl() {
		return backUserUrl;
	}

	public void setBackUserUrl(String backUserUrl) {
		this.backUserUrl = backUserUrl;
	}

	public String getBackUserPwdUrl() {
		return backUserPwdUrl;
	}

	public void setBackUserPwdUrl(String backUserPwdUrl) {
		this.backUserPwdUrl = backUserPwdUrl;
	}

	public String getBackPartnerUrl() {
		return backPartnerUrl;
	}

	public void setBackPartnerUrl(String backPartnerUrl) {
		this.backPartnerUrl = backPartnerUrl;
	}

	/**
	 * 登录其他应用
	 * @return
	 */
	public String getLoginUrl() {
		return loginUrl;
	}

	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}

	/**
	 * 登出其他应用
	 * @return
	 */
	public String getLogoutUrl() {
		return logoutUrl;
	}

	public void setLogoutUrl(String logoutUrl) {
		this.logoutUrl = logoutUrl;
	}

	/**
	 * 密码加密格式，每个应用的格式允许不一样
	 */
	public String getEncryFormat() {
		return encryFormat;
	}

	public void setEncryFormat(String encryFormat) {
		this.encryFormat = encryFormat;
	}

	@Override
	public String toString() {
		try {
			return "uid=" + uid + "&description=" + description + "&backSpaceUrl="
					+ URLEncoder.encode(backSpaceUrl, "UTF-8") + "&backUserUrl="
					+ URLEncoder.encode(backUserUrl, "UTF-8") + "&backUserPwdUrl="
					+ URLEncoder.encode(backUserPwdUrl, "UTF-8") + "&backPartnerUrl="
					+ URLEncoder.encode(backChangeAdminUrl, "UTF-8") + "&backChangeAdminUrl="
					+ URLEncoder.encode(backPartnerUrl, "UTF-8") + "&encryFormat=" + encryFormat + "&copyApps="
					+ copyApps;
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}

	/**
	 * 关联的应用
	 * <p>
	 * 在用户集、用户信息发生改变时，会同步到关联应用
	 * </p>
	 * 
	 * @return
	 */
	public String getCopyApps() {
		return copyApps;
	}

	public void setCopyApps(String copyApps) {
		this.copyApps = copyApps;
	}

	/**
	 * 公钥
	 * 
	 * <pre>
	 * 用于跨域验证；
	 * 当App将私钥加密的密文传递到账户中心时，账户中心要用该App的公钥验证合法性
	 * </pre>
	 * 
	 * @return
	 */
	public String getPublicKey() {
		return publicKey;
	}

	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}

	/**
	 * 绑定的UserSpace个数
	 * 
	 * @return
	 */
	public int getUsCount() {
		return usCount;
	}

	public void setUsCount(int usCount) {
		this.usCount = usCount;
	}

	/**
	 * 某些应用没有独立企业及用户集，而是借用其他应用的；不为空时，登录该应用，将会使用{@code userControl}的企业及用户来验证<br>
	 * 此时{@code backSpaceUrl}、{@code backUserUrl}、{@code encryFormat}和
	 * {@code copyApps}不用填写
	 * 
	 * @return
	 */
	public String getUserControl() {
		return userControl;
	}

	public void setUserControl(String userControl) {
		this.userControl = userControl;
	}

	/**
	 * 作用域，内网private，外网public，受限protected
	 * 
	 * @return
	 */
	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	/**
	 * 默认注册
	 * 
	 * <pre>
	 * 企业注册的时候，默认注册并开通该应用
	 * </pre>
	 * 
	 * @return
	 */
	public Integer getDefaultUse() {
		return defaultUse;
	}

	public void setDefaultUse(Integer defaultUse) {
		this.defaultUse = defaultUse;
	}

	/**
	 * 允许dialect方式（登录、修改等操作）
	 * 
	 * @return
	 */
	public Integer getDialectEnable() {
		return dialectEnable;
	}

	public void setDialectEnable(Integer dialectEnable) {
		this.dialectEnable = dialectEnable;
	}

	/**
	 * 应用首页， 支持格式如：http://$domain/saas.ubtob.com <br>
	 * 其中，{@code domain}表示企业注册域名
	 * 
	 * @return
	 */
	public String getHomePage() {
		return homePage;
	}

	public void setHomePage(String homePage) {
		this.homePage = homePage;
	}

	public Integer getPersonalEnable() {
		return personalEnable;
	}

	/**
	 * 是否允许个人注册
	 * @param personalEnable
	 */
	public void setPersonalEnable(Integer personalEnable) {
		this.personalEnable = personalEnable;
	}


	public String getBackChangeAdminUrl() {
		return backChangeAdminUrl;
	}

	public void setBackChangeAdminUrl(String backChangeAdminUrl) {
		this.backChangeAdminUrl = backChangeAdminUrl;
	}

	public enum Scope {
		PRIVATE, PUBLIC, PROTECTED
	}

}
