package com.evt.mgr.handler;

import java.util.ArrayList;

import com.data.cache.AccountCache;
import com.data.util.ConsumeCode;
import com.data.util.Utility;
import com.evt.mgr.EventController;
import com.evt.mgr.IHandler;
import com.taurus.core.entity.ITObject;
import com.taurus.core.plugin.redis.Redis;

import redis.clients.jedis.Jedis;

public class HandlerPay implements IHandler{
	
	int reason;
	boolean isPay;
	public HandlerPay(int reason,boolean isPay) {
		this.reason = reason;
		this.isPay = isPay;
	}
	
	@Override
	public int process(int uid, ITObject param) throws Exception {
		int pay = param.getInt("pay");
		if(pay==0) {
			return 0;
		}
		int result  = 1;
		int gameId =param.getInt("game");
		int groupId = 0;
		int pid = 0;
		if(param.containsKey("group")) {
			groupId = param.getInt("group");
		}
		if(param.containsKey("pid")) {
			pid = param.getInt("pid");
		}
		
		if(reason == ConsumeCode.DIAMO_JOIN_ROOM) {
			pay = Math.abs(pay);
		}
		if(reason == ConsumeCode.DIAMO_REFUND) {
			pay = -Math.abs(pay);
		}
		if(reason == ConsumeCode.DIAMO_ADD) {
			pay = -pay;
		}
		
		if(isPay) {
			int cur_diamo = param.getInt("diamo");
			EventController.payDiamo(uid, pay, cur_diamo, reason, gameId, groupId, pid);
			result = 0;
		}else {
			String session = AccountCache.genKey(uid);
			Jedis jedis0 = Redis.use("group1_db0").getJedis();
			try {
				ArrayList<Long> result_list = Utility.payDiamo(jedis0, session,pay);
				if(result_list!=null) {
					result = result_list.get(0).intValue();
					if(result ==0) {
						long cur_diamo = result_list.get(1);
						EventController.payDiamo(uid, pay, (int)cur_diamo, reason, gameId, groupId, pid);
					}
					result = 0;
				}
			}finally {
				jedis0.close();
			}
		}
		
		return result;
	}

	@Override
	public int processGroup(int groupId, ITObject param) throws Exception {

		return 0;
	}

}
