public RootObject login(String username, String password)
        {

            if (username == null || username.Trim().Length == 0 || password == null || password.Trim().Length == 0)
            {
                MessageBox.Show("invalid User credentials");
            }

           
            var client = new RestClient(Resource.endpoint + "wittyparrot/api/auth/login");
            var strJSONContent = "{\"userId\":\"" + username + "\" ,\"password\":\"" + password + "\"}";
            
            var request = new RestRequest();
            request.Method = Method.POST;
            request.AddHeader("Accept", "application/json");
            request.Parameters.Clear();
            request.AddParameter("application/json", strJSONContent, ParameterType.RequestBody);
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Content-Type", "application/json");

            // If the response has exception the applocation will half and throw Application exception
            IRestResponse response = client.Execute(request);
            if (response.ErrorException != null || response.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                var statusMessage = RestUtils.getErrorMessage(response.StatusCode);
                MessageBox.Show(statusMessage == "" ? response.StatusDescription : statusMessage);
                var myException = new ApplicationException(response.StatusDescription, response.ErrorException);
                throw myException;
            }
           
            var content = response.Content;
            RootObject rootObj = new RootObject();
            rootObj = JsonConvert.DeserializeObject<RootObject>(content);
            return rootObj;
        }
        // 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());
            }

        }
        public void prepareUserDBSchema(RootObject rootObj)
        {

            createLocalUserProfileFolder();

            if (!File.Exists(Common.localProfilePath + "\\userDB.sqlite"))
            {

                SQLiteConnection.CreateFile(Common.localProfilePath + "\\userDB.sqlite");

                // create table queries
                var commentQuery = Resource.ResourceManager.GetString("comments");
                var contactsQuery = Resource.ResourceManager.GetString("contacts");
                var contentExpiryQuery = Resource.ResourceManager.GetString("content_expiry");
                var docsQuery = Resource.ResourceManager.GetString("docs");
                var eventRecordsQuery = Resource.ResourceManager.GetString("event_records");
                var foldersQuery = Resource.ResourceManager.GetString("folders");

                var groupContactsuery = Resource.ResourceManager.GetString("group_contacts");
                var groupsQuery = Resource.ResourceManager.GetString("groups");
                var notificationActionsQuery = Resource.ResourceManager.GetString("notification_actions");
                var notificationsQuery = Resource.ResourceManager.GetString("notifications");

                var packageQuery = Resource.ResourceManager.GetString("package");
                var packageFeatureQuery = Resource.ResourceManager.GetString("package_feature");
                var socialMediaQuery = Resource.ResourceManager.GetString("socialmedia");
                var tagGroupsQuery = Resource.ResourceManager.GetString("taggroups");
                var tagsQuery = Resource.ResourceManager.GetString("tags");

                var topWitsQuery = Resource.ResourceManager.GetString("top_wits");
                var userDefaultsQuery = Resource.ResourceManager.GetString("user_defaults");
                var userPackagesuery = Resource.ResourceManager.GetString("user_package");
                var witAttachmentQuery = Resource.ResourceManager.GetString("wit_attachments");
                var witTagsQuery = Resource.ResourceManager.GetString("wit_tags");

                var witsQuery = Resource.ResourceManager.GetString("wits");
                var witsUsageQuery = Resource.ResourceManager.GetString("wits_usage");
                var witsUsageGraphDataQuery = Resource.ResourceManager.GetString("witsusagegraph_data");
                var witsUsageGraphsQuery = Resource.ResourceManager.GetString("wits_usagegraphs");

                var userWorkspacesQuery = Resource.ResourceManager.GetString("userworkspaces");
                var createdbyQuery = Resource.ResourceManager.GetString("createdby");
                var modifiedbyQuery = Resource.ResourceManager.GetString("modifiedby");
                var permissionQuery = Resource.ResourceManager.GetString("permission");

                var profileSyncEventQuery = Resource.ResourceManager.GetString("profileSyncEvent");
                


                // create table 
                this.ExecuteUserDBQuery(commentQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(contactsQuery, Common.localProfilePath);

                this.ExecuteUserDBQuery(contentExpiryQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(docsQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(eventRecordsQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(foldersQuery, Common.localProfilePath);

                this.ExecuteUserDBQuery(groupContactsuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(groupsQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(notificationActionsQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(notificationsQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(packageQuery, Common.localProfilePath);

                this.ExecuteUserDBQuery(packageFeatureQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(socialMediaQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(tagGroupsQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(tagsQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(topWitsQuery, Common.localProfilePath);

                this.ExecuteUserDBQuery(userDefaultsQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(userPackagesuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(witAttachmentQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(witTagsQuery, Common.localProfilePath);

                this.ExecuteUserDBQuery(witsQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(witsUsageQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(witsUsageGraphDataQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(witsUsageGraphsQuery, Common.localProfilePath);

                this.ExecuteUserDBQuery(userWorkspacesQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(createdbyQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(modifiedbyQuery, Common.localProfilePath);
                this.ExecuteUserDBQuery(permissionQuery, Common.localProfilePath);

                this.ExecuteUserDBQuery(profileSyncEventQuery, Common.localProfilePath);

            }
            else { }

        }