package com.mgr.group;

import com.mgr.group.data.Group;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import com.taurus.core.util.Logger;

import redis.clients.jedis.JedisPubSub;

public class GroupSubscriber extends JedisPubSub {
	
	public static final String CHANNEL_NAME = "mgr_group";
	
	private static final String CMD_UPDATE_ROOM="update_room";
	private static final String CMD_DEL_ROOM="del_room";
	private static final String CMD_ADD_ROOM="add_room";
	private static final String CMD_DEL_PLAY="del_play";
	private static final String CMD_ADD_PLAY="add_play";
	private static final String CMD_UPDATE_PLAY="update_play";
	private static final String CMD_DEL_GROUP="del_group";
	private static final String CMD_UPDATE_GROUP="update_group";
	
	
	private static final String CMD_UPDATE_JOINS="update_joins";
	private static final String CMD_UPDATE_MEMBER="update_member";
	private static final String CMD_UPDATE_MAIL_TIP = "update_mail_tip";
	
	private Logger log;
	
	public GroupSubscriber() {
		log = Logger.getLogger(this.getClass());
	}
	
	private void updateRoomEvt(Group group,ITObject param) {
		String roomid = param.getUtfString("roomid");
		Global.groupCtr.updateRoom(group,roomid);
	}
	
	private void addRoomEvt(Group group,ITObject param) {
		String roomid = param.getUtfString("roomid");
		Global.groupCtr.addRoom(group, roomid);
	}
	
	private void delRoomEvt(Group group,ITObject param) {
		String roomid = param.getUtfString("roomid");
		Global.groupCtr.delRoom(group, roomid);
	}
	
	private void addPlay(Group group,ITObject param) {
		int pid = param.getInt("pid");
		Global.groupCtr.addPlay(group, pid);
	}
	
	private void updatePlay(Group group,ITObject param) {
		int pid = param.getInt("pid");
		Global.groupCtr.updatePlay(group, pid);
	}
	
	private void delPlay(Group group,ITObject param) {
		int pid = param.getInt("pid");
		Global.groupCtr.delPlay(group, pid);
	}
	
	private void updateGroup(Group group,ITObject param) {
		String name = param.getUtfString("name");
		int ban = param.getBoolean("ban")?1:0;
		String notice = param.getUtfString("notice");
		int option = param.getInt("option");
		int showNum = param.getInt("show_num");
		Global.groupCtr.updateGroup(group, name, ban,notice,option,showNum);
	}
	
	private void updateJoins(Group group,ITObject param) {
		int joins = param.getInt("joins");
		Global.groupCtr.updateJoins(group, joins);
	}
	
	private void updateMailTip(Group group,ITObject param) {
		int uid = param.getInt("uid");
		Global.groupCtr.updateMailTip(group, uid);
	}
//	private void updateMember(Group group,ITObject param) {
//		int uid = param.getInt("uid");
//		int type = param.getInt("type");
//		int value = param.getInt("value");
//		Global.groupCtr.updateMember(group, uid, type, value);
//	}
	

	
	
	public void onMessage(String channel, String message) {
		if(channel.equals(CHANNEL_NAME)) {
			if(Global.loggerDebug) {
				log.info(message);
			}
			try {
				ITObject data = TObject.newFromJsonData(message);
				final int groupId = data.getInt("gid");
				Group group = Global.groupMap.get(groupId);
				if(group!=null) {
					group.enqueueRunnable(new Runnable() {
						@Override
						public void run() {
							String cmd = data.getUtfString("cmd");
							Group group = Global.groupMap.get(groupId);
							switch(cmd) {
							case CMD_UPDATE_ROOM:
								updateRoomEvt(group,data);
								break;
							case CMD_DEL_ROOM:
								delRoomEvt(group,data);
								break;
							case CMD_ADD_ROOM:
								addRoomEvt(group,data);
								break;
							case CMD_DEL_GROUP:
								group.destroy();
								break;
							case CMD_UPDATE_GROUP:
								updateGroup(group,data);
								break;
							case CMD_ADD_PLAY:
								addPlay(group,data);
								break;
							case CMD_UPDATE_PLAY:
								updatePlay(group, data);
								break;
							case CMD_DEL_PLAY:
								delPlay(group,data);
								break;
							case CMD_UPDATE_JOINS:
								updateJoins(group,data);
								break;
							case CMD_UPDATE_MEMBER:
//								updateMember(group,data);
								break;
							case CMD_UPDATE_MAIL_TIP:
								updateMailTip(group,data);
								break;
							}
						}
					});
				}
			}catch (Exception e) {
				log.error(e);
			}
		}
    }

    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("subscribe redis channel success, channel %s, subscribedChannels %d", 
                channel, subscribedChannels));
    }

    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("unsubscribe redis channel, channel %s, subscribedChannels %d", 
                channel, subscribedChannels));

    }
}
