#!/usr/bin/env python3

import sys
import datetime
import ck2parser
from collections import defaultdict

#TAB = ' ' * 4
TAB = '\t'

emf_path = ck2parser.rootpath / 'EMF/EMF'
#revolt_modifier_path = emf_path / 'common/event_modifiers/emf_revolt_codegen_modifiers.txt'
revolt_effect_path = emf_path / 'common/scripted_effects/emf_revolt_codegen_effects.txt'
revolt_trigger_path = emf_path / 'common/scripted_triggers/emf_revolt_codegen_triggers.txt'
#revolt_localisation_path = emf_path / 'localisation/1_emf_revolt_codegen.csv'
#revolt_custom_loc_path = emf_path / 'localisation/customizable_localisation/emf_revolt_custom_loc_codegen.txt'

###

g_codegen_file_hdr = '''
###################################################################################
# WARNING: Do NOT modify this file manually!
#
# This file is code-generated by src/revolt_codegen.py and will overwrite changes.
###################################################################################
'''

###


def main():
	global g_religions
	
	# grab a list of religions from the religions folder
	g_religions = []

	for _, tree in ck2parser.SimpleParser(emf_path).parse_files('common/religions/*.txt'):
		for n, v in tree:
			if n.val.endswith('_trigger'):
				continue
			for n2, v2 in v:
				if isinstance(v2, ck2parser.Obj) and n2.val not in ['color', 'male_names', 'female_names', 'interface_skin', 'alternate_start']:
					g_religions.append(n2.val)

	with revolt_trigger_path.open('w', encoding='cp1252', newline='\n') as f:
		print_file_header(f, 'ck2.scripted_triggers')
		print_trigger_ROOT_religion_matches_rebel_victory_title(f)

	with revolt_effect_path.open('w', encoding='cp1252', newline='\n') as f:
		print_file_header(f, 'ck2.scripted_effects')
		print_effect_clear_religion_title_flags(f)
		print_effect_set_religion_title_flag(f)

	return 0


def print_file_header(f, spec=None):
	if spec:
		print('# -*- {} -*-'.format(spec), file=f)
	print(g_codegen_file_hdr, file=f)


#### TRIGGERS ####


def print_trigger_ROOT_religion_matches_rebel_victory_title(f):
	print('''
# ROOT = character, THIS = religious rebel victory title
emf_revolt_ROOT_religion_matches_rebel_victory_title = {
	OR = {''', file=f)

	for r in g_religions:
		print('''\
		AND = {{
			has_flag = emf_revolt_religious_victory_title_{0}
			ROOT = {{ religion = {0} }}
		}}'''.format(r), file=f)

	print(TAB + '}\n}', file=f)


#### EFFECTS ####

def print_effect_clear_religion_title_flags(f):
	print('''
emf_revolt_clear_religion_title_flags = {''', file=f)

	for rel in g_religions:
		print('\tclr_title_flag = emf_revolt_religious_victory_title_{}'.format(rel), file=f)

	print('}', file=f)


def print_effect_set_religion_title_flag(f):
	print('''
emf_revolt_set_religion_title_flag = {''', file=f)

	for rel in g_religions:
		print('''\
	if = {{
		limit = {{ owner = {{ religion = {0} }} }}
		set_flag = emf_revolt_religious_victory_title_{0}
	}}'''.format(rel), file=f)

	print('}', file=f)


if __name__ == '__main__':
	sys.exit(main())
