# Path: lib/fpos/core.py
# def money(value):
#     return "{:.2f}".format(value)
# 
# Path: lib/fpos/core.py
# def money(value):
# def lcs(a, b):


import argparse
import csv
import sys
import dateutil.parser as dp
import re
from datetime import datetime
from .core import money
from .core import date_fmt
from itertools import chain


def transform_nab_2018(csv, args=None):
    # NAB 2018 format:
    #
    # Date, Amount,,,Transfer Type, Description, Remaining Balance
    # "07 Feb 18","-12.00",,,"TRANSFER DEBIT","INTERNET TRANSFER   Lunch","+500.00"
    #
    # The date format is different to 2018, and there's one less column. Also
    # flips the bird to data types, everything is a string. And now
    # currency-style value formatting means locale-specific separators such as
    # ',' get in the way of parsing the transaction value.
    def _gen():
        for l in csv:
            if l:
                ir_date = datetime.strptime(l[0], _nab_2018_date_fmt).strftime(date_fmt)
                # Deal with currency-style strings by cutting out everything
                # except the sign, digits the decimal point
                ir_amount = money(float(re.sub(r'[^\d.+-]', '', l[1])))
                ir_description = " ".join(e for e in l[4:6] if (e is not None and "" != e ))
                yield [ ir_date, ir_amount, ir_description ]
    return _gen()

def transform_bankwest(csv, args=None):
    # Bankwest format:
    #
    # BSB Number,Account Number,Transaction Date,Narration,Cheque,Debit,Credit,Balance,Transaction Type
    # ,5229 8079 0109 8683,27/10/2014,"CALTRAIN TVM             SAN CARLOS   CA85450784297436040013768           5.25US",,6.00,,116.32,WDL
    def _gen():
        for l in csv:

# Path: lib/fpos/core.py
# def money(value):
#     return "{:.2f}".format(value)
# 
# Path: lib/fpos/core.py
# def money(value):
# def lcs(a, b):


# Path: lib/fpos/transform.py
import argparse
import csv
import sys
import dateutil.parser as dp
import re
from datetime import datetime
from .core import money
from .core import date_fmt
from itertools import chain



def transform_nab_2018(csv, args=None):
    # NAB 2018 format:
    #
    # Date, Amount,,,Transfer Type, Description, Remaining Balance
    # "07 Feb 18","-12.00",,,"TRANSFER DEBIT","INTERNET TRANSFER   Lunch","+500.00"
    #
    # The date format is different to 2018, and there's one less column. Also
    # flips the bird to data types, everything is a string. And now
    # currency-style value formatting means locale-specific separators such as
    # ',' get in the way of parsing the transaction value.
    def _gen():
        for l in csv:
            if l:
                ir_date = datetime.strptime(l[0], _nab_2018_date_fmt).strftime(date_fmt)
                # Deal with currency-style strings by cutting out everything
                # except the sign, digits the decimal point
                ir_amount = money(float(re.sub(r'[^\d.+-]', '', l[1])))
                ir_description = " ".join(e for e in l[4:6] if (e is not None and "" != e ))
                yield [ ir_date, ir_amount, ir_description ]
    return _gen()

def transform_bankwest(csv, args=None):
    # Bankwest format:
    #
    # BSB Number,Account Number,Transaction Date,Narration,Cheque,Debit,Credit,Balance,Transaction Type
    # ,5229 8079 0109 8683,27/10/2014,"CALTRAIN TVM             SAN CARLOS   CA85450784297436040013768           5.25US",,6.00,,116.32,WDL
    def _gen():
        for l in csv:

            yield [l[2], money((-1.0 * float(l[5])) if l[5] else float(l[6])), l[3][1:-1]]