import discord, aiohttp, re
from bs4 import BeautifulSoup as b_s
from io import BytesIO
from discord.ext import commands
import os
import random
from random import choice
import lxml    

class retrosign:
    def __init__(self, bot):
      self.bot = bot  
    
    @commands.command(name="retrosign")
    async def retrosign(self, *, content : str):
        """Make a retrosign with 3 words seperated by space or one word in the middle"""
        texts = [t.strip() for t in content.split()]
        await self.bot.say(len(texts))
        if len(texts) < 3 and len(texts) > 1:
            lenstr = len(texts[0])
            await self.bot.say(lenstr)
            if lenstr <= 12:
                    global data
                    data = dict(
                      bcg=choice([1, 2, 3, 4, 5]),
                      txt=choice([1, 2, 3, 4]),
                      text1="",
                      text2=texts[0],
                      text3=""
                    )
                    await self.bot.type() 
                    with aiohttp.ClientSession() as session:
                      async with session.post("http://photofunia.com/effects/retro-wave", data=data) as response:
                        if response.status == 200:
                          soup = b_s(await response.text(), "lxml")
                          download_url = soup.find("div", class_="downloads-container").ul.li.a["href"]
                          async with session.get(download_url) as image_response:
                            if image_response.status == 200:
                              image_data = await image_response.read()
                              with BytesIO(image_data) as temp_image:
                                await self.bot.upload(temp_image, filename="retro.jpg")
            else:
                await self.bot.say("\N{CROSS MARK} too many characters for one line")
                return
        elif len(texts) != 3:
            await self.bot.say("\N{CROSS MARK} please provide three words seperated by space or one word")
            return
        else:
            global data
            data = dict(
              bcg=choice([1, 2, 3, 4, 5]),
              txt=choice([1, 2, 3, 4]),
              text1=texts[0],
              text2=texts[1],
              text3=texts[2]
            )
            await self.bot.type() 
            with aiohttp.ClientSession() as session:
              async with session.post("http://photofunia.com/effects/retro-wave", data=data) as response:
                if response.status == 200:
                  soup = b_s(await response.text(), "lxml")
                  download_url = soup.find("div", class_="downloads-container").ul.li.a["href"]
                  async with session.get(download_url) as image_response:
                    if image_response.status == 200:
                      image_data = await image_response.read()
                      with BytesIO(image_data) as temp_image:
                        await self.bot.upload(temp_image, filename="retro.jpg")
        
        
                    
    @commands.command(name="retrotopsign")
    async def _top_(self, *, content : str):
        """Make a retrosign with top and middle text"""
        texts = [t.strip() for t in content.split()]
        if len(texts) != 2:
            await self.bot.say("\N{CROSS MARK} please provide two words seperated by space")
            return
        else:
            global data
            data = dict(
              bcg=choice([1, 2, 3, 4, 5]),
              txt=choice([1, 2, 3, 4]),
              text1=texts[0],
              text2=texts[1],
              text3=""
            )
            await self.bot.type() 
            with aiohttp.ClientSession() as session:
              async with session.post("http://photofunia.com/effects/retro-wave", data=data) as response:
                if response.status == 200:
                  soup = b_s(await response.text(), "lxml")
                  download_url = soup.find("div", class_="downloads-container").ul.li.a["href"]
                  async with session.get(download_url) as image_response:
                    if image_response.status == 200:
                      image_data = await image_response.read()
                      with BytesIO(image_data) as temp_image:
                        await self.bot.upload(temp_image, filename="retro.jpg")
            
    @commands.command(name="retrobottomsign")
    async def _bottom_(self, *, content : str):
        """Make a retrosign with middle and bottom text"""
        texts = [t.strip() for t in content.split()]
        if len(texts) != 2:
            await self.bot.say("\N{CROSS MARK} please provide two words seperated by space")
            return
        else:
            global data
            data = dict(
              bcg=choice([1, 2, 3, 4, 5]),
              txt=choice([1, 2, 3, 4]),
              text1="",
              text2=texts[0],
              text3=texts[1]
            )
            await self.bot.type() 
            with aiohttp.ClientSession() as session:
              async with session.post("http://photofunia.com/effects/retro-wave", data=data) as response:
                if response.status == 200:
                  soup = b_s(await response.text(), "lxml")
                  download_url = soup.find("div", class_="downloads-container").ul.li.a["href"]
                  async with session.get(download_url) as image_response:
                    if image_response.status == 200:
                      image_data = await image_response.read()
                      with BytesIO(image_data) as temp_image:
                        await self.bot.upload(temp_image, filename="retro.jpg")
                  
def setup(bot):
  n = retrosign(bot)
  bot.add_cog(n)

@@ -555,16 +555,20 @@
 %3C 3 and 
+not 
 len(text
@@ -575,16 +575,16 @@
 s) %3E 1:%0A
-
         
@@ -682,40 +682,8 @@
 12:%0A
-                    global data%0A
     
@@ -2047,32 +2047,8 @@
 se:%0A
-            global data%0A
     
@@ -3373,32 +3373,8 @@
 se:%0A
-            global data%0A
     
@@ -4650,32 +4650,32 @@
          return%0A
+
         else:%0A  
@@ -4676,32 +4676,8 @@
 se:%0A
-            global data%0A
     

Update retrosign.py