import os, argparse, sys, re
from mwclient import (Site,
                      errors)
from jinja2 import Template
from functions import (print_colormsg,
                       reorder_imgs)

p = argparse.ArgumentParser(description='Upload files from a directory, with metadata values to the wiki.\n'
                                        'Note that any VALUES CONTAINING '
                                        'SPACES SHOULD BE BETWEEN QUOTATION MARKS',
                            formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# TODO: Add example of command to description
p.add_argument('--host', default='hub.xpub.nl/sandbox', help='wiki host')
p.add_argument('--path', default='/itchwiki/', help='Wiki path. Should end with /')
p.add_argument('--dry', '-d', action='store_true',
               help='dry-run: will only print the metadata of each file that '
                    'will be upload, but does NOT upload')
p.add_argument('--dir', required=True,
               help='Required. Full path of the image directory, that you wish to upload')

p.add_argument('--title', required=True,
               help='Required. Must not exist yet in the wiki.')
p.add_argument('--date', required=True,
               help='Required. Format: yyyy/mm/dd '
                    'For dates without day or month use 01 as default '
                    'ie. 1986: --date "1986/01/01" '
                    'March 1985: --date "1984/05/01"')
p.add_argument('--creator', required=False, action='append', default=[''],
               help='Multiple values should be SEPARATED BY COMMA')
p.add_argument('--org', required=False, action='append', default=[''],
               help='Organization:Multiple values should be SEPARATED BY '
                    'COMMA')
p.add_argument('--format', required=False, action='append', default=[''],
               help='Multiple values should be SEPARATED BY COMMA')
p.add_argument('--event', required=False, action='append', default=[''],
               help='Multiple values should be SEPARATED BY COMMA')
p.add_argument('--topic', required=False, action='append', default=[''],
               help='Multiple values should be SEPARATED BY COMMA')
p.add_argument('--language', required=False, action='append', default=[''],
               help='Multiple values should be SEPARATED BY COMMA')

# TODO ADD  NEW PROPS
args = p.parse_args()

# login
site = Site(host=args.host, path=args.path)

wd =os.path.dirname(os.path.abspath(__file__))  # parent working directory
with open(os.path.join(wd, 'login.txt'), 'r') as login:  # read login user & pwd
    loginlines = login.read()
    user, pwd = loginlines.split('\n')
    site.login(username=user, password=pwd)  # login to wiki

# metadata checks
if os.path.isdir(args.dir) is False:
    print_colormsg(f'Error: --dir {args.dir} absolute path cannot be found', level='fail')
    sys.exit()
elif not re.match(r'\d{4}\/\d{2}\/\d{2}', args.date):
    print_colormsg(f'Error:  --date {args.date} format should be --date "yyyy/mm/dd"', level='fail')
    sys.exit()
elif len(list(site.ask(f'[[Title::{args.title}]]'))) > 0:
    print_colormsg(f'Error: --title "{args.title}" already exists in wiki. Provide a different one', level='fail')
    sys.exit()

# read template file
with open(os.path.join(wd, 'templates/smw_infobox_template.jinja')) as tmplt:
    smw_propval_template = Template(tmplt.read())

lsimgs = reorder_imgs(dir=args.dir, dry=args.dry)
dirname = os.path.split(args.dir)[-1].replace(' ', '_')
dirname = re.sub(r'[\W]', '', dirname) #remove non letters or digits
# print('lsimgs:', lsimgs, '\n', dirname)

for n, _file in enumerate(lsimgs):
    pagename = f'{dirname}-{_file}'
    print_colormsg(pagename, level='ok')
    page = site.pages[_file]

    if page.exists:
        url = page.imageinfo['descriptionurl']
        print_colormsg(
            f'Already exists in {url} Will NOT be uploaded',
            level='warning')
    else:
        img_smw_prop_val = smw_propval_template.render(
            title=args.title,
            date=args.date,
            part=n + 1,
            partof=len(lsimgs),
            creator=(', ').join(args.creator[1:]),
            organization=(', ').join(args.org[1:]),
            format=(', ').join(args.format[1:]),
            event=(', ').join(args.event[1:]),
            topic=(', ').join(args.topic[1:]),
            language=(', ').join(args.language[1:])
        )

        _file_path = os.path.join(args.dir, _file)
        if not args.dry:
            pageurl = f'https://{args.host}{args.path}index.php/File:{pagename}'
            with open(_file_path, 'rb') as _f:
                try:
                    site.upload(file=_file_path,
                                filename=pagename,
                                description=img_smw_prop_val,
                                ignore=True)
                    print(img_smw_prop_val)
                except errors.APIError as e:
                    print_colormsg(f'Error: {e.info}\n'
                                   f'It will not be uploaded',
                                   level='fail')

                print(f'See image at {pageurl}')
        else:
            print(img_smw_prop_val)

