package cn.tee3.avd;

import android.text.TextUtils;

import javax.annotation.Nullable;

/**
 * 房间模块接口类
 *
 * 房间模块接口定义了房间模块公共的操作。
 */
public class Module {
    private static final String TAG = "Module";
    protected static enum Type
    {
        usermanager,
        chat,
        audio,
        video,
        screen,
        localrecord,
        svrlive,
		MWhiteboard,
    };

    private Room room;
    protected long nativeobj;
    protected Module(Room room, Type type, long nativeobj) {
        this.type = type;
        this.room = room;
        this.nativeobj = nativeobj;
    }
    private Type type;
    protected Type getType() {
        return type;
    }
    @Nullable protected static Module createModule(Room room, Type type) {
        Module newmod = null;
        switch (type) {
            case usermanager: newmod = new MUserManager(room); break;
            case chat: newmod = new MChat(room); break;
            case audio: newmod = new MAudio(room); break;
            case video: newmod = new MVideo(room); break;
            case screen: newmod = new MScreen(room); break;
            case localrecord: newmod = new MLocalRecord(room); break;
            case svrlive: newmod = null; break;
			case MWhiteboard: newmod = new MWhiteboard(room); break;
            default:
                break;
        }
        if (null != newmod) {
            room.addModule(newmod);
        }
        return newmod;
    }
    // need imp in subclass
    protected void dispose() {
    }

    /** 获取房间接口指针
     *
     * @return 返回房间接口指针。
     * @sa IRoom
     */
    public Room getRoom() {
        return room;
    }
    /** 获取房间用户管理模块指针
     *
     * @return 返回用户管理模块指针。
     * @sa IMUserManager
     */
    @Nullable public MUserManager getUserManager() {
        return (MUserManager)room.getModule(Type.usermanager);
    }
    /** 获取设备Id的关联用户Id
     * @param[in] deviceId 设备Id
     * @return 返回用户Id。
     */
    @Nullable public String getOwnerId(String deviceId)  {
        String ownerId = "";

        if (Type.usermanager != this.getType()) {
            ownerId = nativegetOwnerId(deviceId);
        } else {
            Module chat = room.getModule(Type.chat);
            ownerId = chat.getOwnerId(deviceId);
        }
        return TextUtils.isEmpty(ownerId) ? "" : ownerId;
    }
 		/** 获取设备Id的关联用户名称
     * @param[in] deviceId 设备Id
     * @return 返回用户名称。
     */
    @Nullable public String getOwnerName(String deviceId)  {
        return getUserName(getOwnerId(deviceId));
    }

    /** 通过用户Id获取用户名称
     * @param[in] userId 用户Id
     * @return 返回用户名称。
     */
    @Nullable public String getUserName(String userId)  {
        User user = getUserManager().getUser(userId);
        if (null == user) {
            return null;
        }
        return user.getUserName();
    }
    /** 判断是否是自己的用户Id
     * @param[in] userId 用户Id
     * @return 返回是否是自己。
     */
    public boolean isSelfUser(String userId) {
        if (null == userId) {
            return false;
        }
        return userId.equals(getUserManager().getSelfUserId());
    }
    /** 判断设备Id是否是属于自己的
     * @param[in] deviceId 设备Id
     * @return 返回设备Id是否是属于自己的。
     */
    public boolean isSelfDevice(String deviceId) {
        if (null == deviceId) {
            return false;
        }
        String  auserId = getOwnerId(deviceId);
        return isSelfUser(auserId);
    }

    private native String nativegetOwnerId(String deviceId);
}
