package cn.tee3.avd;

/**
 * Created by Administrator on 2015/8/26.
 */
public class RolePrivilege {
    private static final String TAG = "RolePrivilege";
    // define privileges
    public static final int privilege_room_grantprivilege = 1 << 31;   // grant privileges
    public static final int privilege_chat_publicsmessage = 1 << 0;
    public static final int privilege_chat_privatemessage = 1 << 1;
    public static final int privilege_audio_openmicrophone = 1 << 4;
    public static final int privilege_audio_openremotemicrophone = 1 << 5;
    public static final int privilege_video_subscriblecamera  = 1 << 8;
    public static final int privilege_video_publishcamera  = 1 << 9;
    public static final int privilege_video_publishremotecamera  = 1 << 10;
    public static final int privilege_video_setpublishcapbility  = 1 << 11;
    public static final int privilege_screen_publishscreen  = 1 << 16;
    // room
    public static final int privilege_room_updateappdata  = 1 << 24;
    public static final int privilege_room_closeroom  = 1 << 25;
    public static final int privilege_room_updateroomstatus  = 1 << 26; // such as lock, unlock
    public static final int privilege_room_kickoutuser  = 1 << 27;
    public static final int privilege_room_grantrole  = 1 << 27;
    public static final int privilege_room_updateroleprivilege  = 1 << 29;

    //
    public static int setFlag(int value, int flag) { value |= flag; return value; }
    public static int clearFlag(int value, int flag) { value &= ~flag; return value; }
    public static boolean hasFlag(int value, int flag) { return 0 != (value & flag); }
    ///
    // role-privilege
    public int setDefaultRole(String role) {
        Tlog.i(TAG, "setDefaultRole,role:" + role);
        return nativesetDefaultRole(role);
    }
    public String getDefaultRole() {
        return nativegetDefaultRole();
    }
    public int updateRolePrivileges(String role, int privileges) {
        Tlog.i(TAG, "updateRolePrivileges,role:" + role+",privileges:"+privileges);
        return nativesetRolePrivilege(role, privileges);
    }
    public int getRolePrivileges(String role) {
        return nativegetRolePrivilege(role);
    }
    // role
    public int updateUserRole(String userId, String role) {
        Tlog.i(TAG, "updateUserRole,userId:"+userId+",role:" + role);
        return  nativeupdateUserRole(userId, role);
    }
    public int updateUserPrivileges(String userId, int privileges) {
        Tlog.i(TAG, "updateUserRole,userId:"+userId+",privileges:" + privileges);
        return  nativeupdateUserPrivileges(userId, privileges);
    }
    // user
    private String role;
    private int privileges;
    public boolean isRole(String value) {
        return this.role.equals(value);
    }
    public boolean hasPrivilege(int privilege) {
        return hasFlag(this.privileges, privilege);
    }
    //callback
    public static interface Listener {
        public void onRolePrivilegesNotify(String role, int privileges);   // change privileges of a role
        //
        public void onUserRoleNotify(String userId, String role);           // change role of a user
        public void onUserPrivilegesNotify(String userId, int privileges);  // change privileges of a user
    }
    ///
    private native int nativesetDefaultRole(String role);
    private native String nativegetDefaultRole();
    private native int nativesetRolePrivilege(String role, int privileges);
    private native int nativegetRolePrivilege(String role);
    private native int nativeupdateUserRole(String userId, String role);
    private native int nativeupdateUserPrivileges(String userId, int privileges);
}
