#!/usr/bin/env python

import json
from argparse import ArgumentParser
from os.path import expanduser


def args_cmd():
    """Parse the command line arguments via argparse"""
    parser = ArgumentParser(description='Convert tool from txt to json format')
    parser.add_argument(
        '--input', '-i', default=expanduser('~/.workstamps.txt'),
        help='Input filename (default ~/.workstamps.txt')
    parser.add_argument(
        '--output', '-o', default=expanduser('~/.workstamps.json'),
        help='Input filename (default: ~/.workstamps.json)')
    return parser.parse_args()


def itemify(line):
    """Split a line, returning date and the rest of data"""
    line = line.strip()
    end_time = line[:16]
    info = line[17:]
    return(line[:16], line[17:])


if __name__ == '__main__':
    args = args_cmd()

    stamps = []

    with open(args.input, 'r') as infile:

        for line in infile:
            time, info = itemify(line)

            if info == 'start':
                customer = None
                action = None
                start_time = time
                end_time = None

            elif time == 'restarttotals':
                continue

            else:
                info_items = info.split(' ', 1)
                customer = info_items[0]
                action = ''
                if len(info_items) > 1:
                    action = info_items[1].lstrip('- ')
                end_time = time

            stamps.append({
                'start': start_time,
                'end': end_time,
                'customer': customer,
                'action': action,
            })
    with open(args.output, 'w') as stamps_file:
        json.dump(stamps, stamps_file, indent=4)
