package com.x.base.core.project.server;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.x.base.core.DefaultCharset;
import com.x.base.core.gson.GsonPropertyObject;
import com.x.base.core.gson.XGsonBuilder;
import com.x.base.core.role.RoleDefinition;

public class Token {

	private static final String surfix = "o2platform";

	private static final String icon_initialManager = "[encoded data, 13222 characters]\u003d\u003d";

	public static final String defaultInitialManager = "xadmin";

	public static final String initPassword = "o2";

	public static Token defaultInstance() {
		Token o = new Token();
		return o;
	}

	/** 加密用的key,用于加密口令 */
	private String key;

	/** 管理员密码,用于管理员登录,内部服务器口令以及http加密 */
	private String password;

	/** 用于单点登录的啥来着?干啥也不知道 */
	private String sso;

	/** 初始管理员名称 */
	private String initialManager;

	/* ssl密码 */
	private String ssl;

	public String getKey() {
		return key + surfix;
	}

	public String getCipher() {
		return this.getPassword() + surfix;
	}

	public String getPassword() {
		return Objects.toString(this.password, initPassword);
	}

	public String getInitialManager() {
		return Objects.toString(this.initialManager, defaultInitialManager);
	}

	public String getSso() {
		return Objects.toString(this.sso, initPassword);
	}

	public void setKey(String key) {
		this.key = key;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSsl() {
		return ssl;
	}

	public void setSsl(String ssl) {
		this.ssl = ssl;
	}

	public void setSso(String sso) {
		this.sso = sso;
	}

	public void setInitialManager(String initialManager) {
		this.initialManager = initialManager;
	}

	public boolean isInitialManager(String name) {
		return StringUtils.equals(this.getInitialManager(), name);
	}

	public void save() throws Exception {
		File file = new File(Config.base(), Config.PATH_CONFIG_TOKEN);
		FileUtils.write(file, XGsonBuilder.toJson(this), DefaultCharset.charset);
	}

	public InitialManager initialManagerInstance() {
		InitialManager o = new InitialManager();
		String name = this.getInitialManager();
		o.name = name;
		o.id = name;
		o.employee = name;
		o.display = name;
		o.mail = name + "@o2oa.io";
		o.weixin = "";
		o.qq = "";
		o.weibo = "";
		o.mobile = "";
		o.icon = icon_initialManager;
		o.roleList = new ArrayList<String>();
		o.roleList.add(RoleDefinition.CompanyCreator);
		o.roleList.add(RoleDefinition.GroupCreator);
		o.roleList.add(RoleDefinition.Manager);
		o.roleList.add(RoleDefinition.MeetingManager);
		o.roleList.add(RoleDefinition.PersonManager);
		o.roleList.add(RoleDefinition.ProcessPlatformCreator);
		o.roleList.add(RoleDefinition.ProcessPlatformManager);
		return o;
	}

	public class InitialManager extends GsonPropertyObject {
		private String name;
		private String employee;
		private String display;
		private String mail;
		private String weixin;
		private String qq;
		private String weibo;
		private String mobile;
		private String id;
		private String icon;
		private List<String> roleList;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getEmployee() {
			return employee;
		}

		public void setEmployee(String employee) {
			this.employee = employee;
		}

		public String getDisplay() {
			return display;
		}

		public void setDisplay(String display) {
			this.display = display;
		}

		public String getMail() {
			return mail;
		}

		public void setMail(String mail) {
			this.mail = mail;
		}

		public String getWeixin() {
			return weixin;
		}

		public void setWeixin(String weixin) {
			this.weixin = weixin;
		}

		public String getQq() {
			return qq;
		}

		public void setQq(String qq) {
			this.qq = qq;
		}

		public String getWeibo() {
			return weibo;
		}

		public void setWeibo(String weibo) {
			this.weibo = weibo;
		}

		public String getMobile() {
			return mobile;
		}

		public void setMobile(String mobile) {
			this.mobile = mobile;
		}

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public List<String> getRoleList() {
			return roleList;
		}

		public void setRoleList(List<String> roleList) {
			this.roleList = roleList;
		}

		public String getIcon() {
			return icon;
		}

		public void setIcon(String icon) {
			this.icon = icon;
		}

	}

}