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

import six

if six.PY2:
    import sys
    reload(sys)  # Reload does the trick!
    sys.setdefaultencoding('utf8')

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'ntuh.settings'

import django
django.setup()


from datetime import *
from dateutil.relativedelta import *

from django.db.models import *

# from ntuhgov import xportal
from ntuhgov.portal_selenium import Login, ReportPathology
from registry.models import *

import json

import logging
FORMAT = '%(asctime)s %(levelname)s %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)

def SavePathology(ChartNo, SESSION = None):
    # print(ChartNo, SESSION)
    reports = ReportPathology(ChartNo, SESSION)
#     print json.dumps(reports, ensure_ascii=False, indent=4, )
    for r in reports:
        p, created = PathologyReport.objects.get_or_create(PathCode = r['PathCode'])
        p.ChartNo         = r['ChartNo']
        p.ReportKey       = r['ReportKey']
        p.ReportCode      = r['ReportCode']
        p.PersonID        = r['PersonID']
#        p.PathCode        = r['PathCode']
        p.SpecimenGetDate = r['SpecimenGetDate'].replace('/', '-').split(' ')[0]
        p.ReportDate      = r['ReportDate'].replace('/', '-').split(' ')[0]
        p.url             = r['url']
        p.html            = r['html']
        p.save()

def ScanPathology(days = False):
    SESSION=Login()
    if days:
        start = date.today() + relativedelta( days = -days )
        ops = OPSchedule.objects.filter(OPDate__gt = start).order_by('OPDate')
        LIMIT = 999999999
    else:
        ops = OPSchedule.objects.all().order_by('-OPDate')
        LIMIT = 100
        
    count = 0
    for op in ops:
        SpecimenGetDate = PathologyReport.objects.filter(ChartNo=op.PatChartNo).aggregate(Max('SpecimenGetDate'))
        logging.info((op.OPDate, op.PatChartNo, SpecimenGetDate ))
        if not SpecimenGetDate['SpecimenGetDate__max'] or SpecimenGetDate['SpecimenGetDate__max'] < op.OPDate:
            logging.info( '    try to fetch it: %d' % count )
            SavePathology(op.PatChartNo, SESSION)
            count += 1
            if count > LIMIT:
                break
        
# SavePathology('2231486')
# ScanPathology()
ScanPathology(14)
