/*		public void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs  e)
         *      {
         *              // This example shows how to create a script line in QuickTest
         *              // when a MouseDown event is encountered during recording.
         *              if(e.Button == System.Windows.Forms.MouseButtons.Left)
         *              {
         *                      RecordFunction( "CustomMouseDown", RecordingMode.RECORD_SEND_LINE, e.X, e.Y);
         *              }
         *      }
         */
        #endregion
        #endregion

        #region MarsFuncitons
        public bool ClickMenuItem(object objMenuInfo)
        {
            Logger.logBegin("ClickMenuItem");
            //MessageBox.Show("come");
            string strMenuInfo = objMenuInfo.ToString();

            Logger.Info("ClickMenuItem", string.Format("parameters:[{0}]", strMenuInfo));
            try
            {
                string[] arrMenuItems = strMenuInfo.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                if (!(base.SourceControl is DevExpress.XtraBars.Controls.PopupMenuBarControl))
                {
                    Logger.Error("ClickMenuItem", string.Format("Control is not DevExpress.XtraBars.Controls.PopupMenuBarControl, but [{0}]", base.SourceControl.GetType().ToString()));
                    return(false);
                }
                DevExpress.XtraBars.Controls.PopupMenuBarControl objMenu = (DevExpress.XtraBars.Controls.PopupMenuBarControl)base.SourceControl;
                //objMenu.Menu.ItemLinks[0].
                List <BarItemLink> lstMenuItem = new List <BarItemLink>();
                if (TraverseItems(objMenu.Menu.ItemLinks, arrMenuItems, 0, lstMenuItem))
                {
                    if (lstMenuItem.Count > 0)
                    {
                        /** click all items by order **/
                        for (int i = 0; i < lstMenuItem.Count; i++)
                        {
                            Rectangle rect = lstMenuItem[i].Bounds;
                            /** convert to screen Position **/
                            Point ptScreen = objMenu.PointToScreen(new Point(rect.X, rect.Y));
                            TigerMarsUtil.LeftMouseClick(ptScreen.X + rect.Width / 2, ptScreen.Y + rect.Height / 2);
                            Thread.Sleep(100);
                        }
                        return(true);
                    }
                    Logger.Error("ClickMenuItem", "TraverseItems return true, but no items exists in Chain. ");
                    return(false);
                }
                else
                {
                    Logger.Error("ClickMenuItem", "no menu Item matches. ");
                    return(false);
                }
            }
            finally
            {
                Logger.logEnd("ClickMenuItem");
            }
        }
        /*		public void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs  e)
         *      {
         *              // This example shows how to create a script line in QuickTest
         *              // when a MouseDown event is encountered during recording.
         *              if(e.Button == System.Windows.Forms.MouseButtons.Left)
         *              {
         *                      RecordFunction( "CustomMouseDown", RecordingMode.RECORD_SEND_LINE, e.X, e.Y);
         *              }
         *      }
         */
        #endregion
        #endregion


        #region Replay interface implementation
        #region Wizard generated sample code (commented)

        /*		public void CustomMouseDown(int X, int Y)
         *      {
         *              MouseClick(X, Y, MOUSE_BUTTON.LEFT_MOUSE_BUTTON);
         *      }
         */
        #endregion

        public bool ClickAtSpecialNode(params object[] arrObj)
        {
            Logger.logBegin("ClickAtSpecialNode");
            base.PrepareForReplay();
            if (!(base.SourceControl is TreeList))
            {
                Logger.Error("ClickAtSpecialNode", string.Format("Type is [{0}],but [{1}] and any of its Descendants is required. ", base.SourceControl.GetType().ToString(), typeof(TreeList).ToString()));
                return(false);
            }


            if (arrObj.Length < 2)
            {
                Logger.Error("ClickAtSpecialNode", string.Format("For Sophis(DevExpress based) application, at least two parameters required, 1st--[Left or right mouse button]"));
            }

            /**
             * Parameters:
             * 1, Left or right mouse button,0, left,1-right,2 left-double click,3-expand
             * 2, Node text to be clicked at with path
             * **/

            /** parameters check **/
            int iMouseMark = -1;

            if (!int.TryParse(arrObj[0].ToString(), out iMouseMark))
            {
                Logger.Error("ClickAtSpecialNode", string.Format("First parameters should be [0,2-Left Mouse button/1-Right Mouse button], but the value is [{0}]", arrObj[0].ToString()));
                return(false);
            }
            string strNodeToSearch = arrObj[1].ToString();
            /** for DevExpress, the last parameter is the column Id, default value is the first column to search **/
            int iColumn = 0;

            if (arrObj.Length >= 3)
            {
                /** the column information could be string **/
                /** **/
                if (!int.TryParse(arrObj[2].ToString(), out iColumn))
                {
                    Logger.Info("ClickAtSpecialNode", string.Format("Can't convert the third paramter [{0}] to Int, default value 0 is used", arrObj[2].ToString()));
                    return(false);
                }
            }

            try
            {
                /** get position **/
                string[] arrNodeSearchItems = strNodeToSearch.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                TreeList objTrLst           = (TreeList)base.SourceControl;

                bool   isFindNode    = true;
                object objDataSrcTmp = objTrLst.DataSource;
                if (objTrLst.Nodes.Count <= 0)
                {
                    Logger.Error("ClickAtSpecialNode", "no Node exists ");
                    return(false);
                }
                TreeListNode objCurrentNode = objTrLst.Nodes[0];

                if (objCurrentNode == null)
                {
                    Logger.Error("ClickAtSpecialNode", "Root node is null ");
                    return(false);
                }
#if _sophis7_
                if (objTrLst is Sophis.Util.GUI.CustomTreeList)
                {
                    if (objTrLst.Columns.Count > 1)
                    {
                        isFindNode = FindNodeBySettings_Sophis((Sophis.Util.GUI.CustomTreeList)objTrLst, arrNodeSearchItems.Length, 0, arrNodeSearchItems, ref objCurrentNode, iColumn);
                    }
                    else
                    {
                        isFindNode = FindNodeBySettings(objTrLst.Nodes, arrNodeSearchItems.Length, 0, arrNodeSearchItems, ref objCurrentNode, iColumn);
                    }
                }
                else
                {
                    isFindNode = FindNodeBySettings(objTrLst.Nodes, arrNodeSearchItems.Length, 0, arrNodeSearchItems, ref objCurrentNode, iColumn);
                }
#else
                isFindNode = FindNodeBySettings(objTrLst.Nodes, arrNodeSearchItems.Length, 0, arrNodeSearchItems, ref objCurrentNode, iColumn);
#endif

                if (!isFindNode)
                {
                    return(false);
                }

                Thread.Sleep(100);
                //objTrLst.VirtualDataHelper
                /** try to double or click node **/
                RowInfo  objRow  = objTrLst.ViewInfo.RowsInfo[objCurrentNode];
                CellInfo objCell = objRow.Cells[0] as CellInfo;
                if (objCell == null)
                {
                    Logger.Error("ClickAtSpecialNode", "View for cell info is null");
                    return(false);
                }

                Rectangle objRectTarget = objCell.Bounds;
                Logger.Info("ClickAtSpecialNode", string.Format("Bounds:x:[{0}],y:[{1}],w:[{2}],h:[{3}]", objRectTarget.X, objRectTarget.Y, objRectTarget.Width, objRectTarget.Height));

                /** convert 2 screen and move mouse to **/
                Point ptScreenLocation = objTrLst.PointToScreen(objRectTarget.Location);
                Point ptMouseMoveTo    = new Point(ptScreenLocation.X + objRectTarget.Width / 2,
                                                   ptScreenLocation.Y + objRectTarget.Height / 2);
                Logger.Info("ClickAtSpecialNode", string.Format("New screen position:[{0},{1}] compare to ", ptMouseMoveTo.X, ptMouseMoveTo.Y));
                /** stimulate click **/
                switch (iMouseMark)
                {
                case 0:
                case 2:
                    TigerMarsUtil.LeftMouseClick(ptMouseMoveTo.X, ptMouseMoveTo.Y);
                    if (iMouseMark == 2)
                    {
                        Thread.Sleep(50);
                        TigerMarsUtil.LeftMouseClick(ptMouseMoveTo.X, ptMouseMoveTo.Y);
                    }
                    break;

                case 1:
                    TigerMarsUtil.RightMouseClick(ptMouseMoveTo.X, ptMouseMoveTo.Y);
                    break;

                default:
                    /** Expand **/
                    TigerMarsUtil.LeftMouseClick(ptMouseMoveTo.X, ptMouseMoveTo.Y);
                    Thread.Sleep(50);
                    objCurrentNode.ExpandAll();
                    break;
                }

                return(true);
            }
            catch (Exception e)
            {
                Logger.Error("ClickAtSpecialNode", string.Format("Errors come when trying to click special Node:[{0}], Exceptions:[{1}]", strNodeToSearch, e.Message), e);
                return(false);
            }
            finally
            {
                Logger.logEnd("ClickAtSpecialNode");
            }
        }