using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using zyxAdapter;

namespace nativeCom {


    public partial class dcEvent_zyx : dcEvent_zyx0 {
        static dcEvent_zyx _instance;
        public static dcEvent_zyx Instance {
            get {
                if (_instance == null) {
                    _instance = new dcEvent_zyx();


                }
                return _instance;
            }
        }
        public dcEvent_zyx() {
            _instance = this;
            //_instance.init();
        }

        public override void init() {
            base.init();

            //Form2.fmSub.init();
            //baseinfo.instance.dll.fun=doMsg_fun;
            //baseinfo.instance.dll.rukou=doMsg_rukou;
        }



        /// <summary>
        /// 调用gamefun -- 调试用的
        /// </summary>
        /// <param name="dowhat"></param>
        /// <param name="param"></param>
        /// <param name="rlt"></param>
        /// <param name="dataEx"></param>
        /// <param name="result"></param>
        public int TestGameFun(string dowhat, string param, ref tr_rltpchar rlt, ref nativeCom.ABlock dataEx, ref nativeCom.ABlock result) {
            //MrWu.Debug.Debug.Error("GameFun:" + dowhat + "," + param);
            return 0;
        }
    }



    public interface Igamedo {

        void i_do_pack(object obj, int evt);
        void i_do_initMem(bool isfree);
        void i_do_startWar();
        void i_do_timer();
        void i_do_reconect();
        void i_setData(ref tr_rltpchar t);
        //		void i_getData(byte[] bts);
        void i_getData(string str);
    }
    public class myGameClassBase : myGameObj {
        public static string teststrs;
        tr_rltpchar rlt = new tr_rltpchar();
        ABlock ab = new ABlock();
        Igamedo igamedo { get { return this as Igamedo; } }
        public myGameClassBase(ref tr_rltpchar t) {
            t.str = getAddr.ToString();

        }


        //----------------数据--------------
        /// <summary>
        ///座位总数  拿这个循环  中途可能有人先结算
        /// </summary>
        public int seatCnts;
        /// <summary>
        /// 开战中否
        /// </summary>
        public bool isFighting { get; set; }//自己控制，不需要问底层
                                            //			{
                                            //				return baseinfo.instance.exe.gamefun("-1","6*-1*-1*5",  ref rlt,ref ab,ref ab)==1;
                                            //			}
                                            //		}

        //-------------------过程或属性---------------------
        /// </summary>
        /// <param name="获取某座位的玩家ID"></param>
        /// <returns></returns>
        public int getUserId(int seat) {
            return dcEvent_zyx.Instance.TestGameFun("-1", "2*" + seat, ref rlt, ref ab, ref ab);
        }

        /// <summary>
        /// 结算
        /// </summary>
        /// <param name="seat"></param>
        /// <param name="value"></param>
        /// <param name="sifangFinal 私房有效 0增量  1最终"></param>
        public void setFan(int seat, int value, int sifangFinal = 0) {

            int id = getUserId(seat); setFan_id(id, value, sifangFinal);
            //			int lx=2;
            //
            //			if (isSifang)
            //				lx=8+sifangFinal;
            //
            //			baseinfo.instance.exe.gamefun("-1","11*"+id.ToString()+"*"+lx.ToString()+"*"+value.ToString(),	    ref rlt,ref ab,ref ab);

        }

        /// <summary>
        /// 结算
        /// </summary>
        /// <param name="seat"></param>
        /// <param name="value"></param>
        /// <param name="sifangFinal 私房有效 0增量  1最终"></param>
        public void setFan_id(int id, int value, int sifangFinal = 0) {
            //			int id=getUserId(seat);
            int lx = 2;

            if (isSifang)
                lx = 8 + sifangFinal;


            dcEvent_zyx.Instance.TestGameFun("-1", "11*" + id.ToString() + "*" + lx.ToString() + "*" + value.ToString(), ref rlt, ref ab, ref ab);

        }


        //提前结算--》可以离开桌子了
        public void tiqianJieSuan(int seat) {
            int id = getUserId(seat);
            tiqianJieSuan_id(id);
            dcEvent_zyx.Instance.TestGameFun("-1", "11*" + id.ToString() + "*10*0", ref rlt, ref ab, ref ab);
        }
        //提前结算--》可以离开桌子了
        public void tiqianJieSuan_id(int id) {
            //			int id=getUserId(seat);
            dcEvent_zyx.Instance.TestGameFun("-1", "11*" + id.ToString() + "*10*0", ref rlt, ref ab, ref ab);
        }
        public void tiqianXiaZhuo_id(int id) {
            //			int id=getUserId(seat);
            dcEvent_zyx.Instance.TestGameFun("-1", "11*" + id.ToString() + "*11*0", ref rlt, ref ab, ref ab);
        }
        public int warOver() {

            return dcEvent_zyx.Instance.TestGameFun("-1", "10", ref rlt, ref ab, ref ab);
        }
        public bool isGui_id(int id) {
            return dcEvent_zyx.Instance.TestGameFun("-1", "4*" + id.ToString() + "*5", ref rlt, ref ab, ref ab) == 1;
        }
        public bool isTiqianJiesuan_id(int id) {
            return dcEvent_zyx.Instance.TestGameFun("-1", "4*" + id.ToString() + "*20", ref rlt, ref ab, ref ab) == 1;
        }
        public string teststr() {
            int addr = dcEvent_zyx.Instance.TestGameFun("-1", "14*1*2", ref rlt, ref ab, ref ab);//获取pchar地址
            NativeDllMethod.nativefun("2", addr.ToString(), ref ab, ref ab);//pchar装入ab结构体
            return cls_nativeCom.Instance.getUnmanagedString(ref ab);//ab结构体转string
        }
        public bool canPlay(int seat) {
            return !isEmptySeat(seat) && !isTiqianJiesuan_id(getUserId(seat));
        }
        unsafe public int yasuo(byte[] src, byte[] dst, int len, int sty) {

            fixed (byte* p1 = src) {
                fixed (byte* p2 = dst) {
                    //					ABlock sdfsdf;
                    //					sdfsdf.pt=(int*)p1;
                    //					sdfsdf.len=len;
                    //					nativeCom.cls_nativeCom.Instance.

                    return dcEvent_zyx.Instance.TestGameFun("-1", "21*" + sty.ToString() + "*" + ((int)p1).ToString() + "*" + ((int)p2).ToString() + "*" + len.ToString(), ref rlt, ref ab, ref ab);

                }
            }

        }
        unsafe public void sendPack(object obj, int evt, int userId = -1) {


            byte[] data = cls_nativeCom.Instance.Serizeable_obj_withEvt(obj, evt);
            fixed (byte* bt = data) {

                int i = (int)(int*)bt;
                dcEvent_zyx.Instance.TestGameFun("-1", "9*" + userId.ToString() + '*' + i.ToString() + '*' + data.Length,
                                              ref rlt, ref ab, ref ab);

            }
        }



        /// <summary>
        /// 是否socket在线    不是玩家是否登录了游戏
        /// </summary>
        /// <param name="seat"></param>
        /// <returns></returns>
        public bool isOnline(int seat) {
            int id = getUserId(seat);
            MrWu.Debug.Debug.Info("玩家是否在线!!!");
            return false;
            /*
            if (id < 0) return false;
            return baseinfo.instance.exe.gamefun("-1", "4*" + id + "*3", ref rlt, ref ab, ref ab) != 1;
            */
        }
        public bool isOnline_id(int id) {
            //			int id=getUserId(seat);
            if (id < 0) return false;
            return dcEvent_zyx.Instance.TestGameFun("-1", "4*" + id + "*3", ref rlt, ref ab, ref ab) != 1;
        }
        /// <summary>
        /// 是否空座位
        /// </summary>
        /// <param name="seat"></param>
        /// <returns></returns>
        public bool isEmptySeat(int seat) {
            return getUserId(seat) < 0;
        }
        public bool isSifang { get { return dcEvent_zyx.Instance.TestGameFun("-1", "13*6*-1", ref rlt, ref ab, ref ab) != -1; } }

        public int getSifangInfo(int sty) {
            return dcEvent_zyx.Instance.TestGameFun("-1", "13*6*2*" + sty.ToString(), ref rlt, ref ab, ref ab);
        }
        //---------

        public void log(string str, bool isError = false) {
            if(isError)
                MrWu.Debug.Debug.Error(str);
            else
                MrWu.Debug.Debug.Log(str);
        }



        public int getSeatCnts { get { return dcEvent_zyx.Instance.TestGameFun("-1", "6*-1*-1*6", ref rlt, ref ab, ref ab); } }
        //--------
        public void savePaiju() {
            dcEvent_zyx.Instance.TestGameFun("-1", "15", ref rlt, ref ab, ref ab);
        }



    }
}