def __ProcessShowAll(self, pageNumber: int):
        limit = 5
        offset = limit * (pageNumber - 1)
        allReqs = self.__db.GetAllRequests(self.username, offset, limit)
        if len(allReqs) == 0:
            self.__bot.send_message(
                self.__chatId,
                ld.get_translate(self.__db, self.username, ld.EmptyKey))
            return
        for req in allReqs:
            keyboard = InlineKeyboardMarkup(row_width=1)
            buttonTextKey = ld.AcceptSellRequestKey if db.RequestType(
                req[2]) == db.RequestType.Sell else ld.AcceptBuyRequestKey
            keyboard.row(
                InlineKeyboardButton(ld.get_translate(self.__db, self.username,
                                                      buttonTextKey),
                                     callback_data="{0}{1}".format(
                                         buttonTextKey, req[0])))
            formattedReq = self.__getFormattedRequest(req)
            if str(req[1]) == self.username:
                keyboard = None
            reply = self.__bot.send_message(self.__chatId,
                                            formattedReq,
                                            parse_mode="HTML",
                                            reply_markup=keyboard)
            self.__allReqMsgIds.append(reply.message_id)
        reqCount = self.__db.GetAllRequestsCount()
        showPrevButton = pageNumber > 1
        showNextButton = (reqCount - (limit + limit * (pageNumber - 1))) > 0

        keyboard = None
        if showNextButton or showPrevButton:
            keyboard = ReplyKeyboardMarkup(row_width=2,
                                           resize_keyboard=True,
                                           one_time_keyboard=True)
            buttons = []
            if showPrevButton:
                buttons.append(KeyboardButton("⬅️"))
            if showNextButton:
                buttons.append(KeyboardButton("➡️"))
            keyboard.add(*buttons)
            self.__isKeyboardActive = True
        maxPageNumber = reqCount // limit
        if reqCount % limit > 0:
            maxPageNumber += 1
        reply = self.__bot.send_message(self.__chatId,
                                        "Page {0} of {1}".format(
                                            pageNumber, maxPageNumber),
                                        parse_mode="HTML",
                                        reply_markup=keyboard)
        self.__allReqMsgIds.append(reply.message_id)
 def __getFormattedRequest(self, req: tuple):
     number = req[0]
     username = req[1]
     reqType = self.__getLocalizedRequestType(db.RequestType(req[2]),
                                              self.username)
     quantity = req[3]
     currency = req[4]
     fee = str(req[6]).replace(",", ".")
     bank = req[5]
     startDate = req[7]
     endDate = req[8]
     whoPayFee = ""
     if float(fee) > 0:
         whoPayFee = ld.get_translate(self.__db, self.username,
                                      ld.FeePayBuyerKey)
     elif float(fee) < 0:
         whoPayFee = ld.get_translate(self.__db, self.username,
                                      ld.FeePaySellerKey)
     req = ld.get_translate(self.__db, self.username,
                            ld.RequestResultStringTemplate).format(
                                number, username, reqType, quantity,
                                currency, fee, whoPayFee, bank, startDate,
                                endDate)
     return req
    def __ProcessStartState(self, msg: str):
        self.__deleteAllReqMessages()
        if msg == ld.SellKey:
            print(self.username + " Sell")
            self.__deleteAllReqKeyboard()
            self.__reqType = db.RequestType.Sell
            self.currentStep = RequestSteps.EnterCurrency
            self.__deleteStartMessage()
            assets = self.__db.GetAssetsList()
            keyboard = self.__GetMarkupForAssetList(assets)
            reply = self.__bot.send_message(self.__chatId,
                                            ld.get_translate(
                                                self.__db, self.username,
                                                ld.SellingMsgKey),
                                            parse_mode="HTML",
                                            reply_markup=keyboard)
            self.__processMsgId = reply.message_id
        elif msg == ld.BuyKey:
            print(self.username + " Buy")
            self.__deleteAllReqKeyboard()
            self.__reqType = db.RequestType.Buy
            self.currentStep = RequestSteps.EnterCurrency
            self.__deleteStartMessage()
            assets = self.__db.GetAssetsList()
            keyboard = self.__GetMarkupForAssetList(assets)
            reply = self.__bot.send_message(self.__chatId,
                                            ld.get_translate(
                                                self.__db, self.username,
                                                ld.BuyingMsgKey),
                                            parse_mode="HTML",
                                            reply_markup=keyboard)
            self.__processMsgId = reply.message_id
        elif ld.RemoveKey in msg:
            parseResult = self.__ParseReqId(msg)
            if not parseResult[0]:
                self.__bot.send_message(
                    self.__chatId,
                    ld.get_translate(self.__db, self.username,
                                     ld.RemoveErrorKey))
                return
            else:
                print(self.username + " are removing request #" +
                      str(parseResult[1]))
                self.__db.DeleteReqWithId(parseResult[1])
                self.__bot.send_message(
                    self.__chatId,
                    ld.get_translate(self.__db, self.username,
                                     ld.RemoveSuccessKey).format(
                                         parseResult[1]))
        elif ld.ChangeKey in msg:
            parseResult = self.__ParseReqId(msg)
            if not parseResult[0]:
                self.__bot.send_message(
                    self.__chatId,
                    ld.get_translate(self.__db, self.username,
                                     ld.ChangeErrorKey))
                return
            else:
                print(self.username + " are changing request #" +
                      str(parseResult[1]))
                self.currentStep = RequestSteps.ChangeCurrency
                self.__reqIdForUpdate = parseResult[1]
                req = self.__db.GetRawRequest(parseResult[1])
                self.__reqType = db.RequestType(req[2])
                self.__quantity = str(req[3])
                self.__currency = req[4]
                self.__fee = float(req[6])
                self.__bank = req[5]
                self.__startDate = datetime.strptime(req[7], "%d.%m.%Y")
                self.__endDate = datetime.strptime(req[8], "%d.%m.%Y")
                self.__daysQuantity = (self.__endDate - self.__startDate).days
                self.__deleteStartMessage()
                assets = self.__db.GetAssetsList()
                keyboard = self.__GetMarkupForAssetList(assets, True)
                reply = self.__bot.send_message(self.__chatId,
                                                ld.get_translate(
                                                    self.__db, self.username,
                                                    ld.ChangingMsgKey),
                                                parse_mode="HTML",
                                                reply_markup=keyboard)
                self.__processMsgId = reply.message_id
        elif msg == ld.ShowMyReqKey:
            print(self.username + " are browsing his requests")
            self.__deleteAllReqKeyboard()
            self.__ProcessShowMy()
        elif msg == ld.ShowAllReqKey:
            print(self.username + " are browsing all requests")
            self.__currentPage = 1
            self.__ProcessShowAll(self.__currentPage)
        elif msg == "➡️":
            self.__currentPage += 1
            self.__ProcessShowAll(self.__currentPage)
        elif msg == "⬅️":
            self.__currentPage -= 1
            if (self.__currentPage < 1):
                self.__currentPage = 1
            self.__ProcessShowAll(self.__currentPage)
        elif msg == ld.VoteKey:
            self.__deleteAllReqKeyboard()
            if (db.DB.MaxVotes - self.__db.GetVotesCount(self.username)) <= 0:
                self.__bot.send_message(
                    self.__chatId,
                    ld.get_translate(self.__db, self.username,
                                     ld.ZeroVotesKey))
                return
            self.currentStep = RequestSteps.VoteUser
            keyboard = InlineKeyboardMarkup(row_width=1)
            keyboard.row(
                InlineKeyboardButton(ld.get_translate(self.__db, self.username,
                                                      ld.CancelKey),
                                     callback_data=ld.CancelKey))
            self.__bot.send_message(self.__chatId,
                                    ld.get_translate(self.__db, self.username,
                                                     ld.VotingMsgKey),
                                    parse_mode="HTML",
                                    reply_markup=keyboard)
        elif msg == ld.UnvoteKey:
            self.__deleteAllReqKeyboard()
            votedUsersList = self.__db.GetMyVotedUsers(self.username)
            if len(votedUsersList) == 0:
                self.__bot.send_message(
                    self.__chatId,
                    ld.get_translate(self.__db, self.username,
                                     ld.VoteListEmptyKey))
                return
            self.currentStep = RequestSteps.UnvoteUser
            reply = self.__bot.send_message(
                self.__chatId,
                "<b>{0}</b>".format(
                    ld.get_translate(self.__db, self.username, ld.UnvoteKey)),
                parse_mode="HTML",
                reply_markup=self.__GetMarkupForUnvote(votedUsersList))
            self.__unvoteMsgId = reply.message_id
        elif msg == ld.EscrowListKey:
            self.__deleteAllReqKeyboard()
            escrowList = self.__db.GetEscrowList()
            if len(escrowList) == 0:
                self.__bot.send_message(
                    self.__chatId,
                    ld.get_translate(self.__db, self.username, ld.EmptyKey))
                return
            result = "\n".join(escrowList)
            self.__bot.send_message(self.__chatId, result, parse_mode="HTML")
        elif msg == ld.DisableNotifKey:
            self.__deleteAllReqKeyboard()
            print(self.username + " has been disabled notifications")
            self.__db.DeleteUserFromNotifications(self.username)
            self.__bot.send_message(
                self.__chatId,
                ld.get_translate(self.__db, self.username,
                                 ld.NotificationsDisabledKey))
            self.Start()
        elif msg == ld.EnableNotifKey:
            self.__deleteAllReqKeyboard()
            print(self.username + " has been enabled notifications")
            if not self.__db.IsNotificationsRowExistForUser(self.username):
                self.__db.AddUserForNotifications(self.username, self.__chatId)
            self.__bot.send_message(
                self.__chatId,
                ld.get_translate(self.__db, self.username,
                                 ld.NotificationsEnabledKey))
            self.Start()
        elif msg == ld.EnglishKey:
            self.__deleteAllReqKeyboard()
            self.__db.SetUserLanguage(self.username, ld.Languages.English)
            self.Start()
        elif msg == ld.RussianKey:
            self.__deleteAllReqKeyboard()
            self.__db.SetUserLanguage(self.username, ld.Languages.Russian)
            self.Start()
        elif msg.startswith(ld.AcceptBuyRequestKey) or msg.startswith(
                ld.AcceptSellRequestKey):
            try:
                msg = msg.replace(ld.AcceptBuyRequestKey, "")
                reqNum = int(msg.replace(ld.AcceptSellRequestKey, ""))
                if self.__db.IsRequestProcessing(reqNum):
                    self.__bot.send_message(
                        self.__chatId,
                        ld.get_translate(self.__db, self.username,
                                         ld.RequestAlreadyAcceptedKey))
                    return
                req = self.__db.GetRawRequest(reqNum)
                reqType = db.RequestType(req[2])
                buyer = self.username if reqType == db.RequestType.Sell else req[
                    1]
                seller = req[
                    1] if reqType == db.RequestType.Sell else self.username
                self.__db.AddProcessingRequest(reqNum, seller, buyer)
                reqUserChatId = self.__db.GetUserChatId(req[1])
                keyboard = InlineKeyboardMarkup(row_width=1)
                keyboard.row(
                    InlineKeyboardButton(
                        ld.get_translate(self.__db, self.username,
                                         ld.AcceptKey),
                        callback_data="{0}{1}".format(ld.AcceptKey, reqNum)))
                print("{0}({1}) sent accept message to user {2}({3})".format(
                    self.username, self.__chatId, req[1], reqUserChatId))
                self.__bot.send_message(reqUserChatId,
                                        ld.get_translate(
                                            self.__db, req[1],
                                            ld.RequestWasAcceptedKey).format(
                                                reqNum, self.username),
                                        reply_markup=keyboard)
                self.__bot.send_message(
                    self.__chatId,
                    ld.get_translate(self.__db, self.username,
                                     ld.RequestWasSentKey))
                self.__StartRequestAcceptTimer(reqNum)
            except Exception as ex:
                print("Exception during accepting request: " + str(ex))

        elif msg.startswith(ld.AcceptKey):
            try:
                print("{0} trying to accept request {1}".format(
                    self.username, msg))
                reqNum = int(msg.replace(ld.AcceptKey, ""))
                processingReq = self.__db.GetProcessingRequest(reqNum)
                if len(processingReq) == 0:
                    print("Request is no longer exists")
                    self.__bot.send_message(
                        self.__chatId,
                        ld.get_translate(self.__db, self.username,
                                         ld.AcceptRequestNoLongerActiveKey))
                    return
                seller = processingReq[1]
                buyer = processingReq[2]
                sellerChatId = self.__db.GetUserChatId(seller)
                buyerChatId = self.__db.GetUserChatId(buyer)
                print("Send Finish Accept message to user {0}, chatId {1}".
                      format(seller, sellerChatId))
                self.__bot.send_message(
                    sellerChatId,
                    ld.get_translate(
                        self.__db, seller,
                        ld.RequestHasBeenAcceptedBothSidesKey).format(
                            reqNum, buyer))
                print("Send Finish Accept message to user {0}, chatId {1}".
                      format(buyer, buyerChatId))
                self.__bot.send_message(
                    buyerChatId,
                    ld.get_translate(
                        self.__db, buyer,
                        ld.RequestHasBeenAcceptedBothSidesKey).format(
                            reqNum, seller))
                self.__DeleteProcessingRequest(reqNum)
                self.__db.DeleteReqWithId(reqNum)
            except:
                print("Exception during accepting request")