package cn.tee3.avd;

/**
 * 用户数据结构.
 *
 * 用户数据定义了用户的基本信息。
 */
public class User {
    public static String AVD_userId_allUser          = "userId2allUser";	 /**< AVD定义的虚假用户Id，用于标识房间中所有用户  */
    public static String AVD_userId_allUserWithoutMe = "userId2withoutMe";   /**< AVD定义的虚假用户Id，用于标识房间中除了自己外的所有用户  */
    /**
     * 用户状态结构.
     *
     * 用户状态定义了用户的各种状态集合信息。
     */
    public static class UserStatus {
        /** 用户状态位 */
        public static final int microphone_has = 1 << 31;           /**< 拥有麦克风设置位  */
        public static final int microphone_on = 1 << 30;            /**< 麦克风打开设置位  */
        public static final int camera_has = 1 << 29;               /**< 拥有摄像头设置位  */
        public static final int camera_on = 1 << 28;                /**< 摄像头打开设置位  */
        public static final int screen_on = 1 << 25;                /**< 屏幕窗口共享设置位  *6/

        /** 用户自定义状态位： 0 -- 9 位 */
        public static final int userinfo_flag = 1023;               /**< 用户自定义状态位  */
        private int status = 0;

        public UserStatus() {
        }

        protected UserStatus(int status) {
            this.status = status;
        }

        /** 判断是否有麦克风设备 */
        public boolean hasMicrophone() {
            return RolePrivilege.hasFlag(status,microphone_has);
        }
        /** 判断是否有摄像头设备 */
        public boolean hasCamera() {
            return RolePrivilege.hasFlag(status,camera_has);
        }
        /** 判断是否麦克风已打开 */
        public boolean isAudioOn() {
            return RolePrivilege.hasFlag(status,microphone_on);
        }
        /** 判断是否摄像头已打开 */
        public boolean isVideoOn() {
            return RolePrivilege.hasFlag(status,camera_on);
        }
        /** 判断是否屏幕窗口共享已开启 */
        public boolean isScreenOn() {
            return RolePrivilege.hasFlag(status,screen_on);
        }
        /** 设置用户状态
         *
         * @param[in] flag 添加的用户状态。
         *
         * @note 添加用户状态是在原有用户状态上添加用户状态；
         * @sa Status
         */
        public void setStatus(int flag) {
            status = RolePrivilege.setFlag(status, flag);
        }
        /** 清理用户状态
         *
         * @param[in] flag 用户状态信息。
         *
         * @note 清理部分用户状态；
         * @sa Status
         */
        public void clearStatus(int flag) {
            status = RolePrivilege.clearFlag(status, flag);
        }
        public int getStatus() { return status; }

        /** 用户自定义状态位
         *
         * @param[in] value 用户自定义数据：10位存储，可以设置为0-1023之间的一个值。
         *
         * @note 设置或清理用户自定义状态位；
         * @sa Status
         */
        public void setInfo(int value) {
            this.status = RolePrivilege.clearFlag(this.status, userinfo_flag);
            this.status = this.status + (value & userinfo_flag);
        }
        public int getInfo() { return this.status & userinfo_flag; }
    }

    ///
	private String	userId = "";                /**< 用户Id，应用层可设置 */
    private UserStatus status;                  /**< 用户状态集合  */
    private String	userName = "";              /**< 用户名称，应用层设置  */
    private String userAgent = "";               /**< 携带应用数据 如 iOS Android web pc ==  */
    private String userData = "";               /**< 用户数据，应用层设置  */

    public User(String userId, String userName, int status, String userData, String userAgent) {
        this.userId = (null == userId) ? "" : userId;
        this.userName = (null == userName) ? "" : userName;
        this.userData = (null == userData) ? "" : userData;
        this.status =  new UserStatus(status);
        this.userAgent = (null == userAgent) ? "" : userAgent;
    }
    protected User(String userId, String userName, int status, String userData) {
        this.userId = (null == userId) ? "" : userId;
        this.userName = (null == userName) ? "" : userName;
        this.userData = (null == userData) ? "" : userData;
        this.status = new UserStatus(status);
        this.userAgent = (null == userAgent) ? "" : userAgent;
    }

    public User(String userId, String userName, String userData) {
        this.userId = (null == userId) ? "" : userId;
        this.userName = (null == userName) ? "" : userName;
        this.userData = (null == userData) ? "" : userData;
        this.status = new UserStatus(0);
        this.userAgent = (null == userAgent) ? "" : userAgent;
    }

    public String getUserId() { return this.userId; }
    public String getUserName() { return this.userName; }
    public void setUserName(String name) {
        this.userName = (null == name) ? "" : name;
    }
    public String getUserData() { return this.userData; }
    public void setUserData(String data) {
        this.userData = (null == data) ? "" : data;
    }
    public UserStatus getStatus() { return status; }

    void setUserAgent(String userAgent) { this.userAgent = userAgent; }
    String getUserAgent() { return userAgent; }

    @Override
    public boolean equals(Object obj) {
        User objd = (User) obj;
        if (null == objd) {
            return false;
        }
        return objd.getUserId().equals(this.getUserId());
    }
    @Override
    public String toString() {
        return "User: id=" + this.userId + ",status="+this.status + ",name="+this.userName + "userAgent=" + this.userAgent;
    }
}
