from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone
from viz.models import MunicipalityResult, RawData, PartyResult, Municipality, RawData
import json
import xml.etree.ElementTree as ET
import pprint
import requests
import datetime
import hashlib


class Command(BaseCommand):
	def add_arguments(self, parser):
		parser.add_argument(
			'--local_path',
			dest='local_path',
			help='Specify a file path to import from',
		)

	def handle(self, *args, **options):
		if 'local_path' in options.keys():
			xml_data, xml_header = self.get_local_data(options['local_path'])
		else:
			xml_data, xml_header = self.get_network_data(options)

		# check, if xml is already downloaded and imported via RawData-hashes
		
		# if not, convert xml to dict
		data = []
		root = ET.fromstring(xml_data)

		for municipality in root.iter('municipality'):
			tmp = {}
			for elem in municipality:
				tmp.update({elem.tag: elem.text})
			data.append(tmp)

		# check data
		# is_wrong = False
		# for mun in data:
		# 	print(mun['spatial_id'])
		# 	party_sum = 0
		# 	for party in config['parties']:
		# 		party_sum += int(mun[party])
		# 	print(int(mun['valid']) == party_sum)
		# 	print(int(mun['votes']) == int(mun['valid']) + int(mun['invalid']))
		# 	print(int(mun['eligible_voters']) >= int(mun['valid']))

			# check if timestamp is between first closing of polling station and now.

		#print('all checks: '+str(is_wrong))
		# 2017-10-15 15:23:38Z

		timestamp_now = timezone.now()

		# store raw data in database
		raw = RawData(
				timestamp = timestamp_now,
				hash = hashlib.md5(xml_data.encode()),
				content = xml_data,
				header = xml_header,
				dataformat = 'xml'
			)
		raw.save()

		# store results in database
		for mun in data:
			time_data = datetime.datetime.strptime(
					mun['timestamp'], "%Y-%m-%d %H:%M:%SZ")
			time_data = timezone.make_aware(
				time_data, timezone.get_current_timezone())
			result = MunicipalityResult(
				eligible_voters = mun['eligible_voters'],
				votes = mun['votes'],
				valid = mun['valid'],
				invalid = mun['invalid'],
				#spatial_id = mun['spatial_id'],
				ts_result = time_data,
				is_final = False
			)
			result.save()

	def get_local_data(self, local_path):
		"""
		Get the data from a local directory.
		"""
		with open(local_path) as test_data_file:
			test_data = test_data_file.read()

		return (test_data, '')


	def get_network_data(self, options):
		"""
		Get the data from a network location.
		"""
		import_url = self._get_input_url(options)

		if import_url == '':
			print('No import url supplied!')
			return ''

		# make http request
		r = requests.get(import_url)
		return (r.text, r.header)

	def _get_input_url(self, options):
		"""
		Gets the import path from the kwargs or the config.json
		"""
		import_url = ''

		if 'import_url' in options.keys():
			import_url = options['import_url']

		try:
			with open("config.json") as config_file:
				config = json.load(config_file)
				import_url = config['url_xml']
		except:
			print("Config file not found!")

		return import_url

@@ -676,16 +676,73 @@
 tions)%0A%0A
+%09%09self.write_raw_data_to_database(xml_data, xml_header)%0A%0A
 %09%09# chec
@@ -809,11 +809,8 @@
 hes%0A
-%09%09%0A
 %09%09# 
@@ -1569,251 +1569,8 @@
 ()%0A%0A
-%09%09# store raw data in database%0A%09%09raw = RawData(%0A%09%09%09%09timestamp = timestamp_now,%0A%09%09%09%09hash = hashlib.md5(xml_data.encode()),%0A%09%09%09%09content = xml_data,%0A%09%09%09%09header = xml_header,%0A%09%09%09%09dataformat = 'xml'%0A%09%09%09)%0A%09%09raw.save()%0A%0A%09%09# store results in database%0A
 %09%09fo
@@ -2024,16 +2024,325 @@
 save()%0A%0A
+%09def write_raw_data_to_database(self, xml_data, xml_header):%0A%09%09%22%22%22%0A%09%09Write raw data into table.%0A%09%09%22%22%22%0A%09%09timestamp_now = timezone.now()%0A%0A%09%09raw = RawData(%0A%09%09%09timestamp = timestamp_now,%0A%09%09%09hash = hashlib.md5(xml_data.encode()),%0A%09%09%09content = xml_data,%0A%09%09%09header = xml_header,%0A%09%09%09dataformat = 'xml')%0A%09%09raw.save()%0A%0A
 %09def get
@@ -2423,16 +2423,70 @@
 .%0A%09%09%22%22%22%0A
+%09%09print(%22Importing data from: %7B%7D%22.format(local_path))%0A
 %09%09with o
@@ -3244,16 +3244,16 @@
 und!%22)%0A%0A
-
 %09%09return
@@ -3264,8 +3264,9 @@
 ort_url%0A
+%0A

Move raw data write to its own function