# Create json interface file using mapping rules from idocmapin.txt

import os
import time
import datetime
import json
from collections import OrderedDict

# Get Current Path
dir_path = os.path.dirname(os.path.realpath(__file__))

def jsonAdd(key, segments, status):
	e = 0
# if key is Blank add to Main Segment
	if key == "":
		for segment in segments:
			try:
				jsonMain[segment.split(";")[0]] = segment.split(";")[1]
			except:
				e=e+1
	else:
# Create Associative Dictionary for JSON segment
#		jsonSegment = {}
		jsonSegment = OrderedDict()
		jsonSegment.clear()
#		del jsonSegment[:]
		for segment in segments:
			try:
				jsonSegment[segment.split(";")[0]] = segment.split(";")[1]
			except:
				e=e+1
# Add Segment to Main JSON
		if status == "new":
			if key not in jsonMain:
				if key == "nmukbporBody":
					jsonMain[key] = [jsonSegment]
				elif key == "vimsorderBody":
					jsonMain[key] = [jsonSegment]
				else:
					jsonMain[key] = jsonSegment
#				print("TEST1"+str(jsonMain)+"-"+str(segment))
			elif type(jsonMain[key]) == list:
				jsonMain[key].append(jsonSegment)
#				print("TEST2"+str(jsonMain)+"-"+str(segment))
			elif key == "nmukbporBody":
				jsonMain[key] = [jsonMain[key], jsonSegment]
			elif key == "vimsorderBody":
				jsonMain[key] = [jsonMain[key], jsonSegment]
#				print("TEST3"+str(jsonMain)+"-"+str(segment))
			else:
				jsonMain[key] = [jsonMain[key], jsonSegment]
#				print("TEST4"+str(jsonMain)+"-"+str(segment))
		else:
			if key not in jsonMain:
				jsonMain[key] = jsonSegment
			elif type(jsonMain[key]) == list:
				jsonMain[key].append(jsonSegment)
			else:
				try:
					del jsonSegment['SEGNAM']
				except:
					e=e+1
				jsonMain[key].update(jsonSegment)
#	print("main:"+str(jsonMain))
	
# Main Processing
def main():
	mapSegment = []
	mapLength = []
	mapMap = []
	
	segments = []
	outSegments = []
	
#	jsonMain = {}
	jsonMain = OrderedDict()
	
# Read IDOC Mapping Values
	file_path_map = os.path.join(dir_path, 'idocmapin.txt')
	iDocMap = open(file_path_map,"r")
	for line in iDocMap:
		line = line.replace('\n','')
		mapSegment.append(line.split(":")[0])
		mapLength.append(line.split(":")[1])
		mapMap.append(line.split(":")[2])
	iDocMap.close()
	
# Read IDOC Transaction
	file_path = os.path.join(dir_path, '../vimsin/idoctranin.txt')
	iDocTran = open(file_path,"r")
	l = 0
	outSegments.clear()
#	del outSegments[:]
	lineType = ""
	segmentType = ""
	for line in iDocTran:
		line = line.replace('\n','')
		line = line.replace(',',' ')
#		print("LINE:"+line)
		offset = 0
		while len(line) > offset:
			segment = line[offset:offset+7]
			if (l == 0):
				if segment[0:6] == "TABNAM":
					return False
# format segment from NMUK Message
			if segment[0:2] == "HD":
				segment = "HD"+line[offset+10:offset+18]
				segmentType = line[offset+10:offset+18]
				lineType = "HD"
#				print("SEGMENT:"+segment+":"+segmentType)
			elif segment[0:2] == "DT":
				segment = "DT"+segmentType;
				lineType = "DT"
#				print("SEGMENT:"+segment+":"+segmentType)
			elif segment[0:2] == "FT":
				segment = "FT"+segmentType;
				lineType = "FT"
#				print("SEGMENT:"+segment+":"+segmentType)
#			else:
#				lineType = ""
#				print("SEGMENT:"+segment+":"+segmentType)
# Look for matching Segment in Mapping List
			segmentFound = False
			i = 0
			while i < len(mapSegment) and segmentFound == False:
				if len(segment) > 0:
					if segment.strip() == mapSegment[i].strip():						
#						print("MAPSEGMENT:"+segment+":"+mapSegment[i])
						segments.append(segment)
						segmentFound = True
						firstPos = 0
						lastPos = 0
						detailStr = ""
# Split-out segment line from input string
						segStr = line[offset:offset+int(mapLength[i])]
# Split-out Fields Mapping (name,position,length)
						mapDetail = mapMap[i].split(";")
#						print("MAPDETAIL:"+str(mapDetail))
						d = 0
						while d < len(mapDetail):
# get field starting position in segment string
							if mapDetail[d+1] == "+":
								firstPos = lastPos
							else:
								firstPos = int(mapDetail[d+1])
# Check is Segment is excluded
							if len(mapDetail) > d+2 and mapDetail[d+3] != "*" and mapDetail[d+3][-1:] != ")":
# add formatted segment detail to output string
								if len(detailStr) > 0:
									detailStr = detailStr+","
								
								if len(segStr) > firstPos:
									if mapDetail[d+3] != "" and mapDetail[d+3][-1:] != ")":
										detailStr = detailStr+mapDetail[d+3]+";"+segStr[firstPos:firstPos+int(mapDetail[d+2])]
									else:
										detailStr = detailStr+mapDetail[d]+";"+segStr[firstPos:firstPos+int(mapDetail[d+2])]
								else:
									if mapDetail[d+3] != "" and mapDetail[d+3][-1:] != ")":
										detailStr = detailStr+mapDetail[d+3]+";"+segStr[firstPos:len(detailStr)]
									else:
										detailStr = detailStr+mapDetail[d]+";"+segStr[firstPos:len(detailStr)]
						
							if len(mapDetail) > d+2:
								lastPos = firstPos+int(mapDetail[d+2])
							else:
								lastPos = len(mapDetail)
							d = d+4
# Store formatted Segments
						outSegments.append(detailStr)	
						offset = offset+int(mapLength[i])
				i = i+1
			if segmentFound == False:
				offset = offset+1
#		print("OUT:"+segment+":"+segmentType+":"+lineType+":"+str(outSegments))
		if (lineType == "FT" or lineType == ""):
# Create Message Format
			i = 0
			e = 0
			jsonMain.clear()
#			del jsonMain[:]
# Split-out Message Segments
#			print("OUT:"+outSegments)
			while i < len(outSegments):
				segments = outSegments[i].split(",")
				if len(segments) > 0 and len(segments[0]) > 0:
					key = segments[0].split(";")[1]
#					print("KEY:"+key)
					if key == "E2EDL24":
						key = "receiptBody"
					elif key == "E2EDL52":
						key = "receiptBody"
					elif key == "Z2EDL20":
						key = "receiptBody"
					elif key == "Z2EDL24":
						key = "receiptBody"
					elif key == "E2EDL20":
						key = "receiptHeader"
					elif key == "E2LTORH":
						key = "vimsorderHeader"
					elif key == "Z2LTORH":
						key = "vimsorderBody"
					elif key == "E2LTORI":
						key = "vimsorderBody"
					elif key == "HD":
						if (segmentType.strip().lower() == "vimsor"):
							key = "vimsorderHeader"
						else:
							key = segmentType.strip().lower()+"Header"
					elif key == "DT":
						if (segmentType.strip().lower() == "vimsor"):
							key = "vimsorderBody"
						else:
							key = segmentType.strip().lower()+"Body"
					elif key == "FT":
						if (segmentType.strip().lower() == "vimsor"):
							key = "vimsorderFooter"
						else:
							key = segmentType.strip().lower()+"Footer"
# Call Function to Format to JSON
					if segments[0].split(";")[0].lower() != "segnam" or (segments[0].split(";")[0].lower() == "segnam" and len(segments) > 1):
#						jsonAdd(key, segments, "new")
						e = 0
						status = "new"
# if key is Blank add to Main Segment
						if key == "":
							for segment in segments:
								try:
									jsonMain[segment.split(";")[0]] = segment.split(";")[1]
								except:
									e=e+1
						else:
# Create Associative Dictionary for JSON segment
#							jsonSegment = {}
							jsonSegment = OrderedDict()
							jsonSegment.clear()
#							del jsonSegment[:]
							for segment in segments:
								try:
									jsonSegment[segment.split(";")[0]] = segment.split(";")[1]
								except:
									e=e+1
# Add Segment to Main JSON
							if status == "new":
								if key not in jsonMain:
									if key == "nmukbporBody":
										jsonMain[key] = [jsonSegment]
									elif key == "vimsorderBody":
										jsonMain[key] = [jsonSegment]
									else:
										jsonMain[key] = jsonSegment
#									print("TEST1"+str(jsonMain)+"-"+str(segment))
								elif type(jsonMain[key]) == list:
									jsonMain[key].append(jsonSegment)
#								print("TEST2"+str(jsonMain)+"-"+str(segment))
								elif key == "nmukbporBody":
									jsonMain[key] = [jsonMain[key], jsonSegment]
								elif key == "vimsorderBody":
									jsonMain[key] = [jsonMain[key], jsonSegment]
#								print("TEST3"+str(jsonMain)+"-"+str(segment))
								else:
									jsonMain[key] = [jsonMain[key], jsonSegment]
#								print("TEST4"+str(jsonMain)+"-"+str(segment))
							else:
								if key not in jsonMain:
									jsonMain[key] = jsonSegment
								elif type(jsonMain[key]) == list:
									jsonMain[key].append(jsonSegment)
								else:
									try:
										del jsonSegment['SEGNAM']
									except:
										e=e+1
									jsonMain[key].update(jsonSegment)
				i = i+1
			outSegments.clear();
#			del outSegments[:]

			print("JSON:"+json.dumps(jsonMain, ensure_ascii=False, indent=2))
# Create Message File
			ts = time.time()
			ts = datetime.datetime.fromtimestamp(ts).strftime('_%Y%m%d_%H%M%S_')
			l = l+1
			if (segmentType != ""):
				jsonFile = segmentType.strip()+"MSGIN"+str(ts)+str(l)+".json"
			else:
				jsonFile = "IDOCMSGIN"+str(l)+".json"
			file_path_json = os.path.join(dir_path, '../vimsjson/'+jsonFile)
			with open(file_path_json, "w") as write_file:
				json.dump(jsonMain, write_file, indent=2)
			jsonMain.clear()
#			del jsonMain[:]
#			segmentType = ""
	del outSegments
	del jsonSegment
	del jsonMain
		
	iDocTran.close()
	
	return True

if __name__ == '__main__':
		main()