#!/usr/bin/env python3
# universal command line tool for all gomazing realated tasks for example
# syncing between plentymarkets and shopware

import argparse
import logging

import gmz.sync.shopware
import gmz.sync.sellerlogic
import gmz.prices.snaptrade
import gmz.ebay.profile
import gmz.mailbot.tuvcheck

from gmz.config import *


def idlist(s):
    return s.split(',')


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Gomazing command line tool')
    parser.add_argument('--logfile', default=None, help='file for logging')
    parser.add_argument('-s', '--silent', action='store_true',
                        help='only display errors')
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='display more details')
    subparsers = parser.add_subparsers(dest='action')
    # shopware
    actionparser1 = subparsers.add_parser('shopware')
    shopwaresubparsers = actionparser1.add_subparsers(dest='subaction')
    swparser1 = shopwaresubparsers.add_parser(
        'syncitems', help='syncs items from plenty to shopware')
    swparser1.add_argument(
        'plentyitems', nargs='?', type=str,
        help='Plenty item synchronize with shopware'
    )
    swparser1.add_argument(
        '--all', action='store_true', help='update all items'
    )
    swparser1.add_argument(
        '--ignorewarnings', action='store_true',
        help='Try to update as many items as possible ignoring individual'
        'warnings'
    )
    swparser1.set_defaults(func=gmz.sync.shopware.cmd_syncvariations)
    swparser2 = shopwaresubparsers.add_parser(
        'syncstock', help='sync stock from plenty to shopware')
    swparser2.add_argument(
        'plentyvariations', nargs='?', type=str,
        help='Plenty variations for which to synchronize stock with shopware'
    )
    swparser2.set_defaults(func=gmz.sync.shopware.cmd_syncstock)
    swparser3 = shopwaresubparsers.add_parser(
        'syncorders',
        help='import orders to plenty and set orderstate in shopware')
    swparser3.add_argument(
        'orderids', nargs='?', type=idlist,
        help='plenty or shopware order ids to handle'
    )
    swparser3.set_defaults(func=gmz.sync.shopware.cmd_syncorders)
    actionparser2 = subparsers.add_parser('snaptrade')
    snaptradesubparsers = actionparser2.add_subparsers(dest='subaction')
    snapparser1 = snaptradesubparsers.add_parser(
        'shopfeed', help='prepare feed for snaptrade import')
    snapparser1.add_argument('channel', choices=['gomazing', 'poolpirat'],
                             help='sales channel/shop to process')
    snapparser1.add_argument(
        'feedpath', type=str, help='Output path for feed CSV'
    )
    snapparser1.set_defaults(func=gmz.prices.snaptrade.cmd_shopfeed)
    snapparser2 = snaptradesubparsers.add_parser(
        'reprice', help='set new plenty prices from csv'
    )
    snapparser2.add_argument(
        'csvpaths', type=str, nargs='*',
        help='path to one or more shopspy csvs with price updates'
    )
    snapparser2.set_defaults(func=gmz.prices.snaptrade.cmd_reprice)
    snapparser3 = snaptradesubparsers.add_parser(
        'amazonfeed', help='set prepare feed for snaptrade'
    )
    snapparser3.add_argument(
        'feedpath', type=str, help='Output path for feed CSV'
    )
    snapparser3.set_defaults(func=gmz.prices.snaptrade.cmd_amazonfeed)
    actionparser3 = subparsers.add_parser('ebay')
    ebaysubparsers = actionparser3.add_subparsers(dest='subaction')
    ebayparser1 = ebaysubparsers.add_parser(
        'profilesync',
        help='update ebay shipping profiles and consider release date'
    )
    ebayparser1.set_defaults(func=gmz.ebay.profile.cmd_profilesync)
    actionparser3 = subparsers.add_parser('mailbot')
    botsubparsers = actionparser3.add_subparsers(dest='subaction')
    botparser1 = botsubparsers.add_parser(
        'tuvcheck', help='check one or more variations for valid TÜV '
        'certificates')
    botparser1.add_argument(
        'variationids', nargs='?', type=idlist,
        help='variations to check'
    )
    botparser1.set_defaults(func=gmz.mailbot.tuvcheck.cmd_tuvcheck)
    args = parser.parse_args()

    LOGFORMAT = '%(asctime)s %(levelname)s %(name)s %(message)s'
    level = logging.INFO
    if args.silent:
        level = logging.ERROR
    if args.verbose:
        level = logging.DEBUG
    logging.basicConfig(filename=args.logfile, level=level,
                        format=LOGFORMAT)
    args.func(args)
