using System;
using nativeCom;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using MrWu.Configuration;
using System.Xml;
using Game.Data;

public class mj_ : mj {

    public static mj_ instance;

    protected virtual int playerCnt {
        get {
            return 4;
        }
    }

    /*
	
	[Conditional("DEBUG")]
	public virtual void hufan_ut(){
		//hpr fangpaonew fanidx pk.zjpos;
		int hpr;
		int fangpaonew;
		int zjpos;
		
		int[] fangpaos = new int[]{
			-1,0,1,2,3,10,11,12,13
		};
		
		for(int i=0;i<playerCnt+1;i++){	//胡牌人
			hpr = i-1;
			for(int j=0;j<playerCnt;j++){	//庄家
				zjpos = j;
				int len = fangpaos.Length;
				for(int k=0;k<len;k++){		//放炮人
					fangpaonew = fangpaos[k];
					tr_pack_dapai pack = new tr_pack_dapai();
					tr_pack_dapai.init(ref pack);
					Tr_faninfo fan = new Tr_faninfo();
					Tr_faninfo.init(ref fan);
					setfanidx();
					int mlen = fanidx.Length;
					
					//calFan_hupai(ref pk,
					//cal_hupai(ref pack,ref fan,hpr,fangpaonew,11,1111,)
				}
			}
		}
	}
	
	//int[count][idx][id]
	
	/// <summary>
	/// 排列组合 根据几个获取数组
	/// </summary>
	public int[][] pailiezuhe_count(int count){
		int[][] duilie = new int[count][];
		
	}
	
	/// <summary>
	/// 以idx开头的数组
	/// </summary>
	/// <param name="now"></param>
	public int[] pailiezuhe_startIdx(int StartIdx,int count){
	
	}
	
	/// <summary>
	/// 获取Startidx开头的第x个
	/// </summary>
	/// <param name="idx"></param>
	/// <param name="StratIdx"></param>
	/// <param name="count"></param>
	/// <returns></returns>
	public int pailiezhuhe_startIdx_idx(int idx,int StratIdx,int count){
	
	}
	*/


    protected MemoryStream gzObj;

    /// <summary>
    /// 解析麻将子游戏规则
    /// </summary>
    /// <param name="now"></param>
    public virtual void setZyxGz(ref tcls_mem_dll_agame now) { }

    /// <summary>
    /// 获取规则
    /// </summary>
    /// <param name="address"></param>
    /// <param name="gz"></param>
    unsafe protected void getGz(int address, out Tr_MJGZ gz) {
        ABlock ab = new ABlock();
        myCom_zyx.Instance.getDataOfStruct<ABlock>(address, ref ab, (int*)&ab);
        IntPtr ptr = new IntPtr((int)ab.pt + 100);
        gz = (Tr_MJGZ)Marshal.PtrToStructure(ptr, typeof(Tr_MJGZ));
    }

    unsafe protected void getGz(int address, ref ABlock ab, out Tr_MJGZ gz) {
        IntPtr ptr = new IntPtr((int)ab.pt + 100);
        gz = (Tr_MJGZ)Marshal.PtrToStructure(ptr, typeof(Tr_MJGZ));
    }

    /// <summary>
    /// 设置规则
    /// </summary>
    /// <param name="now"></param>
    unsafe public override void setGz(ref tcls_mem_dll_agame now, int memoryAddress) {
        MrWu.Debug.Debug.Info("读取规则!");

        //解析通用规则
        if (now.isLibrary == 0) {   //测试取测试的
                                    //now.gz = Form2.fmSub.gzTest;
            log("读取测试规则!");
            return;
        }

        //第一步硬盘读文件
        if (gzObj == null) {
            string gzPath = funs.instance.getGzPath();
            XmlDocument doc = new XmlDocument();
            doc.Load(gzPath);
            Object tmp = (Tr_MJGZ)Configuration.LoadConfiguration(typeof(Tr_MJGZ), doc.DocumentElement);

            //         MrWu.Debug.Debug.Log("反序列化:");
            //Object tmp = baseFuntion.DeSerizeable(gzPath);
            //         MrWu.Debug.Debug.Log("反序列化之后!");
            gzObj = baseFuntion.Serizeable(tmp);
        }



        now.gz = (Tr_MJGZ)baseFuntion.DeSerizeable(gzObj);

        if (now.gzcs_big != null) {

            string stt = "";
            int len = now.gzcs_big.Length;
            for (int i = 0; i < len; i++) {
                stt += now.gzcs_big[i];
            };

            MrWu.Debug.Debug.Info("通用规则:" + stt);

            //牌类型
            now.gz.StyOf_Pai = (sbyte)now.gzcs_big[0];//tyGz[0].charToInt();
                                                      //1表示不能吃  2可以吃
            now.gz.Not_Chi = to0(now.gzcs_big[1], now.gz.Not_Chi);
            //2电脑喜欢杠
            now.gz.Fan_GangPai = to0(now.gzcs_big[2], now.gz.Fan_GangPai);
            //3必须自摸
            if (now.gzcs_big[3] != 0) {
                if (now.gzcs_big[3] == 1)
                    now.gz.StyOf_HuPai = 2;
                else if (now.gzcs_big[3] == 2)
                    now.gz.StyOf_HuPai = 1;
                else
                    now.gz.StyOf_HuPai = 0;
            }

            //4吃就清一色
            if (now.gzcs_big[4] != 0) {
                if (now.gzcs_big[4] == 1)
                    now.gz.chijiuqys = 1;
                else if (now.gzcs_big[4] == 2)
                    now.gz.chijiuqys = 0;
                else
                    now.gz.chijiuqys = 11;
            }


            now.gz.chijiuqys = (sbyte)now.gzcs_big[4];//.charToInt();
                                                      //5听功能
            now.gz.TingStyle_ = getLast(now.gzcs_big[5], now.gz.TingStyle_);// (sbyte)(tyGz[5].charToInt() - 1);
                                                                            //6牌墙蹲高
            now.gz.dungaoCnt = (sbyte)now.gzcs_big[6];//.charToInt();
                                                      //8风可成阙
            now.gz.ChiXi = getLast(now.gzcs_big[8], now.gz.ChiXi);// (sbyte)(tyGz[8].charToInt() - 1);
                                                                  //9箭可成阙
            now.gz.ChiYuan = getLast(now.gzcs_big[9], now.gz.ChiYuan);// (sbyte)(tyGz[9].charToInt() - 1);
                                                                      //10加倍次数
                                                                      //log("通用规则加倍次数:" + now.gz.jiabeiCnt + "," + now.gzcs_big[10]);
            now.gz.jiabeiCnt = Tojiabei(now.gzcs_big[10], now.gz.jiabeiCnt);// tyGz[10].charToInt();

            //11前多少张可以加倍
            now.gz.jiabeichupaicnt = Tojiabei(now.gzcs_big[11], now.gz.jiabeichupaicnt);

            //12听后看牌
            now.gz.tinghoukanpai = getLast(now.gzcs_big[12], now.gz.tinghoukanpai);// (sbyte)(tyGz[12].charToInt() - 1);
        } else {
            log("没有找到通用规则!");
        }


        //解析前25个通用规则
        if (now.gzcs_friend != null) {

            int len = now.gzcs_friend.Length;

            string stt = "";
            for(int i=0;i<len;i++){ 
                stt += now.gzcs_friend[i];
                }

            MrWu.Debug.Debug.Info("朋友房规则:" + stt);

            //德国场
            now.gz.fanjing968[0] = (sbyte)now.gzcs_friend[0];
            //1表示不能吃  2可以吃
            now.gz.Not_Chi = to0(now.gzcs_friend[1], now.gz.Not_Chi);
            //2电脑喜欢杠
            now.gz.Fan_GangPai = to0(now.gzcs_friend[2], now.gz.Fan_GangPai);
            //3必须自摸
            if (now.gzcs_friend[3] != 0) {
                if (now.gzcs_friend[3] == 1)
                    now.gz.StyOf_HuPai = 2;
                else if (now.gzcs_friend[3] == 2)
                    now.gz.StyOf_HuPai = 1;
                else
                    now.gz.StyOf_HuPai = 0;
            }

            //4吃就清一色
            if (now.gzcs_friend[4] != 0) {
                if (now.gzcs_friend[4] == 1)
                    now.gz.chijiuqys = 1;
                else if (now.gzcs_friend[4] == 2)
                    now.gz.chijiuqys = 0;
                else
                    now.gz.chijiuqys = 11;
            }

            //5听功能
            now.gz.TingStyle_ = to0(now.gzcs_friend[5], now.gz.TingStyle_);
            //6无效
            //7*
            //8风成阙
            now.gz.ChiXi = getLast(now.gzcs_friend[8], now.gz.ChiXi);
            //9箭成阙
            now.gz.ChiYuan = getLast(now.gzcs_friend[9], now.gz.ChiYuan);
            //10加倍次数
            now.gz.jiabeiCnt = Tojiabei(now.gzcs_friend[10], now.gz.jiabeiCnt);// tyGz[10].charToInt();
                                                                               //11前多少张可以加倍
            now.gz.jiabeichupaicnt = Tojiabei(now.gzcs_friend[11], now.gz.jiabeichupaicnt);
            //12听后看牌
            now.gz.tinghoukanpai = getLast(now.gzcs_friend[12], now.gz.tinghoukanpai);// (sbyte)(tyGz[12].charToInt()
                                                                                      //13*
                                                                                      //14屁胡
            if (now.gzcs_friend[14] != 0) {
                if (now.gzcs_friend[14] == 1) {
                    now.gz.pingHu_1fan[0] = 0;
                    now.gz.pingHu_1fan[1] = 0;
                } else if (now.gzcs_friend[14] == 2) {
                    now.gz.pingHu_1fan[0] = 4;
                    now.gz.pingHu_1fan[1] = 4;
                } else if (now.gzcs_friend[14] == 3) {
                    now.gz.pingHu_1fan[0] = 4;
                    now.gz.pingHu_1fan[1] = 0;
                }
            }

            //16庄家
            if (now.gzcs_friend[16] != 0) {
                if (now.gzcs_friend[16] == 1) {
                    now.gz.lunzhuang_ = 0;
                } else if (now.gzcs_friend[16] == 2) {
                    now.gz.lunzhuang_ = 1;
                    now.gz.lunzhuang_zjhp = 0;
                } else if (now.gzcs_friend[16] == 3) {
                    now.gz.lunzhuang_ = 1;
                    now.gz.lunzhuang_zjhp = 1;
                }
            }

        } else {
            log("没找到子游戏规则!");
        }

        //解析后25个子游戏规则
        setZyxGz(ref now);

        if (now.gz.fanjing968[0] == 0)
            now.gz.Open_JING = 0;

        ABlock ab1 = new ABlock();
        myCom_zyx.Instance.getDataOfStruct<ABlock>(memoryAddress, ref ab1, (int*)&ab1);

        int gzSize = Marshal.SizeOf(typeof(Tr_MJGZ));

        //log("规则大小:" + gzSize +"自摸方式:" + now.gz.StyOf_HuPai + ",清一色:" + now.gz.chijiuqys);

        byte[] bts = new byte[gzSize];
        byte[] gzdata = funs.instance.StructToBytes(now.gz);
        Buffer.BlockCopy(gzdata, 0, bts, 0, gzSize);
        funs.instance.memToNative_bytes(bts, ref ab1, 100);
    }

    /// <summary>
    /// 0表示不生效 1表示可以  2表示不可以 装换成 0不可以  1可以
    /// </summary>
    protected sbyte to0(byte ch, int org) {
        //sbyte t_sty = ch.charToInt();
        if (ch == 1)
            return (sbyte)ch;
        if (ch == 0)
            return (sbyte)org;
        return 0;
    }

    /// <summary>
    /// 获取上一个 0无效返回原值
    /// </summary>
    /// <returns></returns>
    protected sbyte getLast(byte ch, int org) {
        //sbyte t_sty = ch.charToInt();
        if (ch == 0)
            return (sbyte)org;
        return (sbyte)(ch - 1);
    }

    /// <summary>
    /// 加倍的转换  0无效 1不加  2加几次  转 0不加 >2加几次
    /// </summary>
    /// <returns></returns>
    private sbyte Tojiabei(byte ch, int org) {
        //sbyte t_sty = ch.charToInt();
        if (ch == 0)
            return (sbyte)org;
        if (ch == 1)
            return 0;
        return (sbyte)ch;
    }
}