import pspos
import random

lista_level_set = []
authors = []
n_levels = [] # n. livelli per ogni level set

levels = []
ball_position = []

listFile = pspos.listdir("lvls/")
for file in listFile:
    nome_file = file.split(".")
    lista_level_set.append(nome_file[0])
    fl = open("lvls/%s.txt" %nome_file[0])
    for line in fl.readlines():
        if line[0] == "#":
            authors.append(line[1:len(line)].split(',')[0])
            n_levels.append(int(line[1:len(line)].split(',')[1]))
            break
    
def initialize(levelSet):
    global levels, ball_position, author
    if levelSet != 'random':
        levels = []
        ball_position = []
        
        fl = open("lvls/%s" %levelSet)

        levels.append([])
        i = 0
        for line in fl.readlines():
            if line[0] != "#":
                if len(line) != 1:
                    levels[i].append(line.replace(" ", ""))
                else:
                    levels.append([])
                    i += 1
        print levels[1]
        fl.close()

        nLiv = 0
        i = 0 # dispari -> 8 palle; pari -> 7 palle.
        for level in levels:
            ball_position.append([])
            for line in level:
                x = 0
                for lettera in line:
                    x += 1
                    if lettera != "-" and lettera != "\n":
                        if i % 2 == 0:
                            ball_position[nLiv].append((32+(x*21), 25+(18*i), int(lettera)))
                        else:
                            ball_position[nLiv].append((32+(x*21+(7*1.5)), 25+(18*i), int(lettera)))
                i += 1
            i = 0
            nLiv += 1
    else: # initialize random level
        global levels, ball_position
        levels = []
        ball_position = []
        
        levels.append([])
        for i in range(10):
            if i <= 4:
                if i % 2 == 0:
                    levels[0].append('%d%d%d%d%d%d%d%d\n'%(random.randint(1,6), random.randint(1,6), random.randint(1,6), random.randint(1,6),\
                                                           random.randint(1,6), random.randint(1,6), random.randint(1,6), random.randint(1,6)))
                else:
                    levels[0].append('%d%d%d%d%d%d%d\n'%(random.randint(1,6), random.randint(1,6), random.randint(1,6), random.randint(1,6),\
                                                         random.randint(1,6), random.randint(1,6), random.randint(1,6)))
            else:
                if i % 2 == 0:
                    levels[0].append('--------\n')
                else:
                    levels[0].append('-------\n')
                
        nLiv = 0
        i = 0 # dispari -> 8 palle; pari -> 7 palle.
        for level in levels:
            ball_position.append([])
            for line in level:
                x = 0
                for lettera in line:
                    x += 1
                    if lettera != "-" and lettera != "\n":
                        if i % 2 == 0:
                            ball_position[nLiv].append((32+(x*21), 25+(18*i), int(lettera)))
                        else:
                            ball_position[nLiv].append((32+(x*21+(7*1.5)), 25+(18*i), int(lettera)))
                i += 1
            i = 0
            nLiv += 1
