from os import times
from telnetlib import NOP
import time
import disnake
import datetime
import botOptions

class TimedAttack:
    def __init__(self,owner,cID,tID,duration,name):
        self.attackers = {}
        self.mID = 0
        self.cID = cID
        self.tID = tID
        self.duration = duration
        self.name = name
        self.owner = owner
        self.mark = 0
        self.longestAttack = 0
        self.leadIn = 10
        self.attacks = 0
        self.sendt = 0
        self.color = 0xffff00
    
    def setMessageID(self,id):
        self.mID = id

    def setMark(self,time):
        self.color = 0x00ff00
        self.mark = time

    def addAttack(self,owner,time,name):
        self.attacks += 1
        if time > self.longestAttack: self.longestAttack = time
        if owner in self.attackers.keys():
            #Attacker adding more attacks
            self.attackers[owner].append((time,name))
            self.attackers[owner].sort(key=lambda y: y[0],reverse=True)
        else:
            attacklist = []
            attacklist.append((time,name))
            self.attackers[owner] = attacklist
        return
    
    def isOver(self):
        if self.mark == 0: return False
        if time.time() > self.longestAttack+self.mark+self.leadIn:
            return True
        return False
    
    def needsUpdate(self):
        check = 0
        for attacks in self.attackers.values():
            for attack in attacks:
                ttl = int(self.longestAttack - attack[0] + self.leadIn + self.mark)
                if int(time.time()) > ttl: 
                    check += 1
        if check > self.sendt:
            return True
        return False
        
    def generateEmbed(self):
        if self.isOver():
            self.color = 0xff0000
        self.sendt = 0

        #timeString = str(datetime.timedelta(seconds=self.duration))
        attackString = ""
        if self.mark > 0:
            attackString = "\nLanding in: <t:{t}:R>".format(t=int(self.longestAttack + self.leadIn + self.mark))
        embed = disnake.Embed(title="Timed attack on ID: {targetID} - Longest attack: {t} {cd}".format(cd = attackString, targetID = self.tID,owner = self.owner,t=str(datetime.timedelta(seconds=self.longestAttack)))\
            ,description="To participate add your times using: \n {prefix}timed {tID} <Time to Land> <Name of Attack> \n\
            Once everything is added, click the green timer reaction to start the countdowns.\n {leadin} seconds is added to the timers as a lead in.\n**Confused? use  {prefix}timed help**"\
            .format(prefix=botOptions.prefix,owner=self.owner,tID=self.tID,leadin=self.leadIn),color = self.color)
        #If theres attackers, add them.
        for name, attacks in self.attackers.items():

            valString = ""
            for attack in attacks:
                if self.mark > 0:
                    #long Time - time + leadin + mark
                    ttl = int(self.longestAttack - attack[0] + self.leadIn + self.mark)
                    if int(time.time()) > ttl: 
                        self.sendt += 1
                        valString += '**{name}**: Sendt\n'.format(name=attack[1])
                        continue
                    valString += '**{name}**: <t:{t}:R>\n'.format(t=ttl,name=attack[1])
                else:
                    valString += '**{name}**: {t}\n'.format(t=str(datetime.timedelta(seconds=attack[0])),name=attack[1])
            embed.add_field(name = "__{name}__".format(name = name),value = valString,inline=True)
            #embed.add_field(name = 'Strix',value='VilA:<t:{t1}:R>\nVilA:<t:{t2}:R>'.format(t1=int(time.time()+random.random()*360),t2=int(time.time()+random.random()*360)),inline=True)


        embed.set_author(name='Storm Brigade',icon_url='https://media.disnakeapp.net/attachments/947633992688103424/947805799231660032/StormBrigade_White.png')
        return embed


