// gets invoke when the user logins
        public void login(object sender, EventArgs e)
        {

            try {
                Panel witsPanel = this.witsPanel;
                witsPanel.Visible = false;
               

                Panel panel = this.pnlMenu;
                panel.Visible = true;

                // get the username and pasword from the widget
                var userName = textBox1.Text;
                var password = textBox2.Text;

                // rest api for login
                RestClientLogin clientLogin = new RestClientLogin();
                this.rootObj = clientLogin.login(userName, password);

                // check if the db is already present or not
                UserDBConnector userDBConnector = new UserDBConnector(userName);
                if (!userDBConnector.isDataBaseExists()) {
                    refreshDatabaseThread();
                }

                AccessTokenDao accessTokenDao = new AccessTokenDao();
                accessTokenDao.saveAccessToken(rootObj.accessToken);

                this.userName = userName;
                this.password = password;

                UserWorkspaceDao workspaceDao = null;
                if (rootObj.userProfile.userWorkspaces != null && rootObj.userProfile.userWorkspaces.Count > 0) {

                    workspaceDao = new UserWorkspaceDao();
                    // fetch all the workspaces and show it in the workspace panel

                    if (workspaceDao.getWorkspaceNameList() != null && workspaceDao.getWorkspaceNameList().Count != 0)
                    {
                        List<UserWorkspace> workspaces = workspaceDao.getWorkspaceList();                     
                        panel.Controls.Clear(); // clear the panel before adding the workspace controls

                        // loop through all the workspaces and get the folders of the workspaces
                        foreach (var workspace in workspaces)
                        {

                            CustomWorkspaceButton workspaceButton = new CustomWorkspaceButton();
                            workspaceButton.Text = " " + workspace.Name;

                            var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                            var path = Path.Combine(appDataPath, "wpoutlookwidget" + @"\");

                            workspaceButton.Image = new Bitmap(path + "wpdependencies\\list_icon.ico");
                            workspaceButton.Click += workspaceButtonHandler;

                            CustomWorkspacePanel childPanel = new CustomWorkspacePanel();                          
                            childPanel.Controls.Add(workspaceButton);

                            panel.Controls.Add(childPanel);

                        }
                    }


                    // make the backgroud color WhiteSmoke so that if clicks for wits
                    // backgroud should should not look un even
                    this.BackColor = System.Drawing.Color.WhiteSmoke;

                    // clear all the controls from the widget and show only Workspace panel
                    // workspace panel is the starting point (iniital screen) 
                    afterLogin();
                    mainTabPanel.Visible = true;


                    // save all the images needed to show to the widget
                    ImageList myImageList = new ImageList();
                    myImageList.Images.Add(Resource.grayfolder);
                    myImageList.Images.Add(Resource.grayfolder);
                    myImageList.ColorDepth = ColorDepth.Depth32Bit;

                    // Assign the ImageList to the TreeView.
                    myCustomTreeView.ImageList = myImageList;

                    // Set the TreeView control's default image and selected image indexes.
                    myCustomTreeView.ImageIndex = 0;
                    myCustomTreeView.SelectedImageIndex = 0;

                    // control factory for future use
                    witChildPanels = new List<CustomWitPanel>();
                    ControlFactory controlFactory = new ControlFactory();
                    witChildPanels = controlFactory.getChildWitPanels();

                }
                else {

                    // when there is no folders to show !!!
                    MessageBox.Show("No Workspace to show");
                }
            }
            catch (SQLiteException sqlException)
            {
                MessageBox.Show("Error occured: " + sqlException.ToString());
            }
            catch (System.ApplicationException appException)
            {
                MessageBox.Show("Error occured: "+ appException.ToString());
            }
            catch (Exception ew) {

                MessageBox.Show("Error occured: "+ ew.ToString());
            }

        }
        // It is used to collapse and expand the workspaces's associated folders 
        // When we click on particular workspace control it pulls up the folder of that workspace
        // and show in the Tree view heirarchy
        void workspaceButtonHandler(object sender, EventArgs e)
        {
            Button clickedButton = (Button)sender;
            CustomWorkspacePanel clickedPanel = (CustomWorkspacePanel)((Button)sender).Parent;

            // check if the panel is already expanded, then clear all child
            if (clickedPanel.Controls.Count > 1)
            {
                clickedPanel.Controls.Clear();
                clickedPanel.Controls.Add(clickedButton);
            }
            else if (clickedPanel.Controls.Count == 1)
            {

                var workspaceName = clickedButton.Text;
                UserWorkspaceDao workspaceDao = new UserWorkspaceDao();
                UserWorkspace workspaceSelected = workspaceDao.getByName(workspaceName.Trim());
                prepareTreeNodeHeirarchy(workspaceSelected.WorkspaceId);

                Panel childPanel = new Panel();
                childPanel.AutoSize = true;
                childPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                childPanel.Dock = System.Windows.Forms.DockStyle.Top;
                childPanel.Location = new System.Drawing.Point(0, 0);
                childPanel.Name = "childPanel";
                childPanel.Size = new System.Drawing.Size(200, 104);
                childPanel.TabIndex = 1;
                childPanel.BackColor = System.Drawing.Color.WhiteSmoke;
                childPanel.Controls.Add(myCustomTreeView);
                clickedPanel.Controls.Add(childPanel);

                clickedPanel.Controls.Add(clickedButton);
            }

        }
        private void refreshDatabase()
        {

            try
            {           

                UserDBConnector userDBConnector = new UserDBConnector(Common.userName);         
                userDBConnector.prepareUserDBSchema(rootObj);
               
                AccessTokenDao accessTokenDao = new AccessTokenDao();
                accessTokenDao.saveAccessToken(rootObj.accessToken);

             
                UserWorkspaceDao workspaceDao = new UserWorkspaceDao();
                workspaceDao.saveWorkspaces(rootObj.userProfile.userWorkspaces);

                if (workspaceDao.getWorkspaceNameList() != null && workspaceDao.getWorkspaceNameList().Count != 0)
                {
                    List<UserWorkspace> workspaces = workspaceDao.getWorkspaceList();
                   
                    foreach (var workspace in workspaces)
                    {
                        RestClientFolder restClientFolder = new RestClientFolder();
                        restClientFolder.getAllFolders(rootObj.accessToken.tokenValue, workspace.WorkspaceId, 0);
                    }

                   // FolderDao folderDao = new FolderDao();
                    //folderDao.saveAllFolders(allFolderList);
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("Error occured while refreshing data:" + e.Message);
               
            }

        }