import requests
from config import TELEGRAM_SEND_MESSAGE_URL, TELEGRAM_SEND_PHOTO_URL, TELEGRAM_PIC_URL, TELEGRAM_PIC_jURL
import replies
import STATES
from saucefinder import saucefinder



class TelegramBot:

    def __init__(self):
        self.chat_id = None
        self.text = None
        self.first_name = None
        self.last_name = None
        self.raw = None

    def parse_webhook_data(self, data):
        message = data['message']

        self.chat_id = message['chat']['id']
        try:
            self.incoming_message_text = message['text'].lower()
        except:
            self.incoming_message_text = None
        self.first_name = message['from']['first_name']
        self.last_name = message['from']['last_name']
        self.raw = data

    def action(self):

        success = None

        if STATES.state == None and self.incoming_message_text == '/start':
            self.outgoing_message_text = 'Hewo!,you can do some cool stuff with this bot like\n\n' + replies.note
            success = self.send_message()

        elif STATES.state == None and self.incoming_message_text == '/notes':
            self.outgoing_message_text = replies.note
            success = self.send_message()

        elif STATES.state == None and self.incoming_message_text == '/hello':
            self.outgoing_message_text = replies.welcome(self.first_name)
            success = self.send_message()

        elif STATES.state == None and self.incoming_message_text == '/developer':
            self.outgoing_message_text = 'Developer doesnt matter bro, the sauce matters\nyou find the sauce, the developer is happy'
            success = self.send_message()

        #
        elif STATES.state == None and self.incoming_message_text == '/findsauce':
            STATES.state = 'sauce'
            self.outgoing_message_text = "Send a pic and i'll try my best to find the sauce :)\n--i can only find if its available on nhentai, otherwise i'll help you one step closer in finding it--\ndont send irrelevent pics, my feelings are hurt\n(pls dont kill me if i fail)"
            success = self.send_message()

        elif STATES.state == 'sauce':
            recv = False
            try:
                fileid = self.raw['message']['photo'][len(self.raw['message']['photo']) - 1]['file_id']
                recv = True
            except:
                recv = False
                self.outgoing_message_text = 'Invalid image or no image sent pls try again'

            if recv == False:
                success = self.send_message()
            else:
                file_path = requests.get(TELEGRAM_PIC_jURL + fileid)
                file_path = file_path.json()
                path = file_path['result']['file_path']
                imgurl = TELEGRAM_PIC_URL + path
                sauce = saucefinder(imgurl)
                if sauce[0] == True:
                    self.outgoing_message_text = 'Sauce: {}\n\nLink: {}'.format(sauce[1][0], sauce[1][1])
                    success = self.send_message()

                elif sauce[0] == 'yes':
                    text = ''
                    for j, i in enumerate(sauce[1]):
                        text += str(j) + '. ' + i[0] + '\n' + i[1] + '\n\n'
                    self.outgoing_message_text = 'possible sauces: \n{}'.format(text)
                    success = self.send_message()
                else:
                    self.outgoing_message_text = "Cant find it :(\nIt wont find it if it isn't available on nhentai or if the image is too cropped\nthis is the best i can do:\n\n{}".format(
                        sauce[1])
                    success = self.send_message()
            STATES.state = None

        else:
            self.outgoing_message_text = 'Invalid command!, pls enter valid commands'
            success = self.send_message()
        return success

    def send_message(self):
        res = requests.get(TELEGRAM_SEND_MESSAGE_URL.format(self.chat_id, self.outgoing_message_text))

        return True if res.status_code == 200 else False

    def send_photo(self):
        res = requests.get(TELEGRAM_SEND_PHOTO_URL.format(self.chat_id, self.outgoing_photo, self.caption))
        return True if res.status_code == 200 else False

    @staticmethod
    def init_webhook(url):
        requests.get(url)


