# import sleekxmpp
# import json
# import sys


# class MyBot(sleekxmpp.ClientXMPP):
#     def __init__(self, jid, password):
#         sleekxmpp.ClientXMPP.__init__(self, jid, password)

#         self.add_event_handler("session_start", self.start)
#         self.add_event_handler("message", self.message)

#         self.requests = []

#     def start(self, event):
#         self.send_presence()
#         self.get_roster()

#     def message(self, msg):
#         if msg['type'] in ('chat', 'normal'):
#             body = msg['body']
#             if body.startswith('name:'):
#                 name = body.split(':')[1].strip()
#                 self.requests.append({'name': name})
#                 msg.reply('Name saved.').send()
#             elif body.startswith('email:'):
#                 email = body.split(':')[1].strip()
#                 self.requests[-1]['email'] = email
#                 msg.reply('Email saved.').send()
#             elif body.startswith('message:'):
#                 message = body.split(':')[1].strip()
#                 self.requests[-1]['message'] = message
#                 msg.reply('Message saved.').send()
#             elif body == 'submit':
#                 with open('requests.json', 'w') as f:
#                     json.dump(self.requests, f, ensure_ascii=False)
#                 msg.reply('Request submitted.').send()

# if __name__ == '__main__':
#     print('bb')
#     xmpp = MyBot('petrnebot@jabb.im', '.zcCM!bvt!3xL!Y')
#     xmpp.connect()
#     xmpp.process(block=True)

import sleekxmpp
import json
import sys
import time
import asyncio


class MyBot(sleekxmpp.ClientXMPP):
    def __init__(self, jid, password):
        sleekxmpp.ClientXMPP.__init__(self, jid, password)

        self.add_event_handler("session_start", self.start)
        self.add_event_handler("message", self.message)

        self.requests = []
        self.last_message_time = time.time()

    def start(self, event):
        self.send_presence()
        self.get_roster()

    def message(self, msg):
        if msg['type'] in ('chat', 'normal'):
            self.last_message_time = time.time()
            body = msg['body']
            if body.startswith('name:'):
                name = body.split(':')[1].strip()
                self.requests.append({'name': name})
                msg.reply('Name saved.').send()
            elif body.startswith('email:'):
                email = body.split(':')[1].strip()
                self.requests[-1]['email'] = email
                msg.reply('Email saved.').send()
            elif body.startswith('message:'):
                message = body.split(':')[1].strip()
                self.requests[-1]['message'] = message
                msg.reply('Message saved.').send()
            elif body == 'submit':
                with open('requests.json', 'w') as f:
                    json.dump(self.requests, f, ensure_ascii=False)
                msg.reply('Request submitted.').send()

    # async def check_response(self):
    #     while True:
    #         if time.time() - self.last_message_time > 10:
    #             # self.send_message(mto=self.boundjid.bare, mbody='Вы где?')
    #             # msg.reply('Message saved.').send()
    #             jid = self.requests[-1]['jid']
    #             self.send_message(mto=jid, mbody='Вы где?')
    #             msg.reply('Message saved.').send()
    #         await asyncio.sleep(60)

    async def check_response(self):
        while True:
            if time.time() - self.last_message_time > 10:
                self.send_message(mto=self.boundjid.bare, mbody='Вы где?')
            await asyncio.sleep(60)

if __name__ == '__main__':
    xmpp = MyBot('petrnebot@jabb.im', '.zcCM!bvt!3xL!Y')
    xmpp.connect()
    xmpp.process(forever=False)

    loop = asyncio.get_event_loop()
    loop.create_task(xmpp.check_response())
    loop.run_forever()