#!/usr/bin/python3
"""
utilisation: python csvparser.py

plus d'infos sur http://docs.python.org/library/csv.html
"""
import sys
import csv
import os
import locale
import re
import unicodedata

def strip_accents(s):
   return ''.join(c for c in unicodedata.normalize('NFD', s)
                  if unicodedata.category(c) != 'Mn')

def parse_lib(myliblist):
    file_to_parse = open('vteslib.csv', encoding='utf-8')
    dialect = csv.Sniffer().sniff(file_to_parse.read(1024))
    dialect.doublequote=True

    file_to_parse.seek(0)

    csv_reader = csv.reader(file_to_parse, dialect)
    for row in csv_reader:
        ident = row[0]
        name = row[1]
        aka = row[2]
        ctype = row[3]
        clan = row[4]
        discipline = row[5]
        pool_cost = row[6]
        blood_cost = row[7]
        conviction_cost = row[8]
        burn_option = row[9]
        card_text = row[10]
        flavor_text = row[11]
        printed_in_set = row[12]
        requirement = row[13]
        banned = row[14]
        artist = row[15]
        capacity = row[16]
        draft = row[17]
        
        full_text = 'Name: ' + name + '\n'
        if aka != '':
            full_text += 'AKA: ' + aka + '\n'   
        full_text += '[' + printed_in_set + ']\n'
        full_text += 'Cardtype: ' + ctype + '\n'
        if clan != '':
            if clan in ['Avenger','Defender','Innocent','Judge','Martyr','Redeemer','Visionary']:
                full_text += 'Creed: ' + clan + '\n'
            else:
                full_text += 'Clan: ' + clan + '\n'
        if pool_cost != '':
            full_text += 'Cost: ' + pool_cost + ' pool\n'
        if blood_cost != '':
            full_text += 'Cost: ' + blood_cost + ' blood\n'
        if conviction_cost != '':
            full_text += 'Cost: ' + conviction_cost + ' Conviction\n'

        if discipline != '':
            if discipline in ['Defense','Innocence','Judgment','Martyrdom','Redemption','Vengeance','Vision']:
                full_text += 'Virtue: ' + discipline + '\n'
            else:
                full_text += 'Discipline: ' + discipline + '\n'
        if burn_option != '':
            full_text += 'Burn Option\n'
        if banned != '':
            banned = ' {Added to the V:EKN banned list in ' + banned + '.}'
        full_text += card_text + banned + '\n'
        full_text += 'Artist: ' + artist + '\n'
        

        myliblist.append(full_text)

def parse_crypt(mycryptlist):
    file_to_parse = open('vtescrypt.csv', encoding='utf-8')

    dialect = csv.Sniffer().sniff(file_to_parse.read(1024))
    dialect.doublequote=True    
    file_to_parse.seek(0)

    csv_reader = csv.reader(file_to_parse, dialect)

    for row in csv_reader:
        ident = row[0]
        name = row[1]
        aka = row[2]
        ctype = row[3]
        clan = row[4]
        adv = row[5]
        group = row[6]
        capacity = row[7]
        disciplines = row[8]
        card_text = row[9]
        printed_in_set = row[10]
        title = row[11]
        banned = row[12]
        artist = row[13]

        if adv != '':
            name = name + '  advanced'
        full_text = 'Name: ' + name + '\n'
        if aka != '':
            full_text += 'AKA: ' + aka + '\n'
        full_text += '[' + printed_in_set + ']\n'
        full_text += 'Cardtype: ' + ctype + '\n'
        if clan in ['Avenger','Defender','Innocent','Judge','Martyr','Redeemer','Visionary']:
            full_text += 'Creed: ' + clan + '\n'
        else:
            full_text += 'Clan: ' + clan + '\n'
        if adv != '':
            full_text += 'Level: Advanced\n'
        full_text += 'Group: ' + group + '\n'
        if clan in ['Avenger','Defender','Innocent','Judge','Martyr','Redeemer','Visionary']:
            full_text += 'Life: ' + capacity + '\n'
            full_text += 'Virtue: ' + disciplines + '\n'
        else:
            full_text += 'Capacity: ' + capacity + '\n'
            full_text += 'Discipline: ' + disciplines + '\n'
        if banned != '':
            banned = ' {Added to the V:EKN banned list in ' + banned + '.}'
        full_text += card_text + banned + '\n'
        full_text += 'Artist: ' + artist + '\n'
        
        mycryptlist.append(full_text)

if __name__ == "__main__":
    os.chdir('./')
    myliblist = []
    parse_lib(myliblist)
    
    #locale.setlocale(locale.LC_ALL,("no",None))
    print (locale.getlocale()) 
    outputfilename="./output.txt"
    
    outputfile = open(outputfilename,"wb")

    mycryptlist = []
    parse_crypt(mycryptlist)

    all_text = myliblist[1:] + mycryptlist[1:]
    all_text.sort(key=lambda s: strip_accents(s.lower()))
    for row in all_text:
        row=row.replace('  advanced','')
        row=row.replace('\r','')
        row=re.sub(r'Name: (.*), The([^ ])',r'Name: The \1\2', row)
        row=re.sub(r'AKA: (.*), The([^ ])',r'AKA: The \1\2', row)
        outputfile.write(row.encode())
        outputfile.write('\n'.encode())
