private void SetDirectionAndCalling(int calledFirst)
        {
            MovementAndPositionTracker obj = new MovementAndPositionTracker();

            Session["InitialDirection"] = obj.CheckDirection(calledFirst, (int)Session["CurrentPosition"]);
            Session["Direction"]        = (Session["InitialDirection"]).ToString() == "Up" ? 1 : 0;
            lblCalledFrom.Text          = (Session["CalledFirst"]).ToString() + " Floor";
            lblDirection.Text           = (string)Session["InitialDirection"];
        }
        private int GetSetFloorPosition()
        {
            lblFloorPosition.Text  = ((int)GlobalEnums.Floor.ZerothFloor).ToString() + GlobalEnums.defaulFloor;
            trInsideButton.Visible = false;

            MovementAndPositionTracker obj = new MovementAndPositionTracker();

            obj.currentPosition         = 0;
            Session["CurrentPosition"]  = obj.currentPosition;
            Session["InitialPosition"]  = 0;
            Session["requestCompleted"] = 0;
            return(obj.currentPosition);
            //tdDown.Visible = false;
        }
        public void DoWork(int floorNo, int directiondesired, int previousTime, Control ctrl)
        {
            MovementAndPositionTracker obj      = new MovementAndPositionTracker();
            Dictionary <int, int>      requestQ = new Dictionary <int, int>();

            requestQ.Add(floorNo, directiondesired);
            obj.destinationFloor = floorNo;

            Session["PostReqQ"] = null;


            if (Session["CurrentReqQ"] != null)
            {
                Dictionary <int, int> myDictionary = (Dictionary <int, int>)Session["CurrentReqQ"];
                obj.CurrentRequestQueue = myDictionary;
                obj.CurrentRequestQueue.Add(floorNo, directiondesired);
                Session["CurrentReqQ"] = obj.CurrentRequestQueue;
            }
            else
            {
                obj.CurrentRequestQueue = requestQ;
                Session["CurrentReqQ"]  = obj.CurrentRequestQueue;
            }

            if (obj.CurrentRequestQueue.Count > 1)
            {
                SetPreviousTime(previousTime, ctrl);
            }
            else
            {
                int timeMultiplier = obj.GetTimeMultiplier(obj.destinationFloor, obj.currentPosition);
                SetTime(timeMultiplier);
                timer1.Enabled = true;
            }

            MainScriptManager.RegisterAsyncPostBackControl(ctrl);

            Session["CalledFirst"] = obj.CurrentRequestQueue.ElementAt(0).Key;
            SetDirectionAndCalling((int)Session["CalledFirst"]);
            Session["InputTaken"] = false;
        }
        //static int requestCompleted = 0;
        //static int currentposition = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MovementAndPositionTracker obj = new MovementAndPositionTracker();
                obj.currentPosition  = GetSetFloorPosition();
                obj.destinationFloor = 0;
            }

            //Mapping Number of second required to the called floor

            if (!MainScriptManager.IsInAsyncPostBack)
            {
                SetTime(0);
                if (Session["CurrentPosition"] != null && Session["RequestFrom"] != null)
                {
                    if (Session["CurrentPosition"].ToString() == Session["RequestFrom"].ToString())
                    {
                        AddSecondsOnReachingDestination();
                    }
                }
            }
        }
        protected void timer1_tick(object sender, EventArgs e)
        {
            if (0 > DateTime.Compare(DateTime.Now, DateTime.Parse(Session["timeout"].ToString())))
            {
                int timerunning = ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).TotalSeconds);
                MovementAndPositionTracker obj          = new MovementAndPositionTracker();
                Dictionary <int, int>      myDictionary = (Dictionary <int, int>)Session["CurrentReqQ"];

                Session["RequestFrom"] = myDictionary.ElementAt(0).Key;
                int currentPosition  = (int)Session["CurrentPosition"];
                int requestCompleted = (int)Session["requestCompleted"];
                //Setting the Floor Position Dynamically while Moving
                //we have to stop in middle if there are other request while going on the way


                if (myDictionary.Count == 1)
                {
                    int timerunning1 = ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).TotalSeconds);
                    lblFloorPosition.Text       = obj.CheckFloorPosition(ref currentPosition, (int)Session["RequestFrom"], timerunning1, ref requestCompleted);
                    Session["CurrentPosition"]  = currentPosition;
                    Session["requestCompleted"] = requestCompleted;

                    if ((int)Session["CurrentPosition"] == (int)Session["RequestFrom"])
                    {
                        //trInsideButton.Visible = true;
                        if (Session["LastFloorPosition"] == null)
                        {
                            trInsideButton.Visible = true;
                        }

                        if (Session["LastFloorPosition"] != null)
                        {
                            int localCurrentpos = Convert.ToInt32(Session["CurrentPosition"]);
                            int localLastpos    = Convert.ToInt32(Session["LastFloorPosition"]);
                            if (localCurrentpos > localLastpos)
                            {
                                trInsideButton.Visible = false;
                                Session.Remove("LastFloorPosition");
                            }
                        }

                        if (trInsideButton.Visible == true)
                        {
                            timer1.Enabled = false;
                        }
                    }
                    else
                    {
                        trInsideButton.Visible = false;
                    }
                }
                else if (myDictionary.Count > 1)
                {
                    //get all collection for more than currentposition
                    var preRequestQ = myDictionary.Where(s => s.Key > (int)Session["InitialPosition"]);

                    // 0- Down and 1- Up
                    int directionofMovement = (int)Session["Direction"];

                    //get max floor value
                    int maxfloorrequest = Convert.ToInt32(myDictionary.OrderByDescending(s => s.Key).First().Key);

                    // for Up movement
                    if (directionofMovement == 1)
                    {
                        if ((int)Session["CurrentPosition"] == (int)Session["RequestFrom"])
                        {
                            if (Session["LastFloorPosition"] == null)
                            {
                                trInsideButton.Visible = true;
                            }

                            if (Session["LastFloorPosition"] != null)
                            {
                                int localCurrentpos = Convert.ToInt32(Session["CurrentPosition"]);
                                int localLastpos    = Convert.ToInt32(Session["LastFloorPosition"]);
                                if (localCurrentpos > localLastpos)
                                {
                                    trInsideButton.Visible = false;
                                    Session.Remove("LastFloorPosition");
                                }
                            }

                            if (trInsideButton.Visible == true)
                            {
                                timer1.Enabled = false;
                            }

                            //Now take the Input for floor to go
                            //Start the timer hare again
                        }
                        else
                        {
                            foreach (KeyValuePair <int, int> item in preRequestQ)
                            {
                                if ((int)Session["CurrentPosition"] == item.Key)
                                {
                                    if (Session["LastFloorPosition"] == null)
                                    {
                                        trInsideButton.Visible = true;
                                    }

                                    if (Session["LastFloorPosition"] != null)
                                    {
                                        int localCurrentpos = Convert.ToInt32(Session["CurrentPosition"]);
                                        int localLastpos    = Convert.ToInt32(Session["LastFloorPosition"]);
                                        if (localCurrentpos > localLastpos)
                                        {
                                            trInsideButton.Visible = false;
                                            Session.Remove("LastFloorPosition");
                                        }
                                    }

                                    if (trInsideButton.Visible == true)
                                    {
                                        timer1.Enabled = false;
                                    }
                                }
                            }
                        }
                        int timerunning2 = ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).TotalSeconds);
                        lblFloorPosition.Text       = obj.CheckFloorPosition(ref currentPosition, (int)Session["RequestFrom"], timerunning2, ref requestCompleted);
                        Session["CurrentPosition"]  = currentPosition;
                        Session["requestCompleted"] = requestCompleted;
                    }
                    //for Down movement
                    else if (directionofMovement == 0)
                    {
                    }
                }

                if (timerunning < 10)
                {
                    lblTimeTaken.Text = "0" + timerunning.ToString() + " Seconds";
                }
                else
                {
                    lblTimeTaken.Text = timerunning.ToString() + " Seconds";
                }
            }
            else
            {
                lblTimeTaken.Text = "00 " + "Seconds";
            }
        }