#!/usr/bin/env python
"""
File that implements DARE dice-system for my tabletop game
"""
import dice
import codecs

class SpecialDice(dice.Dice):
    def __init__(self, n, name):
        """init a special dice"""
        super().__init__(n)
        self.name=str(name)
        return None
    
    def __str__(self):
        if self.value != None:
            return self.name+str(self.n)+"["+str(self.value)+"]"
        else:
            return self.name+str(self.n)



    def __repr__(self):
        return "SDice object : "+str(self)

    def __lt__(self, otherdice):
        return self.n <= otherdice.n

class LatencyQueue():
    def __init__(self):
        """init the Latency Queue"""
        self.oneQueue = [] 
        self.twoQueue = []
    
    def add(elt, index):
        """put elt in the indexQueue"""
        if index == 1:
            self.oneQueue.append(elt)
            return None
        elif index == 2:
            self.oneQueue.append(elt)
            return None
        else:
            return None
    
    def move(self):
        ret = self.oneQueue
        self.oneQueue = self.twoQueue
        self.twoQueue = []
        return ret

    def __str__(self):
        return "2:"+str(self.twoQueue)+"1:"+str(self.oneQueue)

    def __repr__(self):
        return "Latency Queue Object :"+str(self)

def logo():
    print(
"""
\033[38;5;195m 
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣶⣦⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⢴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠢⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⣼⣿⣿⣿⣿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣿⣿⠽⠿⢛⢻⢟⡽⣛⣿⢿⣿⣿⣿⠿⣿⣿⣿⣿⣿⣟⣻⠿⣿⣻⣙⠛⠾⣄⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢀⣽⡈⡋⠠⠀⡀⣈⢚⣥⢽⢫⢦⣷⣾⢋⣺⣷⣻⣿⠫⢿⣿⣿⣿⣖⡸⣿⣝⠿⣧⣘⡷⡀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢿⣶⣾⣿⣿⣁⣣⣾⡞⠚⢂⡤⠂⠃⡗⢾⢿⣯⡾⢿⣿⣿⣿⣶⣿⡿⣿⡟⣓⢧⣛⡗⢹⣿⣿⣷⡀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠙⣿⠋⡿⠉⢩⣿⢟⣀⡍⠀⠀⠘⣿⣬⣶⣷⣿⣿⣿⣿⣝⣿⠇⠣⠿⣿⣶⠋⡽⢽⠽⢣⣿⣧⠑⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢻⡀⠅⠀⢹⠟⠿⠊⢉⢀⢘⣮⣿⣿⣿⣿⣿⣏⡈⢙⣽⣛⡄⡇⠂⠘⢾⢟⡓⠒⠿⡁⢉⣿⡦⣇\033[38;5;193m 
         ██████   █████  ██████  ███████ 
         ██   ██ ██   ██ ██   ██ ██      
         ██   ██ ███████ ██████  █████   
         ██   ██ ██   ██ ██   ██ ██      
         ██████  ██   ██ ██   ██ ███████⠀⠀⠀\033[38;5;214m ⠀⠀⠀⠀
⠀           Dice As REssources (v0.1)⠀⠀⠀⠀⠀⠀\033[38;5;195m ⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠐⠐⠲⣦⣀⣄⡀⠀⡀⢀⢀⣠⣀⢆⡒⡀⢢⣶⣶⣶⠖⣴⠤⣆⣲⢖⣆⣶⣦⣤⣐⠒⠀⠀⠠⠠⣤⢶⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠂⠡⣿⣿⠦⣄⣒⣿⡁⢠⣍⡳⠈⢸⣼⣿⣽⣹⣿⡽⠉⢾⣷⣸⠙⢞⣝⡿⠲⣒⣀⠔⠋⢀⠃⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢋⣦⡀⠀⠋⠁⠀⠀⠥⠀⠀⠁⢀⡀⡸⣿⣼⣳⢴⡻⡣⣄⡌⣿⣏⣶⠾⢓⠾⠶⠁⣁⣤⡤⠁⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠨⠿⣷⠀⠀⢀⠀⠀⠀⠄⠀⣴⡟⠻⣿⡊⡦⢮⢁⡅⠀⠁⠈⠁⠀⠀⣀⣤⣤⣶⣿⣿⠟⠁⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠁⠀⠀⢀⣀⣉⣉⡭⡻⢇⠀⢀⢠⣀⣤⣤⣶⣾⣾⣷⣿⣿⣿⣿⠟⠋⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠐⣦⣶⣚⠻⢿⣿⣿⣷⣗⣤⠊⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠟⣺⡟⡾⣯⣛⣿⣿⣿⣚⣛⣛⣿⡿⣛⠿⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠉⠛⠛⠓⠒⠋⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
\033[0m 
"""
    )

def art():
    f = codecs.open("art", "r", encoding="utf-8")
    art = f.read()
    f.close()
    print("\033[2J\033[0;0H",end="")
    print(art)
    print(
"""
\033[38;5;148m\033[11;10H ██████   █████  ██████  ███████ 
\033[38;5;149m\033[12;10H ██   ██ ██   ██ ██   ██ ██      
\033[38;5;150m\033[13;10H ██   ██ ███████ ██████  █████   
\033[38;5;151m\033[14;10H ██   ██ ██   ██ ██   ██ ██      
\033[38;5;152m\033[15;10H ██████  ██   ██ ██   ██ ███████
\033[38;5;223m\033[16;16H Dice As REssources \033[0m\033[25;0H"""
    )
    return None

def line():
    print("\033[38;5;240m------------------------------------------------\033[0m",end="")
    return None

def ask(symbol):
    out = input("\033[38;5;217m"+symbol+">\033[0m")
    return out

if __name__=="__main__":

    from types import SimpleNamespace
    ##INITVAR
    S = SimpleNamespace()
    S.PHIdicepool = []
    S.PSIdicepool = []
    S.PDiceTray = dice.DiceTray() #PHI DiceTray
    S.pDiceTray = dice.DiceTray() #pSI DiceTray
    S.Latency = LatencyQueue()
    S.PHIqueue = [SpecialDice(2,"Ψ"),SpecialDice(6,"Ψ")]
    S.PSIqueue = [SpecialDice(2,"Ψ"),SpecialDice(6,"Ψ")] 
    S.thro = ["dx"] 
    S.name = ""
    
    S.PDiceTray.add(SpecialDice(2,"Ψ"))

    art()

    ## get init param :
    print(
"""
Please give your (sur)name"""
    )
    S.name=ask("name?")
    if S.name == "": S.name = "Ninten"
    print(
"""
Please give your PHI dice pool
formatting is expected to be such as :
d4,d4,d6,d4,d8 (no spaces at the start !)"""
    )
    uinput = ask("Φ?")
    if uinput=="":uinput="d4,d4,d4,d6,d6,d6"
    argsdice = uinput.split(",")
    S.PHIdicepool = [] 
    for e in argsdice:
        dval = int(e[1:])
        S.PHIdicepool.append(SpecialDice(dval,"Φ"))
    S.PHIdicepool.sort()

    print(
"""
Please give your PSI dice pool
(same format)"""
    )
    uinput=ask("Ψ?")
    if uinput=="":uinput="d4,d4,d4,d6,d8"
    argsdice = uinput.split(",")
    S.PSIdicepool = [] 
    for e in argsdice:
        dval = int(e[1:])
        S.PSIdicepool.append(SpecialDice(dval,"Ψ"))
    S.PSIdicepool.sort()

    def stupidPrint(mlist):
        out = ""
        for e in mlist:
            out += str(e)+" "
        return out
    
    def printDiceTray():
        print(" ROL  [ \033[38;5;214m"+stupidPrint(S.PDiceTray)+"\033[38;5;159m"+stupidPrint(S.pDiceTray)+"\033[0m] ")

    def printallstat(ShowDiceTray=False):
        print("\033[38;5;214m PHI \033[38;5;10m [ "+stupidPrint(S.PHIdicepool)+"]\033[0m "
        ,end="")
        if not S.PHIqueue: print("") #list is empty
        else:
            print("\033[38;5;64m < "+str(S.PHIqueue[0])+"\033[38;5;242m "+stupidPrint(S.PHIqueue[1:])+"<     \033[0m " )   
        print("\033[38;5;214m PSI \033[38;5;159m [ "+stupidPrint(S.PSIdicepool)+"]\033[0m "
        ,end="")
        if not S.PSIqueue: print("") 
        else:  
            print("\033[38;5;67m < "+str(S.PSIqueue[0])+"\033[38;5;242m "+stupidPrint(S.PSIqueue[1:])+"<     \033[0m " )
        if S.Latency.oneQueue or S.Latency.twoQueue: # not empty
            print("\033[38;5;202 LAT \033[38;5;242m 1[ "+stupidPrint(S.Latency.oneQueue)+"] 2[ "+stupidPrint(S.Latency.twoQueue)+"]")
        
        if ShowDiceTray:
            printDiceTray()

        if S.thro:
            print("\033[38;5;242m DEL  [ "+stupidPrint(S.thro)+"]\033[0m")
    line() 
    print("\nWelcome "+S.name+" !")
    while True:
        printallstat(True)
        print(
"""
Please type your command. Type \033[1m help \033[0m
To see available command or \033[1m help <command> \033[0m
To get an explanation of a command"""
        )
        ask("command?")