package robot.mj;

import com.robot.GameController;
import com.taurus.core.annotation.ActionKey;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import com.taurus.core.util.Logger;
import robot.handler.HuNanChangShaHandler;
import taurus.client.Message;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 长沙麻将游戏控制器
 * 处理游戏协议和AI逻辑
 */
public class EXGameController extends GameController {
    
    private static final Logger log = Logger.getLogger(EXGameController.class);
    
    private final HuNanChangShaHandler huNanChangShaHandler = new HuNanChangShaHandler();
    
    //用于存储其他玩家的操作信息
    private final Map<Integer, List<Integer>> playerOutcardsMap = new HashMap<>();
    private final Map<Integer, List<Integer>> playerchisMap = new HashMap<>();
    private final Map<Integer, List<Integer>> playerpengsMap = new HashMap<>();
    private final Map<Integer, List<Integer>> playermingsMap = new HashMap<>();
    private final Map<Integer, List<Integer>> playerzisMap = new HashMap<>();
    
    /**
     * 处理游戏协议 - 811 初始化收手牌
     */
    @ActionKey("811")
    public void handleInitCard(ITObject param, int gid, Object recipient) {
        log.debug("处理协议 811 - 初始化收手牌");
        Message virtualMessage = new Message();
        virtualMessage.command = "811";
        virtualMessage.param = param;
        huNanChangShaHandler.cardInHead("811", virtualMessage, null);
    }
    
    /**
     * 处理游戏协议 - 812 出牌广播
     */
    @ActionKey("812")
    public void handleDiscardBroadcast(ITObject param, int gid, Object recipient) {
        log.debug("处理协议 812 - 出牌广播");
        // 处理其他玩家的操作信息
        ITArray outcard_map = param.getTArray("outcard_map");
        ITArray opchicards = param.getTArray("opchicards");
        ITArray oppengcards = param.getTArray("oppengcards");
        ITArray opmingcards = param.getTArray("opmingcards");
        ITArray opzicards = param.getTArray("opzicards");

        // 清空旧数据，用新数据完全覆盖
        playerOutcardsMap.clear();
        playerchisMap.clear();
        playerpengsMap.clear();
        playermingsMap.clear();
        playerzisMap.clear();
        
        // 出过的牌
        if (outcard_map != null) {
            for (int i = 0; i < outcard_map.size(); i++) {
                ITObject playerData = outcard_map.getTObject(i);
                int playerId = playerData.getInt("playerId");
                ITArray outcardsArray = playerData.getTArray("outcards");

                List<Integer> outcardsList = new ArrayList<>();
                for (int j = 0; j < outcardsArray.size(); j++) {
                    outcardsList.add(outcardsArray.getInt(j));
                }

                // 存储到Map中（覆盖旧数据）
                playerOutcardsMap.put(playerId, outcardsList);
            }
        }

        // 吃的牌
        if (opchicards != null) {
            for (int i = 0; i < opchicards.size(); i++) {
                ITObject playerData = opchicards.getTObject(i);
                int playerId = playerData.getInt("playerId");
                ITArray outchiArray = playerData.getTArray("opchicards");

                List<Integer> outchiList = new ArrayList<>();
                for (int j = 0; j < outchiArray.size(); j++) {
                    outchiList.add(outchiArray.getInt(j));
                }
                playerchisMap.put(playerId, outchiList);
            }
        }

        // 碰的牌
        if (oppengcards != null) {
            for (int i = 0; i < oppengcards.size(); i++) {
                ITObject playerData = oppengcards.getTObject(i);
                int playerId = playerData.getInt("playerId");
                ITArray outpengArray = playerData.getTArray("oppengcards");

                List<Integer> outpengList = new ArrayList<>();
                for (int j = 0; j < outpengArray.size(); j++) {
                    outpengList.add(outpengArray.getInt(j));
                }
                playerpengsMap.put(playerId, outpengList);
            }
        }

        // 明杠的牌
        if (opmingcards != null) {
            for (int i = 0; i < opmingcards.size(); i++) {
                ITObject playerData = opmingcards.getTObject(i);
                int playerId = playerData.getInt("playerId");
                ITArray outmingArray = playerData.getTArray("opmingcards");

                List<Integer> outmingList = new ArrayList<>();
                for (int j = 0; j < outmingArray.size(); j++) {
                    outmingList.add(outmingArray.getInt(j));
                }
                playermingsMap.put(playerId, outmingList);
            }
        }

        // 暗杠的牌
        if (opzicards != null) {
            for (int i = 0; i < opzicards.size(); i++) {
                ITObject playerData = opzicards.getTObject(i);
                int playerId = playerData.getInt("playerId");
                ITArray outziArray = playerData.getTArray("opzicards");

                List<Integer> outziList = new ArrayList<>();
                for (int j = 0; j < outziArray.size(); j++) {
                    outziList.add(outziArray.getInt(j));
                }
                playerzisMap.put(playerId, outziList);
            }
        }
        Message virtualMessage = new Message();
        virtualMessage.command = "812";
        virtualMessage.param = param;
        HuNanChangShaHandler.drawCard("812", virtualMessage);
    }
    
    /**
     * 处理游戏协议 - 813 出牌提示
     */
    @ActionKey("813")
    public void handleDiscardTip(ITObject param, int gid, Object recipient) {
        log.debug("处理协议 813 - 出牌提示");
        huNanChangShaHandler.outCard(null, playerOutcardsMap, playerchisMap, playerpengsMap, playermingsMap, playerzisMap);
    }
    
    /**
     * 处理游戏协议 - 814 放招提示
     */
    @ActionKey("814")
    public void handleActionTip(ITObject param, int gid, Object recipient) {
        log.debug("处理协议 814 - 放招提示");
        huNanChangShaHandler.actionCard(param, null);
    }
    
    /**
     * 处理游戏协议 - 815 服务器通知客户端有玩家执行了操作
     */
    @ActionKey("815")
    public void handlePlayerAction(ITObject param, int gid, Object recipient) {
        log.debug("处理协议 815 - 服务器通知客户端有玩家执行了操作");
        huNanChangShaHandler.shanchuchuguopai(param);
    }
    
    /**
     * 处理游戏协议 - 817 结算
     */
    @ActionKey("817")
    public void handleSettlement(ITObject param, int gid, Object recipient) {
        log.debug("处理协议 817 - 结算");
        // 清理手牌数据
        huNanChangShaHandler.getChangShaCardInhand().clear();
    }
    
    /**
     * 处理游戏协议 - 819 摸牌
     */
    @ActionKey("819")
    public void handleDrawCard(ITObject param, int gid, Object recipient) {
        log.debug("处理协议 819 - 摸牌");
        Message virtualMessage = new Message();
        virtualMessage.command = "819";
        virtualMessage.param = param;
        huNanChangShaHandler.getCard("819", virtualMessage);
    }
    
    /**
     * 处理游戏协议 - 820 换牌提示
     */
    @ActionKey("820")
    public void handleChangePlayer(ITObject param, int gid, Object recipient) {
        log.debug("处理协议 820 - 换牌提示");
        Message virtualMessage = new Message();
        virtualMessage.command = "820";
        virtualMessage.param = param;
        HuNanChangShaHandler.changePlayer("820", virtualMessage);
    }
}