/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2018-11-28
 * 时间: 16:26
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Runtime.InteropServices;
using MrWu.Debug;

namespace zyxAdapter
{
    /// <summary>
    /// dephi 中介者
    /// </summary>
    public sealed class DephiMediatorPattern : IMediatorPattern
    {
        /// <summary>
        /// 方法
        /// </summary>
        private FunctionRecord funs;

        private IParasitismUtil parasitism_;

        public IParasitismUtil parasitism {
            get {
                return parasitism_;
            }
            set {
                if (parasitism_ == null)
                    parasitism_ = value;
            }
        }

        public DephiMediatorPattern(IParasitismUtil parasitism)
        {
            this.parasitism_ = parasitism;
        }

        #region IHostUtil  c# 调用 dephi 函数

        [DllImport("hjhaServer.dll", EntryPoint = "Init")]
        static extern int Init_(
            [MarshalAs(UnmanagedType.LPStr)]
            string exepath,
            [MarshalAs(UnmanagedType.LPStr)]
            string exename,
            int gameid, ref FunctionRecord func);

        int IHostUtil.Init(string exepath, string exename, int gameid)
        {
            funs = new FunctionRecord(this);
            return Init_(exepath, exename, gameid, ref funs);
        }

        [DllImport("hjhaServer.dll", EntryPoint = "FreeDll")]
        static extern int FreeDll_();

        void IHostUtil.FreeDll()
        {
            FreeDll_();
        }

        [DllImport("hjhaServer.dll", EntryPoint = "CreateTing")]
        static extern void CreateTing_(int tingCnt, int defaultDeskCnt);

        void IHostUtil.CreateTing(int tingid, int defaultDeskCnt)
        {
            CreateTing_(tingid, defaultDeskCnt);
        }

        [DllImport("hjhaServer.dll", EntryPoint = "CreateDesk")]
        static extern void CreateDesk_(int tingid, int deskid);

        void IHostUtil.CreateDesk(int tingid, int deskid)
        {
            CreateDesk_(tingid, deskid);
        }

        [DllImport("hjhaServer.dll", EntryPoint = "DestroyDesk")]
        static extern void DestroyDesk_(int tingid, int deskid);

        void IHostUtil.DestroyDesk(int tingid, int deskid)
        {
            DestroyDesk_(tingid, deskid);
        }

        [DllImport("hjhaServer.dll", EntryPoint = "StartWar")]
        static extern void StartWar_(int tingid, int deskid);

        void IHostUtil.StartWar(int tingid, int deskid)
        {
            StartWar_(tingid, deskid);
        }

        [DllImport("hjhaServer.dll", EntryPoint = "Reconnect")]
        static extern void Reconnect_(int tingid, int deskid, int plid);

        void IHostUtil.Reconnect(int tingid, int deskid, int plid)
        {
            Reconnect_(tingid, deskid, plid);
        }

        [DllImport("hjhaServer.dll", EntryPoint = "Dopack")]
        static extern void Dopack_(int tingid, int deskid,int seat,
                                   IntPtr intPtr,
                                   int length);

        void IHostUtil.Dopack(int tingid, int deskid, int seat,IntPtr intPtr, int length)
        {
            Dopack_(tingid, deskid, seat,intPtr,length);
        }

        [DllImport("hjhaServer.dll", EntryPoint = "DoTimer")]
        static extern void DoTimer_(int tingid, int deskid, int terval);

        void IHostUtil.DoTimer(int tingid, int deskid, int interval)
        {
            DoTimer_(tingid, deskid, interval);
        }

        [DllImport("hjhaServer.dll", EntryPoint = "LoadMem")]
        static extern void LoadMem_(int tingid, int deskid, IntPtr ptr,int len,bool isOld);

        void IHostUtil.LoadMem(int tingid, int deskid, byte[] data,bool isOld)
        {
            IntPtr ptr = Marshal.AllocHGlobal(data.Length);
            try
            {
                Marshal.Copy(data, 0, ptr, data.Length);
                LoadMem_(tingid, deskid, ptr, data.Length, isOld);
            }
            catch (Exception e)
            {
                Debug.Error($"LoadMem 报错:{e.ToString()}");
            }finally
            {
                Marshal.FreeHGlobal(ptr);
            }
        }

        [DllImport("hjhaServer.dll", EntryPoint = "csharpdozyxfun")]
        static extern int doZyxFun_(int tingid, int deskid,
            [MarshalAs(UnmanagedType.LPStr)]string what,
            [MarshalAs(UnmanagedType.LPStr)]string param
            );

        /// <summary>
        /// 调用子游戏方法
        /// </summary>
        /// <param name="tingid"></param>
        /// <param name="deskid"></param>
        /// <param name="what"></param>
        /// <param name="param"></param>
        int IHostUtil.doZyxFun(int tingid, int deskid, string what, string param)
        {
            return doZyxFun_(tingid, deskid, what, param);
        }

        [DllImport("hjhaServer.dll", EntryPoint = "DoTest")]
        static extern void DoTest_(int data, int len);

        void IHostUtil.DoTest(int data, int len)
        {
            DoTest_(data, len);
        }

        #endregion

        #region IParaistismUtil  dephi调用函数  c# 宿主程序实现

        int IParasitismUtil.Logs(
            [MarshalAs(UnmanagedType.LPStr)]
            string msg, int level)
        {
            return parasitism.Logs(msg, level);

        }

        int IParasitismUtil.GetPlayerInt(int tingid,int deskid,int seat, int lx)
        {
            try
            {
                return parasitism.GetPlayerInt(tingid,deskid,seat, lx);
            }
            catch (Exception e)
            {
                Debug.Error("GetPlayerInt is error:" + e.ToString());
                throw e;
            }
        }

        [return: MarshalAs(UnmanagedType.LPStr)]
        string IParasitismUtil.GetPlayerStr(int tingid,int deskid,int seat, int lx)
        {
            try
            {
                return parasitism.GetPlayerStr(tingid,deskid, seat, lx);
            }
            catch (Exception e)
            {
                Debug.Error("GetPlayerStr is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.GetDeskInt(int tingid, int deskid, int lx)
        {
            try
            {
                return parasitism.GetDeskInt(tingid, deskid, lx);
            }
            catch (Exception e)
            {
                Debug.Error("GetDeskInt is error:" + e.ToString());
                throw e;
            }

        }

        [return: MarshalAs(UnmanagedType.LPStr)]
        string IParasitismUtil.GetDeskStr(int tingid, int deskid, int lx)
        {
            try
            {
                return parasitism.GetDeskStr(tingid, deskid, lx);
            }
            catch (Exception e)
            {
                Debug.Error("GetDeskStr is error:" + e.ToString());
                throw e;
            }
        }

        [return: MarshalAs(UnmanagedType.LPStr)]
        string IParasitismUtil.GetTingStr(int tingid, int lx)
        {
            try
            {
                return parasitism.GetTingStr(tingid, lx);
            }
            catch (Exception e)
            {
                Debug.Error("GetTingStr is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.GetTingInt(int tingid, int lx)
        {
            try
            {
                return parasitism.GetTingInt(tingid, lx);
            }
            catch (Exception e)
            {
                Debug.Error("GetTingInt is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.GetRoomInt(int roomid, int lx)
        {
            try
            {
                return parasitism.GetRoomInt(roomid, lx);
            }
            catch (Exception e)
            {
                Debug.Error("GetRoomInt is error:" + e.ToString());
                throw e;
            }
        }

        [return: MarshalAs(UnmanagedType.LPStr)]
        string IParasitismUtil.GetRoomStr(int roomid, int lx)
        {
            try
            {
                return parasitism.GetRoomStr(roomid, lx);
            }
            catch (Exception e)
            {
                Debug.Error("GetRoomStr is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.GetSeatid(int deskid, int id, int seat)
        {
            try
            {
                return parasitism.GetSeatid(deskid, id, seat);
            }
            catch (Exception e)
            {
                Debug.Error("GetSeatid is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.SetPlayer(int tingid,int deskid,int seat, int lx, int num,
                                      [MarshalAs(UnmanagedType.LPStr)]
                                      string str)
        {
            try
            {
                return parasitism.SetPlayer(tingid,deskid, seat, lx, num, str);
            }
            catch (Exception e)
            {
                Debug.Error("SetPlayer is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.SendPack(int tingid, int deskid, int seat,
                     IntPtr intPtr,
                     int length)
        {
            try
            {
                return parasitism.SendPack(tingid, deskid, seat,intPtr, length);
            }
            catch (Exception e)
            {
                Debug.Error("SendPack is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.WarOver(int tingid, int deskid)
        {
            try
            {
                return parasitism.WarOver(tingid, deskid);
            }
            catch (Exception e)
            {
                Debug.Error("WarOver is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.GetGameInt(
            [MarshalAs(UnmanagedType.LPStr)]
            string lx)
        {
            try
            {
                return parasitism.GetGameInt(lx);
            }
            catch (Exception e)
            {
                Debug.Error("GetGameInt is error:" + e.ToString());
                throw e;
            }
        }

        [return: MarshalAs(UnmanagedType.LPStr)]
        string IParasitismUtil.GetGameStr(
            [MarshalAs(UnmanagedType.LPStr)]
            string lx)
        {
            try
            {
                return parasitism.GetGameStr(lx);
            }
            catch (Exception e)
            {
                Debug.Error("GetGameStr is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.IsKaiZhan(int tingid, int deskid)
        {
            try
            {
                return parasitism.IsKaiZhan(tingid, deskid);
            }
            catch (Exception e)
            {
                Debug.Error("IsKaiZhan is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.SaveMem(int tingid, int deskid, IntPtr ptr,int len)
        {
            try
            {
                return parasitism.SaveMem(tingid, deskid, ptr,len);
            }
            catch (Exception e)
            {
                Debug.Error("SaveMem is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.SaveHuiFang(int tingid, int deskid, int lx, string jsondata)
        {
            try
            {
                return parasitism.SaveHuiFang(tingid, deskid, lx, jsondata);
            }
            catch (Exception e)
            {
                Debug.Error("SaveHuiFang is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.LoadHuiFang(int tingid, int deskid)
        {
            try
            {
                return parasitism.LoadHuiFang(tingid, deskid);
            }
            catch (Exception e)
            {
                Debug.Error("LoadHuiFang is error:" + e.ToString());
                throw e;
            }
        }

        int IParasitismUtil.DoZyxFun(string dowhat, string param, ref tr_rltpchar rlt)
        {
            try
            {
                return parasitism.DoZyxFun(dowhat, param, ref rlt);
            }
            catch (Exception e)
            {
                Debug.Error("DoZyxFun is error:" + e.ToString());
                throw e;
            }
        }

        /// <summary>
        /// 加载额外的台桌数据,子游戏自己保存的
        /// </summary>
        /// <param name="tingid"></param>
        /// <param name="deskid"></param>
        /// <returns></returns>
        string IParasitismUtil.Load_taidata(int tingid, int deskid)
        {
            try
            {
                return parasitism.Load_taidata(tingid, deskid);
            }
            catch (Exception e)
            {
                Debug.Error("Load_taidata is error:" + e.ToString());
                throw e;
            }
        }

        /// <summary>
        /// 保存额外的台桌数据,
        /// </summary>
        /// <param name="tingid"></param>
        /// <param name="deskid"></param>
        /// <param name="base64data"></param>
        /// <returns></returns>
        int IParasitismUtil.Save_taidata(int tingid, int deskid, string base64data)
        {
            try
            {
                return parasitism.Save_taidata(tingid, deskid, base64data);
            }
            catch (Exception e)
            {
                Debug.Error("Save_taidata is error:" + e.ToString());
                throw e;
            }
        }

        #endregion

    }
}
