#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Run the edapy main script."""

# core modules
import collections
import io
import os
import sys

# 3rd party modules
import pandas as pd
import yaml

# local modules
import edapy
from edapy.interactive_type_finder import find_type
from edapy.describe import describe_pandas_df


def setup_yaml():
    """
    Make yaml.dump print collections.OrderedDict the right way.

    https://stackoverflow.com/a/8661021
    """
    represent_dict_order = (lambda self, data:
                            self.represent_mapping('tag:yaml.org,2002:map',
                                                   data.items()))
    yaml.add_representer(collections.OrderedDict, represent_dict_order)


setup_yaml()


def main(csv_path, yaml_path):
    """
    Start the CSV recognizing.

    Parameters
    ----------
    csv_path : str
    yaml_path : str
    """
    csv_path = os.path.abspath(csv_path)
    yaml_path = os.path.abspath(yaml_path)
    if not os.path.isfile(csv_path):
        print("Could not find '{}'.".format(csv_path))
        sys.exit(1)
    if not os.path.isfile(yaml_path):
        df = pd.read_csv(csv_path, sep=None, engine='python')
        data = collections.OrderedDict()
        data['csv_meta'] = {'delimiter':
                            edapy.utils.get_csv_delimiter(csv_path),
                            'quotechar': edapy.utils.get_quote_char(csv_path)}
        data['columns'] = find_type(df)
        _write_yaml(yaml_path, data)
    else:
        data = _read_yaml(yaml_path)
    describe_pandas_df(df)
    _write_yaml(yaml_path, data)


def _load_dtype(data):
    dtype = {}
    for el in data['columns']:
        print(el)
        if 'type' in el:
            dtype[el['name']] = el['type']
        elif 'dtype' in el:
            dtype[el['name']] = el['dtype']
    return dtype


def _write_yaml(yaml_path, data):
    with io.open(yaml_path, 'w', encoding='utf8') as outfile:
        yaml.dump(data, outfile, default_flow_style=False, allow_unicode=True)


def _read_yaml(yaml_path):
    with open(yaml_path, 'r') as stream:
        data_loaded = yaml.load(stream)
    return data_loaded


def get_parser():
    """Get parser object for edapy."""
    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    parser = ArgumentParser(description=__doc__,
                            formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument('--csv',
                        dest='csv_path',
                        help='CSV file to read',
                        required=True,
                        metavar='FILE')
    parser.add_argument('--types',
                        dest='yaml_path',
                        help='YAML file to read / write',
                        default='types.yaml',
                        metavar='FILE')
    return parser


if __name__ == '__main__':
    args = get_parser().parse_args()
    main(args.csv_path, args.yaml_path)
