private void InitCoreSbobetEngine()
        {
            if (_sbobetEngine != null && _sbobetEngine.sboAgent.State == AgentState.Running)
            {
                this._sbobetEngine.sboAgent.Logout();
                this.lblSbobetCurrentCredit.Caption = "-";
                this.iNewR.Enabled = false;
            }
            else
            {
                BCCore.BetAccount sboAccount = new BCCore.BetAccount();
                sboAccount.Account = this.config.Sbo.Account;
                sboAccount.Password = this.config.Sbo.Password;
                sboAccount.Website = this.config.Sbo.Website;

                BCCore.Utis.EngineLogger logger = new BCCore.Utis.EngineLogger("log");
                this._sbobetEngine = new SbobetEngine(sboAccount, logger, "Engine2");

                BCCore.LoginStatus loginStatus = this._sbobetEngine.sboAgent.Login();
                if (loginStatus == BCCore.LoginStatus.OK)
                {
                    this.rpgSbobet.Text = "SBO - " + sboAccount.Account;                                        
                    this._sbobetEngine.FullDataCompleted += new EngineDelegate(this._sbobetEngine_FullDataCompleted);
                    this._sbobetEngine.sboAgent.RefreshCredit();
                    this.lblSbobetCurrentCredit.Caption = this._sbobetEngine.sboAgent.Config.Balance.ToString();
                    this.btnStart.Enabled = true;
                    this.iNewR.Enabled = true;
                    this.btnSbobetGetInfo.Caption = "Log out";
                }
            }
        }        
        private TransactionDTO PlaceBet(bool sendOrNOT, MatchDTO ibetMatch, MatchDTO sbobetMatch, eOddType oddType, string ibetOddID, string sbobetOddID, string ibetOddType, string sbobetOddType, string ibetOddValue, string sbobetOddValue, int ibetStake, int sbobetStake, IBetEngine ibetEngine, SbobetEngine sbobetEngine, string sbobetOdd, bool ibetHomeGive)
        {
            this._betting = true;
            TransactionDTO transactionDTO = new TransactionDTO();
            //transactionDTO.OddType = oddType.ToString() + " - " + ibetOddType.Replace("h", "Home").Replace("a", "Away") + " / " + sbobetOddType.Replace("h", "Home").Replace("a", "Away");
            string text = "";
            string text2 = "";
            string text3 = "";
            bool flag = false;
            bool flag2 = false;
            int num = 0;
            int num2 = 0;
            string text4 = "";
            string text5 = "";
            string text6 = "";
            bool flag3 = false;
            bool flag4 = false;
            bool sBOBETReTrade = false;
            int num3 = 0;
            string text7 = "";
            string betCount = "";
            string text8 = "";
            bool flag5 = false;

            float ibetOddValue_ = float.Parse(ibetOddValue);
            float sboOddValue_ = float.Parse(sbobetOddValue);

            if (ibetOddValue_ > 0.84 || (ibetOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = ibetStake * (float)txtAddValue.Value;
                ibetStake = ibetStake + (int)valueAdd;
            }
            if (sboOddValue_ > 0.84 || (sboOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = sbobetStake * (float)txtAddValue.Value;
                sbobetStake = sbobetStake + (int)valueAdd;
            }

            if (MatchDTO.IsSameMatch(ibetMatch.HomeTeamName.ToLower(), sbobetMatch.HomeTeamName.ToLower(), ibetMatch.AwayTeamName.ToLower(), sbobetMatch.AwayTeamName.ToLower()))
            {
                try
                {
                    ibetEngine.PrepareBet(ibetOddID, ibetOddType, ibetOddValue, ibetStake.ToString(), out flag, out num, out num2, out text4, out text2, out text3);
                    sbobetEngine.PrepareBet(sbobetOddID, sbobetOddValue, sbobetOddType, out betCount, out num3, out flag3, out text7, out text5, out text6);
                    if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
                    {
                        if (flag && flag3)
                        {
                            if ((checkEdit13.Checked || checkEdit15.Checked) && sendOrNOT)//gui lenh
                            {
                                var taskA = new Task(() => this.AddOddToLocalCommunity(ibetMatch, sbobetMatch, oddType, sbobetOdd, sbobetOdd, ibetOddType, sbobetOddType, ibetOddValue, sbobetOddValue, ibetHomeGive));
                                taskA.Start();
                            }
                            float num4 = float.Parse(text4);
                            float num5 = float.Parse(text7);
                            if (num4 + num5 == 0f || num4 == num5)
                            {
                                if (ibetStake <= num2 && sbobetStake <= num3)
                                {
                                    try
                                    {                                        
                                        float currentCredit = 0;
                                        float currentCredit2 = 0;
                                        if (checkEdit8.Checked)
                                        {
                                            currentCredit = ibetEngine.GetCurrentCredit();
                                            currentCredit2 = sbobetEngine.GetCurrentCredit();
                                        }
                                        else
                                        {
                                            currentCredit = ibetEngine._currentCredit;
                                            currentCredit2 = sbobetEngine._currentCredit;
                                        }

                                        if ((float)ibetStake <= currentCredit && (float)sbobetStake <= currentCredit2)
                                        {
                                            try
                                            {
                                                
                                                ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
                                                if (flag2)
                                                {
                                                    sbobetEngine.ConfirmBet(sbobetOddID, sbobetOddValue, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out flag4);
                                                    if (!flag4)
                                                    {
                                                        try
                                                        {
                                                            sbobetEngine.PrepareBet(sbobetOddID, text8, sbobetOddType, out betCount, out num3, out flag5, out text7, out text5, out text6);
                                                            if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
                                                            {
                                                                num5 = float.Parse(text7);
                                                                if (num4 + num5 == 0f || num4 == num5)
                                                                {
                                                                    text = "Retrade. IBET: " + ibetOddValue + " -  SBOBET: " + text8;
                                                                    sbobetEngine.ConfirmBet(sbobetOddID, text8, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out sBOBETReTrade);
                                                                    this._lastTransactionTime = System.DateTime.Now;
                                                                }
                                                                else
                                                                {
                                                                    text = "Invalid Odd while Retrade. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                text = string.Concat(new string[]
																{
																	"Not Same Match - Retrade. IBET: ", 
																	text2, 
																	" / ", 
																	text3, 
																	" - SBOBET: ", 
																	text5, 
																	" / ", 
																	text6
																});
                                                            }
                                                        }
                                                        catch (System.Exception ex)
                                                        {
                                                            text = "Error while Retrade. Details: " + ex.Message;
                                                        }
                                                    }
                                                    this._lastTransactionTime = System.DateTime.Now;
                                                    object obj = text;
                                                    text = string.Concat(new object[]
													{
														obj, 
														" - Success Transaction. Half: ", 
														ibetMatch.Half, 
														" - Minute: ", 
														ibetMatch.Minute, 
														" - Halftime: ", 
														ibetMatch.IsHalfTime
													});
                                                }
                                                
                                            }
                                            catch (System.Exception ex)
                                            {
                                                text = "Error while Trading. Details: " + ex.Message;
                                            }
                                        }
                                        else
                                        {
                                            text = string.Concat(new object[]
											{
												"Out of Cash. IBET Credit: ", 
												currentCredit, 
												" -  SBOBET Credit: ", 
												currentCredit2
											});
                                        }
                                    }
                                    catch (System.Exception ex)
                                    {
                                        text = "Error while getting Credit. Details: " + ex.Message;
                                    }
                                }
                                else
                                {
                                    text = string.Concat(new object[]
									{
										"Max Bet. IBET: ", 
										num2, 
										" - SBOBET: ", 
										num3
									});
                                }
                            }
                            else
                            {
                                text = "Invalid Odd while Preparing Ticket. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                            }
                        }
                    }
                    else
                    {
                        text = string.Concat(new string[]
						{
							"Not Same Match - Preparing Ticket. IBET: ", 
							text2, 
							" / ", 
							text3, 
							" - SBOBET: ", 
							text5, 
							" / ", 
							text6
						});
                    }
                }
                catch (System.Exception ex)
                {
                    text = "Error while Preparing Trade. Details: " + ex.Message;
                }
            }
            else
            {
                text = string.Concat(new string[]
				{
					"Not Same Match - Comparing. IBET: ", 
					ibetMatch.HomeTeamName, 
					" / ", 
					ibetMatch.AwayTeamName, 
					" - SBOBET: ", 
					sbobetMatch.HomeTeamName, 
					" / ", 
					sbobetMatch.AwayTeamName
				});
            }
            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName + " / " + sbobetMatch.HomeTeamName;
            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName + " / " + sbobetMatch.AwayTeamName;
            transactionDTO.Odd = text4 + " / " + text7;
            transactionDTO.OddKindValue = text4 + " / " + text7;
            transactionDTO.OddValue = ibetOddValue + " / " + sbobetOddValue;
            transactionDTO.Stake = ibetStake + " / " + sbobetStake;
            transactionDTO.IBETAllow = flag;
            transactionDTO.IBETTrade = flag2;
            transactionDTO.IBETReTrade = false;
            transactionDTO.SBOBETAllow = flag3;
            transactionDTO.SBOBETTrade = flag4;
            transactionDTO.SBOBETReTrade = sBOBETReTrade;
            transactionDTO.OddType = oddType.ToString();
            transactionDTO.Note = text;
            transactionDTO.DateTime = System.DateTime.Now;
            transactionDTO.OddType = oddType.ToString() + " - " + ibetOddType.Replace("h", "Home").Replace("a", "Away") + " / " + sbobetOddType.Replace("h", "Home").Replace("a", "Away");
            this._betting = false;
            return transactionDTO;
            
        }
        private TransactionDTO PlaceBetAllowMaxBet(bool sendOrNOT, MatchDTO ibetMatch, MatchDTO sbobetMatch, eOddType oddType, string ibetOddID, string sbobetOddID, string ibetOddType, string sbobetOddType, string ibetOddValue, string sbobetOddValue, int ibetStake, int sbobetStake, bool ibetHomeGive, IBetEngine ibetEngine, SbobetEngine sbobetEngine, string sbobetOdd)
        {
            this._betting = true;
            TransactionDTO transactionDTO = new TransactionDTO();
            
            //transactionDTO.OddType = current2.Type.ToString() + " - Home / Away";
            string text = "";
            string text2 = "";
            string text3 = "";
            bool flag = false;
            bool flag2 = false;
            int num = 0;
            int num2 = 0;
            string text4 = "";
            string text5 = "";
            string text6 = "";
            bool flag3 = false;
            bool flag4 = false;
            bool sBOBETReTrade = false;
            int num3 = 0;
            string text7 = "";
            string betCount = "";
            string text8 = "";
            bool flag5 = false;

            float ibetOddValue_ = float.Parse(ibetOddValue);
            float sboOddValue_ = float.Parse(sbobetOddValue);

            if (ibetOddValue_ > 0.84 || (ibetOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = ibetStake * (float)txtAddValue.Value;
                ibetStake = ibetStake + (int)valueAdd;
            }
            if (sboOddValue_ > 0.84 || (sboOddValue_ > 0f && txtAddValue.Value.ToString() == "0.01"))
            {
                float valueAdd = sbobetStake * (float)txtAddValue.Value;
                sbobetStake = sbobetStake + (int)valueAdd;
            }

            bool ibetGoFirst = true;// who is Away or Under

            if (checkEdit11.Checked) //safe bet
            {
                if (sbobetOddType == "a") //  ibet  vs sbo
                {
                    if (oddType == eOddType.FirstHalfOverUnder || oddType == eOddType.FulltimeOverUnder)//neu bong over under
                    {
                        ibetGoFirst = false;
                    }
                    else // bong handicap
                    {
                        if (ibetHomeGive) // neu away la doi cua duoi
                        {
                            ibetGoFirst = false;
                        }
                    }
                }
                else //  sbo vs ibet
                {
                    if (oddType == eOddType.FirstHalfOverUnder || oddType == eOddType.FulltimeOverUnder)//neu bong over under
                    {
                        ibetGoFirst = true;
                    }
                    else // bong handicap
                    {
                        if (ibetHomeGive) // neu away la doi cua duoi
                        {
                            ibetGoFirst = true;
                        }
                    }
                }
            }

            if (MatchDTO.IsSameMatch(ibetMatch.HomeTeamName.ToLower(), sbobetMatch.HomeTeamName.ToLower(), ibetMatch.AwayTeamName.ToLower(), sbobetMatch.AwayTeamName.ToLower()))
            {
                try
                {
                    ibetEngine.PrepareBet(ibetOddID, ibetOddType, ibetOddValue, ibetStake.ToString(), out flag, out num, out num2, out text4, out text2, out text3);
                    sbobetEngine.PrepareBet(sbobetOddID, sbobetOddValue, sbobetOddType, out betCount, out num3, out flag3, out text7, out text5, out text6);
                    //object result = this.CallJavascriptFunction("bet", new object[] { 0, ibetMatch.ID.Remove(0, 2), ibetOddID, ibetOddType, ibetOddValue });
                    
                    if (MatchDTO.IsSameMatch(text2, text5, text3, text6)) // ten hom - away giong nhau
                    {                        
                        float num4 = 0f;
                        float num5 = 0f;
                        if (flag && flag3 && float.TryParse(text4, out num4) && float.TryParse(text7, out num5))
                        {
                            if ((checkEdit13.Checked || checkEdit15.Checked) && sendOrNOT)//gui lenh
                            {
                                var taskA = new Task(() => this.AddOddToLocalCommunity(ibetMatch, sbobetMatch, oddType, sbobetOdd, sbobetOdd, ibetOddType, sbobetOddType, ibetOddValue, sbobetOddValue, ibetHomeGive));
                                taskA.Start();
                            }

                            //try parse de tranh hien tuong mat Odd 1 ben
                            //float num4 = float.Parse(text4); 
                            //float num5 = float.Parse(text7);
                            if (num4 + num5 == 0f || num4 == num5)
                            {
                                int num6 = 0;
                                if (num2 >= num3) // num2 maxbet ibet
                                {
                                    num6 = num3; // num3 maxbet sbo
                                }
                                else
                                {
                                    if (num2 <= num3)
                                    {
                                        num6 = num2;
                                    }
                                }
                                if (ibetStake >= num6)
                                {
                                    ibetStake = num6;
                                }
                                if (sbobetStake >= num6)
                                {
                                    sbobetStake = num6;
                                }
                                try
                                {
                                    float currentCredit = 0;
                                    float currentCredit2 = 0;
                                    if (checkEdit8.Checked)
                                    {
                                        currentCredit = ibetEngine.GetCurrentCredit();
                                        currentCredit2 = sbobetEngine.GetCurrentCredit();
                                    }
                                    else
                                    {
                                        currentCredit = ibetEngine._currentCredit;
                                        currentCredit2 = sbobetEngine._currentCredit;
                                    }

                                    if ((float)ibetStake <= currentCredit && (float)sbobetStake <= currentCredit2)
                                    {
                                        try
                                        {
                                            if (ibetGoFirst)
                                            {
                                                
#if TESTMODE
                                                #region IBET_FIRTST_THEN_SBO
                                                if (!checkEdit12.Checked || (checkEdit12.Checked && ibetOddType == "a" && (oddType == eOddType.FirstHalfOverUnder || oddType == eOddType.FulltimeOverUnder)))
                                                    ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
                                                if (flag2 && !checkEdit12.Checked)
                                                {
                                                    sbobetEngine.ConfirmBet(sbobetOddID, sbobetOddValue, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out flag4);
                                                    if (!flag4)
                                                    {
                                                        try
                                                        {
                                                            sbobetEngine.PrepareBet(sbobetOddID, text8, sbobetOddType, out betCount, out num3, out flag5, out text7, out text5, out text6);
                                                            if (MatchDTO.IsSameMatch(text2, text5, text3, text6))
                                                            {
                                                                if (!float.TryParse(text7, out num5)) //text 7 = BetKindValue
                                                                {
                                                                    Thread.Sleep(3000);
                                                                    sbobetEngine.PrepareBet(sbobetOddID, text8, sbobetOddType, out betCount, out num3, out flag5, out text7, out text5, out text6);
                                                                    float.TryParse(text7, out num5);
                                                                }

                                                                if (num4 + num5 == 0f || num4 == num5)
                                                                {
                                                                    text = "Retrade. IBET: " + ibetOddValue + " -  SBOBET: " + text8;
                                                                    //text = "No retrade in this version please trade manually.";
                                                                    sbobetEngine.ConfirmBet(sbobetOddID, text8, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out sBOBETReTrade);
                                                                    this._lastTransactionTime = System.DateTime.Now;
                                                                    playSound(false);
                                                                    
                                                                }
                                                                else
                                                                {
                                                                    text = "Invalid Odd in SBOBET. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                                                                    playSound(false);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                text = string.Concat(new string[]
															    {
																    "Not Same Match - Retrade. IBET: ", 
																    text2, 
																    " / ", 
																    text3, 
																    " - SBOBET: ", 
																    text5, 
																    " / ", 
																    text6
															    });
                                                            }
                                                        }
                                                        catch (System.Exception ex)
                                                        {
                                                            text = "Error while Retrade. Details: " + ex.Message;
                                                            playSound(false);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        playSound(true);
                                                    }

                                                    this._lastTransactionTime = System.DateTime.Now;
                                                    object obj = text;
                                                    text = string.Concat(new object[]
												    {
													    obj, 
													    " - Success Transaction. Half: ", 
													    ibetMatch.Half, 
													    " - Minute: ", 
													    ibetMatch.Minute, 
													    " - Halftime: ", 
													    ibetMatch.IsHalfTime
												    });
                                                    ibetEngine.GetCurrentCredit();
                                                    sbobetEngine.GetCurrentCredit();

                                                }
                                                
                                                #endregion
#endif
                                            }

                                            else
                                            {
                                                text = "No more SBOBET comes first.";
                                                //#region SBO_BET_FIRST_THEN_IBET

                                                //sbobetEngine.ConfirmBet(sbobetOddID, sbobetOddValue, sbobetOddType, sbobetStake.ToString(), betCount, out text8, out flag4);
                                                //if (flag4)
                                                //{ 
                                                //    ibetEngine.ConfirmBet(oddType, ibetOddValue, ibetStake.ToString(), num.ToString(), num2.ToString(), out flag2);
                                                //    if (!flag2)
                                                //    {
                                                //        text = "Invalid Odd while Retrade.";// IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                                                //        sbobetEngine.GetCurrentCredit();
                                                //        playSound(false);
                                                //    }
                                                //    else
                                                //    {
                                                //        this._lastTransactionTime = System.DateTime.Now;
                                                //        object obj = text;
                                                //        text = string.Concat(new object[]
                                                //        {
                                                //            obj, 
                                                //            " - Success Transaction. Half: ", 
                                                //            ibetMatch.Half, 
                                                //            " - Minute: ", 
                                                //            ibetMatch.Minute, 
                                                //            " - Halftime: ", 
                                                //            ibetMatch.IsHalfTime
                                                //        });
                                                //        ibetEngine.GetCurrentCredit();
                                                //        sbobetEngine.GetCurrentCredit();
                                                //        playSound(true);
                                                //    }
                                                //}

                                                //#endregion
                                            }

                                        }
                                        catch (System.Exception ex)
                                        {
                                            text = "Error while Trading. Details: " + ex.Message;
                                        }
                                    }
                                    else
                                    {
                                        text = string.Concat(new object[]
										{
											"Out of Cash. IBET Credit: ", 
											currentCredit, 
											" -  SBOBET Credit: ", 
											currentCredit2
										});
                                    }
                                }
                                catch (System.Exception ex)
                                {
                                    text = "Error while getting Credit. Details: " + ex.Message;
                                }
                            }
                            else
                            {
                                text = "Invalid Odd while Preparing Ticket. IBET Odd: " + text4 + " - SBOBET Odd:" + text7;
                            }
                        }
                    }                        
                    else
                    {
                        text = string.Concat(new string[]
						{
							"Not Same Match - Preparing Ticket. IBET: ", 
							text2, 
							" / ", 
							text3, 
							" - SBOBET: ", 
							text5, 
							" / ", 
							text6
						});
                    }                    
                }
                catch (System.Exception ex)
                {
                    text = "Error while Preparing Trade. Details: " + ex.Message;
                }
            }
            else
            {
                text = string.Concat(new string[]
				{
					"Not Same Match - Comparing. IBET: ", 
					ibetMatch.HomeTeamName, 
					" / ", 
					ibetMatch.AwayTeamName, 
					" - SBOBET: ", 
					sbobetMatch.HomeTeamName, 
					" / ", 
					sbobetMatch.AwayTeamName
				});
            }
            transactionDTO.HomeTeamName = ibetMatch.HomeTeamName + ":" + ibetMatch.HomeScore + " / " + sbobetMatch.HomeTeamName;
            transactionDTO.AwayTeamName = ibetMatch.AwayTeamName + ":" + ibetMatch.AwayScore + " / " + sbobetMatch.AwayTeamName;
            transactionDTO.Odd = text4 + " / " + text7;
            transactionDTO.OddKindValue = text4 + " / " + text7;
            transactionDTO.OddValue = ibetOddValue + " / " + sbobetOddValue;
            transactionDTO.Stake = ibetStake + " / " + sbobetStake;
            transactionDTO.IBETAllow = flag;
            transactionDTO.IBETTrade = flag2;
            transactionDTO.IBETReTrade = false;
            transactionDTO.SBOBETAllow = flag3;
            transactionDTO.SBOBETTrade = flag4;
            transactionDTO.SBOBETReTrade = sBOBETReTrade;
            //transactionDTO.OddType = oddType.ToString() + ;
            transactionDTO.OddType = oddType.ToString() + " - " + ibetOddType.Replace("h", "Home").Replace("a", "Away") + " / " + sbobetOddType.Replace("h", "Home").Replace("a", "Away");
            transactionDTO.Note = text;
            transactionDTO.DateTime = System.DateTime.Now.ToLocalTime();

            transactionDTO.HomeTeamSBOBET = sbobetMatch.HomeTeamName;
            transactionDTO.AwayTeamSBOBET = sbobetMatch.AwayTeamName;
            
            this._betting = false;
            return transactionDTO;            
        }        
 private void TransactionProcess(MatchDTO ibetMatch, MatchDTO sbobetMatch, eOddType oddType, string ibetOddID, string sbobetOddID, string ibetOddType, string sbobetOddType, string ibetOddValue, string sbobetOddValue, int ibetStake, int sbobetStake, IBetEngine ibetEngine, SbobetEngine sbobetEngine, string sbobetOdd, bool homeFavor)
 {
     if (!this._betting)
     {
         TransactionDTO transactionDTO = new TransactionDTO();
         transactionDTO = this.PlaceBet(true, ibetMatch, sbobetMatch, oddType, ibetOddID, sbobetOddID, ibetOddType, sbobetOddType, ibetOddValue, sbobetOddValue.ToString(), ibetStake, sbobetStake, this._ibetEngine, this._sbobetEngine, sbobetOdd, homeFavor);
         this.AddTransaction(transactionDTO);
         if (transactionDTO != null && transactionDTO.IBETTrade)
         {
             this.UpdateOddBetHistory(ibetOddID,"");
         }
     }
 }