# Copyright (C) 2019 The Raphielscape Company LLC.
#
# Licensed under the Raphielscape Public License, Version 1.c (the "License");
# you may not use this file except in compliance with the License.
#

# Asena UserBot - Yusuf Usta

# Turkish word meaning. Only Turkish. Coded @By_Azade, Asena uyarlamasƒ± @qulec
#

import requests

import aiohttp
import asyncio
import json

from userbot import CMD_HELP
from userbot.events import register
from bs4 import BeautifulSoup
import os
from json import loads
from userbot.cmdhelp import CmdHelp

def searchTureng_tr(word):
    url="https://tureng.com/tr/turkce-ingilizce/"+word
    try:
        answer =  requests.get(url)
    except:
        return "No connection"
    soup = BeautifulSoup(answer.content, 'html.parser')
    trlated='{} Kelimesinin Anlamƒ±/Anlamlarƒ±:\n\n'.format(word)
    try:
        table = soup.find('table')
        td = table.find_all('td', attrs={'lang':'en'})
        # print(td)
        for val in td[0:5]:
            trlated = '{}üëâ  {}\n'.format(trlated , val.text )
        return trlated
    except:
        return "Sonu√ß bulunamadƒ±"

@register(outgoing=True, pattern="^.tureng ?(.*)")
async def tureng(event): 
    input_str = event.pattern_match.group(1)
    result = searchTureng_tr(input_str)
    await event.edit(result)

def getSimilarWords(kelime, limit = 5):
    benzerler = []
    if not os.path.exists('autocomplete.json'):
        words = requests.get(f'https://sozluk.gov.tr/autocomplete.json')
        open('autocomplete.json', 'a+').write(words.text)
        words = words.json()
    else:
        words = loads(open('autocomplete.json', 'r').read())

    for word in words:
        if word['madde'].startswith(kelime) and not word['madde'] == kelime:
            if len(benzerler) > limit:
                break
            benzerler.append(word['madde'])
    benzerlerStr = ""
    for benzer in benzerler:
        if not benzerlerStr == "":
            benzerlerStr += ", "
        benzerlerStr += f"`{benzer}`"
    return benzerlerStr
    
@register(outgoing=True, pattern="^.tdk ?(.*)")
async def tdk(event): 
    inp = event.pattern_match.group(1)
    await event.edit('**Bekle!**\n__S√∂zl√ºkte arƒ±yorum...__')
    async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
        async with session.get('https://open-apis-rest.up.railway.app/api/tdk?word=' + str(inp)) as response:

            html = await response.text()
            html2 = json.loads(html)
            if html2["status"] == "OK":
                SAVE = "./soz.mp3"
                resp = requests.get(html2["data"]["ses"])
                with open(SAVE, "wb") as f:
                    f.write(resp.content)

                song = await event.client.send_file(event.chat_id, "./soz.mp3", voice_note=True)
                await event.client.send_message(event.chat_id, "**Kelime:** " + "__" + inp + "__" + "\n**Anlamƒ±:** " + "__" + html2["data"]["anlam"] + "__" + "\n**Atas√∂z√º:** " + "__" + html2["data"]["atasozu"] + "__", reply_to=song, link_preview=False)
                await event.delete()
            else:
                return await event.edit(f'__Kelimeniz({inp}) B√ºy√ºk T√ºrk√ße S√∂zl√ºk\'te Bulunamadƒ±!__')

@register(outgoing=True, pattern="^.tdk2 ?(.*)")
async def tdk(event): 
    inp = event.pattern_match.group(1)
    await event.edit('**Bekle!**\n__S√∂zl√ºkte arƒ±yorum...__')
    response = requests.get(f'https://sozluk.gov.tr/gts?ara={inp}').json()
    if 'error' in response:
        await event.edit(f'**Kelimeniz({inp}) B√ºy√ºk T√ºrk√ße S√∂zl√ºk\'te Bulunamadƒ±!**')
        words = getSimilarWords(inp)
        if not words == '':
            return await event.edit(f'__Kelimeniz({inp}) B√ºy√ºk T√ºrk√ße S√∂zl√ºk\'te Bulunamadƒ±!__\n\n**Benzer Kelimeler:** {words}')
    else:
        anlamlarStr = ""
        for anlam in response[0]["anlamlarListe"]:
            anlamlarStr += f"\n**{anlam['anlam_sira']}.**"
            if ('ozelliklerListe' in anlam) and ((not anlam["ozelliklerListe"][0]["tam_adi"] == None) or (not anlam["ozelliklerListe"][0]["tam_adi"] == '')):
                anlamlarStr += f"__({anlam['ozelliklerListe'][0]['tam_adi']})__"
            anlamlarStr += f' ```{anlam["anlam"]}```'

            if response[0]["cogul_mu"] == '0':
                cogul = '‚ùå'
            else:
                cogul = '‚úÖ'
            
            if response[0]["ozel_mi"] == '0':
                ozel = '‚ùå'
            else:
                ozel = '‚úÖ'


        await event.edit(f'**Kelime:** `{inp}`\n\n**√áoƒüul Mu:** {cogul}\n**√ñzel Mi:** {ozel}\n\n**Anlamlar:**{anlamlarStr}')
        words = getSimilarWords(inp)
        if not words == '':
            return await event.edit(f'**Kelime:** `{inp}`\n\n**√áoƒüul Mu:** `{cogul}`\n**√ñzel Mi:** {ozel}\n\n**Anlamlar:**{anlamlarStr}' + f'\n\n**Benzer Kelimeler:** {words}')


CmdHelp('sozluk').add_command(
    'tdk', '<kelime>', 'Verdiƒüiniz kelimeyi TDK S√∂zl√ºkte arar.'
).add_command(
    'tdk2', '<kelime>', 'Kelimeyi TDK S√∂zl√ºkte arar. (Daha Kapsamlƒ±)'
).add_command(
    'tureng', '<kelime>', 'Verdiƒüiniz kelimeyi Tureng S√∂zl√ºkte arar.'
).add()