package hunan;

import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import taurus.client.TaurusClient;
import taurus.util.*;

import java.util.*;

public class HuNanPaoDeKuai {

    public static int seat = 0;

    //座位号 剩余牌数量
    private  Map<Integer, List<Integer>> seatRemainHistory = new HashMap<>();

    // 跑的快玩家手牌
    private  List<CardObj> paoDekuaiCardInhand = new ArrayList<>();

    // 会话标识
    public static String session = "";
    // 访问令牌
    public static String token = "";

    //跑的快别人出的牌
    private  ITObject card_list = TObject.newInstance();

    public static int guangboseat = 0;

    public static int remain = 0;

    public Map<Integer, List<Integer>> getSeatRemainHistory() {
        return seatRemainHistory;
    }

    public List<CardObj> getPaoDekuaiCardInhand() {
        return paoDekuaiCardInhand;
    }

    public ITObject getCard_list() {
        return card_list;
    }

    /**
     * 出牌方法
     */
    public  String outCard(TaurusClient client) {
        ITArray itArray = null;
        itArray = test.intelligentPaoDeKuaiOutCard(paoDekuaiCardInhand, card_list, seatRemainHistory);
        System.out.println("itArray-----"  + itArray);
        if (remain == 1 && itArray == null) { //如果玩家的下家只有一张牌，玩家出单张必须是最大的
            CardObj maxSingleCard = CardUtil.findMaxSingleCard(paoDekuaiCardInhand);
            System.out.println("出的最大牌  " + maxSingleCard);
            itArray = CardUtil.toTArray1(maxSingleCard);
        }

        //跑得快出牌
        System.out.println("机器人出牌 " + seat + CardUtil.toList(itArray));
        ITObject params = TObject.newInstance();
        params.putString("session", session + "," + token);
        params.putTArray("card", itArray);
        params.putTArray("all_card", CardUtil.toTArray(paoDekuaiCardInhand));
        CardUtil.removeCard(paoDekuaiCardInhand, CardUtil.toList(itArray)); //删除手牌里打过的牌
        System.out.println("目前机器人剩余手牌：" + paoDekuaiCardInhand.toString());
        client.send("1013", params, response ->

        {
            System.out.println("出牌成功: " + response.returnCode);
        });
        return null;
    }

    /**
     * 跑得快初始化手牌
     *
     * @param param 消息参数
     * @return
     */
    public  String paoDeKuaiCardInHead(ITObject param, TaurusClient client) {
        ITArray cardList = param.getTArray("cards");
        List<CardObj> cardObjs = new ArrayList<>();
        if (cardList.size() != 0) {
            cardObjs = CardUtil.toList(cardList);
        }
        paoDekuaiCardInhand.addAll(cardObjs);
//        if (paoDekuaiCardInhand.size() > 14) {
//            outCard(client);
//            System.out.println("机器人:" + param.getInt("seat") + "为庄家，需要出牌" + ",牌为：" + paoDekuaiCardInhand.get(0));
//        }
        System.out.println("机器人:" + param.getInt("seat") + "初始化手牌" + ",牌为：" + paoDekuaiCardInhand.toString());
        return null;
    }

    /**
     * 跑的快出牌广播
     *
     * @param param 消息参数
     * @return
     */
    public  String paoDekuaiChupaiGuangBo(ITObject param) {
        card_list = param.getTObject("card_obj");
        guangboseat = param.getInt("player");
        remain = param.getInt("remain"); //剩余牌数量
        saveRemainHistory(guangboseat, remain);
        System.out.println("座位号和手牌数量记录  " + seatRemainHistory);
        System.out.println("广播手牌数量" + "座位号  " + guangboseat + "手牌数量  " + remain);
        System.out.println("座位号:" + param.getInt("player") + "的用户出牌广播:" + param.getTObject("card_obj"));
        return null;
    }

    public  void saveRemainHistory(int guangboseat, int remain) {
        if (!seatRemainHistory.containsKey(guangboseat)) {
            seatRemainHistory.put(guangboseat, new ArrayList<>());
        }
        seatRemainHistory.get(guangboseat).add(remain);
    }


}
