from telegram import Update
from requests import get as requestget
from bs4 import BeautifulSoup
from json import dumps
from random import choice
from var import bannedids, _1in100, idwords, math

class farts:
    @staticmethod
    def fart(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        if len(update.message.text.split(' ')) > 1:
            update.message.reply_text(f'{update.message.text.split(" ")[1]}\U0001f4a8\U0001f922')
            return
        update.message.reply_text('\U0001f4a8\U0001f922')

    @staticmethod
    def m_fart(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        update.message.reply_text('\U0001f98d\U0001f4a8\U0001f922')

    @staticmethod
    def p_fart(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        update.message.reply_text('\U0001f416\U0001f4a8\U0001f922')

    @staticmethod
    def c_fart(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        update.message.reply_text('\U0001f404\U0001f4a8\U0001f922')

class memes:
    @staticmethod
    def astronaut(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        update.message.reply_video(
            'BAACAgUAAxkBAAIa5GELp-WErDxwK3xdSM-m_8mV78wIAAK9BAACuKZhVsftvu1ORN8eIAQ',
            caption='\U0001f4a7\U0001f4a7\U0001f4a7\n\U0001f4a7\U0001f469'
                    '\u200d\U0001f680\U0001f4a7\n\U0001f4a7\U0001f4a7\U0001f4a7'
        )

    @staticmethod
    def board(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        update.message.reply_video(
            'BAACAgUAAxkBAAIa5WEMlqXWbDvdG0p06rkzb2O9O95HAALFAgACKTxpVO2e9wFU41P0IAQ'
        )

    @staticmethod
    def blank(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        update.message.reply_sticker('CAACAgUAAxkBAAIb_mEjHxTvnDsAAaT_QrWx1zhCVEcBxwACVgIAAkSEAVdzO-fzrqPIqSAE')

def h_secrets(update: Update, context):
    if update.message.from_user.id in bannedids:
        return
    if choice(_1in100 if update.message.from_user.id != 1341877177 else ['r', 'a']) == 'r':
        update.message.reply_animation(
            'CgACAgQAAxkBAAIa6GEM1SHzkkHrKQ_iEDaifFRuL_dFAAIvAgACTuLUUhyg_QNIOvngIAQ',
            caption='Get rickrolled xD\n'
                    'Be happy, theres only 1% chance of this happening :-)'
        )
        return
    update.message.reply_text(
        'Secret commands!!!!\n'
        '/monkefart\n/pigfart\n/cowfart\n/fart'
    )

def xue(update: Update, context):
    if update.message.from_user.id in bannedids:
        return
    update.message.reply_text('\u96ea\u82b1\u98d8\u98d8')

def searchidword(update: Update, context):
    if update.message.from_user.id in bannedids:
        return
    if len(update.message.text.split(' ')) < 2:
        update.message.reply_text('Needs parameter (startofword)\n'
                                  'Usage: /searchidword <letters>\n'
                                  'Example: /searchidword nga\n'
                                  'Bot answers with: Nganga, Ngakngikngok, Ngaben')
        return
    result = []
    start = update.message.text.split(' ')[1]
    for i in idwords:
        if i.lower().startswith(start.lower()):
            result.append(i)
        else:
            pass
    text = ''
    if len(result) == 0:
        text = f'No word starts with {start}'
    else:
        if len(result) == 1:
            text = f'<a href="https://kbbi.web.id/{result[0]}">{result[0]}</a>'
        elif len(result) == 2:
            text = f'<a href="https://kbbi.web.id/{result[0]}">{result[0]}</a>, <a href="https://kbbi.web.id/{result[1]}">{result[1]}</a>'
        else:
            res1 = choice(result)
            result.remove(res1)
            res2 = choice(result)
            result.remove(res2)
            res3 = choice(result)
            result.remove(res3)
            text = f'<a href="https://kbbi.web.id/{res1}">{res1}</a>, <a href="https://kbbi.web.id/{res2}">{res2}</a>, <a href="https://kbbi.web.id/{res3}">{res3}</a>'
    update.message.reply_text(text, parse_mode='HTML')

def defineidword(update: Update, context):
    if update.message.from_user.id in bannedids:
        return
    if len(update.message.text.split(' ')) < 2:
        update.message.reply_text('Needs parameter (word)\n'
                                  'Usage: /defineidword <word>\n'
                                  'Example: /defineidword lalat')
        return
    msg = update.message.reply_text('Please wait...')
    word = update.message.text.split(' ')[1]
    res = requestget(f'https://kbbi.web.id/{word}')
    res = BeautifulSoup(res.content, 'html.parser')
    div = res.find('div', {'id': 'd1'})
    if div.text == '\n':
        text = '<b>No word found.</b>\n\n<a href="https://kbbi.web.id/">KBBI</a>'
    else:
        def_ = div.contents[5][1:]
        try:
            if div.contents[6].name == 'em':
                def_ += div.contents[6].text
        except:
            pass
        text = f'<b><a href="{res.find("a", class_="link-kata").text}">{div.b.contents[0]}</a></b> ' \
               f'<u><i>{div.find("em").text}</i></u>\n' \
               f'<i>{def_}</i>'
        if word == 'sus':
            text += '\n\n<i>This cake is indeed very <b><u><a href="https://www.urbandictionary.com/define.php?term=sus">SUS</a></u></b></i>'
        if word == 'penipu':
            text += '\n\n<i>Did you mean <b><u><a href="https://www.urbandictionary.com/define.php?term=imposter">Imposter</a></u></b>?</i>'
    msg.edit_text(text, parse_mode='HTML')

class math_:
    @staticmethod
    def sqrt(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        try:
            num = float(update.message.text.split(' ')[1])
        except ValueError:
            update.message.reply_text('Parameter must only contain numbers and .(dot).')
            return
        except IndexError:
            update.message.reply_text('Usage: /sqrt <num>\nExample: /sqrt 25')
            return
        update.message.reply_text('Square Root Calculation\n'
                                  f'\u221a{num} = {math.sqrt(num)}')

    @staticmethod
    def cbrt(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        try:
            num = float(update.message.text.split(' ')[1])
        except ValueError:
            update.message.reply_text('Parameter must only contain numbers and .(dot).')
            return
        except IndexError:
            update.message.reply_text('Usage: /cbrt <num>\nExample: /cbrt 125')
            return
        update.message.reply_text('Cube Root Calculation\n'
                                  f'\u221b{num} = {math.cbrt(num)}')

class tod:
    @staticmethod
    def init(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        update.message.reply_text(choice(tod[choice(['truth', 'dare'])]))

    @staticmethod
    def truth(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        update.message.reply_text(choice(tod['truth']))

    @staticmethod
    def dare(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        update.message.reply_text(choice(tod['dare']))

    @staticmethod
    def truth_add(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        if update.message.from_user.id != 1341877177:
            update.message.reply_text('You cant use dis XD')
            return
        truth = ' '.join(update.message.text.split(' ')[1:])
        tod['truth'].append(truth)
        update.message.reply_text(truth)
        with open('tod.json', 'w') as f:
            f.write(dumps(tod, indent=2))

    @staticmethod
    def dare_add(update: Update, context):
        if update.message.from_user.id in bannedids:
            return
        if update.message.from_user.id != 1341877177:
            update.message.reply_text('You cant use dis XD')
            return
        dare = ' '.join(update.message.text.split(' ')[1:])
        tod['dare'].append(dare)
        update.message.reply_text(dare)
        with open('tod.json', 'w') as f:
            f.write(dumps(tod, indent=2))