package hunan;

import com.game.Util;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import taurus.client.Message;
import taurus.client.TaurusClient;
import taurus.util.CardUtil;
import taurus.util.HongZhongSuanFaTest;

import java.util.ArrayList;
import java.util.List;

public class HuNanFangPaoFa {

    public static int fangPaoFaCardCard = 0;

    //湖南字牌放炮罚 手牌
    private List<Integer> fangPaoFaCardInhand = new ArrayList<Integer>();

    // 玩家座位号
    public static int seat = 0;

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

    public  void fangPaoFaCardInhand() {
        fangPaoFaCardInhand = new ArrayList<Integer>();
    }


    public List<Integer> getFangPaoFaCardInhand() {
        return fangPaoFaCardInhand;
    }

    /**
     * 初始化手牌协议 811
     *
     * @param command 协议号
     * @param message 消息对象
     * @return
     */
    public  String cardInHead(String command, Message message, TaurusClient client) {
        if (command.equalsIgnoreCase("811")) {
            ITObject param = message.param;
            if (param == null) {
                return null;
            }
//            {bank_seat=1, laiziCard=0, laiziCard2=0, laiziCard2Before=0, jing=0, laiziCardBefore=0, card_list=[101, 103, 104, 201, 204, 207, 208, 209, 307, 309, 501, 502, 503]}
            ITArray cardList = param.getTArray("card_list");
            for (int i = 0; i < cardList.size(); i++) {
                fangPaoFaCardInhand.add(cardList.getInt(i));
            }
            if (fangPaoFaCardInhand.size() > 19) {
                outCard(client);
                System.out.println("机器人:" + param.getInt("seat") + "为庄家，需要出牌" + ",牌为：" + fangPaoFaCardInhand.get(0));
            }
            System.out.println("机器人:" + param.getInt("seat") + "放炮罚初始化手牌" + ",牌为：" + fangPaoFaCardInhand.toString());

        }
        return null;
    }

    /**
     * 摸牌协议 819
     *
     * @param command 协议号
     * @param message 消息对象
     * @return
     */
    public  String getCard(String command, Message message) {
        if (command.equalsIgnoreCase("819")) {
            ITObject param = message.param;
            if (param == null) {
                return null;
            }
            System.out.println("轮到座位号:" + param.getInt("seat") + "的用户摸牌" + ",牌为：" + param.getInt("card"));
            if (param.getInt("seat") != null && seat == param.getInt("seat")) {
                fangPaoFaCardInhand.add(param.getInt("card"));
                System.out.println("摸牌后放炮罚 手牌" + fangPaoFaCardInhand);
            }
        }
        return null;
    }

    /**
     * 出牌方法
     */
    public  String outCard(TaurusClient client) {
        ITObject params = TObject.newInstance();
        params.putString("session", session + "," + token);
        params.putInt("card", fangPaoFaCardInhand.get(0));
        System.out.println("放炮罚出牌  " + fangPaoFaCardInhand.get(0));
        fangPaoFaCardInhand.remove(0);
        System.out.println("目前机器人剩余手牌：" + fangPaoFaCardInhand.toString());
        client.send("611", params, response -> {
            System.out.println("出牌成功: " + response.returnCode);
        });
        return null;
    }

    /**
     * 出牌广播协议 812
     *
     * @param command 协议号
     * @param message 消息对象
     * @return
     */
    public static String drawCard(String command, Message message) {
        if (command.equalsIgnoreCase("812")) {
            ITObject param = message.param;
            if (param == null) {
                return null;
            }
            fangPaoFaCardCard = param.getInt("card");
            System.out.println("出牌广播" + fangPaoFaCardCard);
            System.out.println("座位号:" + param.getInt("seat") + "的用户出牌:" + param.getInt("card"));
        }
        return null;
    }

    /**
     * 处理 吃碰 偎提胡
     *
     * @param param 消息参数
     * @return
     */
    public  String actionCard(ITObject param, TaurusClient client) {
        //获取碰杠胡参数 type  和id 后续算法接入，是否能让碰和杠
        ITArray tipList = param.getTArray("tip_list");
        int id = 0;
        int type = 0;
        int opcard = 0;
        int opcard1 = 0;
        int card = 0;
        ITArray bi_list = null;
        ITObject params = TObject.newInstance();
        if (tipList != null && tipList.size() > 0) {
            TObject firstTip = (TObject) tipList.get(0).getObject();
            id = firstTip.getInt("id");
            type = firstTip.getInt("type");
            card = firstTip.getInt("card");
            // 获取吃牌组合中的第一张牌
            opcard = firstTip.getTArray("opcard").getInt(0);
            // 获取吃牌组合中的第二张牌
            opcard1 = firstTip.getTArray("opcard").getInt(1);
            bi_list = firstTip.getTArray("bi_list");
            System.out.println("id ++ " + id);
            System.out.println("type ++ " + type);
            System.out.println("opcard ++ " + opcard);
            System.out.println("card ++ " + card);
            System.out.println("bi_list ++ " + bi_list);
        }

        //执行吃
        if (type == 1) {
            params.putString("session", session + "," + token);
            params.putInt("id", id);
            CardUtil.removeCard1(fangPaoFaCardInhand, opcard, 1);
            CardUtil.removeCard1(fangPaoFaCardInhand, opcard1, 1);
            if (bi_list != null ){
                CardUtil.removeCard1(fangPaoFaCardInhand, card, 1);
            }
            System.out.println("删除吃的牌");
            //执行碰
        } else if (type == 2) {
            params.putString("session", session + "," + token);
            params.putInt("id", id);
            CardUtil.removeCard1(fangPaoFaCardInhand, card, 1);
            System.out.println("删除碰的牌");
            //执行吃杠
        } else if (type == 3) {
            params.putString("session", session + "," + token);
            params.putInt("qi", 0);
            params.putInt("id", 1);
//            Util.removeCard(hongZhongCardInhand, opcard, 3);
            System.out.println("执行吃杠");
        } else if (type == 4) {
            params.putString("session", session + "," + token);
            params.putInt("id", id);
            CardUtil.removeCard1(fangPaoFaCardInhand, card, 2);
            System.out.println("执行偎----");
            // 碰后补杠
        } else if (type == 5) {
//            params.putString("session", session + "," + token);
//            params.putInt("qi", 0);
//            params.putInt("id", 1);
//            Util.removeCard(hongZhongCardInhand, opcard, 1);
//            System.out.println("执行碰后补杠");
        } else if (type == 8) {
            params.putInt("id", id);
        }

        System.out.println("调用612");

        client.send("612", params, response -> {
            System.out.println("操作成功: " + response.returnCode);
        });
        return null;
    }

    public static void fangPaoTipEvent(Message message,TaurusClient client) {
        ITObject param = message.param;
        Integer card = param.getInt("card");
        ITObject params = TObject.newInstance();
        params.putInt("card", card);
        client.send("823", params, response -> {
            System.out.println("操作成功: " + response.returnCode);
        });
    }
}
