/**
  * delete messages in cache
  * 
  *   - if the latest message on the cache has a different sequence number then on the imap server
  *     then some messages before the latest message(from the cache) got deleted
  *     we need to remove them from local cache first
  *     
  *   - $folder->cache_totalcount equals to the message sequence of the last message in the cache
  * 
  * @param Felamimail_Model_Folder $_folder
  * @param Felamimail_Backend_ImapProxy $_imap
  */
 protected function _deleteMessagesInCache(Felamimail_Model_Folder $_folder, Felamimail_Backend_ImapProxy $_imap)
 {
     if ($this->_messagesDeletedOnIMAP($_folder)) {
         $messagesToRemoveFromCache = $this->_cacheMessageSequence - $this->_imapMessageSequence;
         if ($this->_initialCacheStatus == Felamimail_Model_Folder::CACHE_STATUS_COMPLETE || $this->_initialCacheStatus == Felamimail_Model_Folder::CACHE_STATUS_EMPTY) {
             $_folder->cache_job_actions_est += $messagesToRemoveFromCache;
         }
         $_folder->cache_status = Felamimail_Model_Folder::CACHE_STATUS_INCOMPLETE;
         if ($this->_timeElapsed < $this->_availableUpdateTime) {
             $begin = $_folder->cache_job_startuid > 0 ? $_folder->cache_job_startuid : $_folder->cache_totalcount;
             $firstMessageSequence = 0;
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " {$messagesToRemoveFromCache} message to remove from cache. starting at {$begin}");
             }
             for ($i = $begin; $i > 0; $i -= $this->_importCountPerStep) {
                 $firstMessageSequence = $i - $this->_importCountPerStep >= 0 ? $i - $this->_importCountPerStep : 0;
                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " fetching from {$firstMessageSequence}");
                 }
                 $cachedMessageUids = $this->_getCachedMessageUidsChunked($_folder, $firstMessageSequence);
                 // $cachedMessageUids can be empty if we fetch the last chunk
                 if (count($cachedMessageUids) > 0) {
                     $messageUidsOnImapServer = $_imap->messageUidExists($cachedMessageUids);
                     $difference = array_diff($cachedMessageUids, $messageUidsOnImapServer);
                     $removedMessages = $this->_deleteMessagesByIdAndUpdateCounters(array_keys($difference), $_folder);
                     $messagesToRemoveFromCache -= $removedMessages;
                     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Cache status cache total count: {$_folder->cache_totalcount} imap total count: {$_folder->imap_totalcount} messages to remove: {$messagesToRemoveFromCache}");
                     }
                     if ($messagesToRemoveFromCache <= 0) {
                         $_folder->cache_job_startuid = 0;
                         $_folder->cache_status = Felamimail_Model_Folder::CACHE_STATUS_UPDATING;
                         break;
                     }
                 }
                 if (!$this->_timeLeft()) {
                     $_folder->cache_job_startuid = $i;
                     break;
                 }
             }
             if ($firstMessageSequence === 0) {
                 $_folder->cache_status = Felamimail_Model_Folder::CACHE_STATUS_UPDATING;
             }
         }
     }
     $this->_cacheMessageSequence = $_folder->cache_totalcount;
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Cache status cache total count: {$_folder->cache_totalcount} imap total count: {$_folder->imap_totalcount} cache sequence: {$this->_cacheMessageSequence} imap sequence: {$this->_imapMessageSequence}");
     }
     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Folder cache status: ' . $_folder->cache_status);
     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Folder cache actions to be done yet: ' . ($_folder->cache_job_actions_est - $_folder->cache_job_actions_done));
 }