﻿PoW_army_window = {
	scope = character
	saved_scopes = { army }
	is_valid = { PoW_is_loaded_gather_commanders_enabled = yes }
	effect = {
		set_variable = {
			name = PoW_army_window
			value = scope:army
		}
	}
}

PoW_appoint_commander = {
	scope = character
	saved_scopes = { army }
	is_valid = { PoW_is_loaded_gather_commanders_enabled = yes }
	effect = {
		start_travel_plan = { 
			destination = scope:army.location
			players_use_planner = no
			return_trip = no
		}
		current_travel_plan = {
			set_variable = block_travel_plan_home_button
			set_variable = block_travel_plan_customize_button
			set_variable = PoW_gather_commanders
			add_travel_plan_modifier = PoW_gather_commanders
			set_variable = {
				name = army
				value = scope:army
			}
			save_scope_as = travel_plan
			set_variable = PoW_gather_commanders
			if = {
				limit = { NOT = { has_game_rule = PoW_gather_commanders_enabled_travel_events }}
				set_variable = FF_vanilla_on_travel_plan_movement_blocker
				set_variable = FF_vanilla_on_travel_plan_arrival_blocker
				set_variable = FF_vanilla_on_travel_plan_start_blocker
				set_variable = FF_vanilla_on_travel_plan_complete_blocker
				set_variable = FF_vanilla_on_travel_plan_abort_blocker
				set_variable = FF_vanilla_on_travel_leader_removed_blocker
			}
		}
		scope:army = {
			set_variable = {
				name = commander_gather
				value = scope:travel_plan
			}
		}
	}
}

PoW_button_army_split_before = {
	scope = character
	saved_scopes = { province }
	is_valid = { PoW_is_loaded_gather_commanders_enabled = yes }
	effect = {
		scope:province = {
			every_army_in_location = {
				limit = { army_owner = root }
				set_variable = PoW_army_exists
			}
		}
	}
}

PoW_button_army_split_after = {
	scope = character
	saved_scopes = { province }
	is_valid = { PoW_is_loaded_gather_commanders_enabled = yes }
	effect = {
		scope:province = {
			every_army_in_location = {
				limit = { army_owner = root }
				if = {
					limit = {
						NOT = { has_variable = PoW_army_exists }
						exists = army_commander
					}
					remove_commander = yes
				}
				remove_variable = PoW_army_exists
			}
		}
	}
}

PoW_button_army_merge_before = {
	scope = character
	saved_scopes = { army }
	is_valid = { PoW_is_loaded = yes }
	effect = {
		# if = {
		# 	limit = { exists = scope:army.army_commander }
		# 	set_variable = {
		# 		name = PoW_army_commander_select
		# 		value = scope:army.army_commander
		# 	}
		# }
		# scope:army.location = {
		# 	every_army_in_location = {
		# 		limit = {
		# 			army_owner = root
		# 			NOT = { this = scope:army }
		# 		}
		# 		army_commander ?= {
		# 			root = {
		# 				add_to_variable_list = {
		# 					name = merge_commanders
		# 					target = prev
		# 				}
		# 			}
		# 		}
		# 	}
		# }
	}
}


PoW_button_army_merge_after = {
	scope = character
	saved_scopes = { army }
	is_valid = { PoW_is_loaded = yes }
	effect = {
		# scope:army = {
		# 	if = {
		# 		limit = {
		# 			exists = root.var:PoW_army_commander_select
		# 			NOT = { scope:army.army_commander ?= root.var:PoW_army_commander_select }
		# 		}
		# 		if = {
		# 			limit = { exists = scope:army.army_commander }
		# 			remove_commander = yes
		# 			assign_commander = root.var:PoW_army_commander_select
		# 		}
		# 	}
		# }
		# every_in_list = {
		# 	variable = merge_commanders
		# 	limit = { is_commanding_army = no }
		# 	set_location = scope:army.location
		# 	var:commander_story ?= { end_story = yes }
		# }
		# if = {
		# 	limit = {
		# 		any_in_list = {
		# 			variable = merge_commanders
		# 			count > 0
		# 		}
		# 	}
		# 	clear_variable_list = merge_commanders
		# }
	}
}

PoW_army_select = {
	scope = character
	saved_scopes = { army }
	is_valid = {
		PoW_is_loaded_gather_commanders_enabled = yes
		exists = scope:army.army_owner
		scope:army.army_owner = root
	}
	effect = {
		if = {
			limit = { scope:army.army_owner = root }
			set_variable = {
				name = PoW_army_select
				value = scope:army
			}
			if = {
				limit = { exists = scope:army.army_commander }
				set_variable = {
					name = PoW_army_commander_select
					value = scope:army.army_commander
				}
			}
			else = { remove_variable = PoW_army_commander_select }
		}
	}
}

PoW_army_select_delete = {
	scope = character
	is_valid = { PoW_is_loaded_gather_commanders_enabled = yes }
	effect = {
		remove_variable = PoW_army_select
		remove_variable = PoW_army_commander_select
	}
}

PoW_commander_show = {
	scope = character
	saved_scopes = { army }
	is_valid = { PoW_is_loaded_gather_commanders_enabled = yes }
	effect = {
		remove_variable = commander_gather_eta
		if = {
			limit = { can_be_commander_now_trigger = { ARMY_OWNER = root }}
			add_to_list = gathering_commanders
			location = { add_to_list = starting_location }
		}
		every_courtier_or_guest = {
			limit = { can_be_commander_now_trigger = { ARMY_OWNER = root } is_ai = yes }
			remove_variable = commander_gather_eta
			add_to_list = gathering_commanders
			location = { add_to_list = starting_location }
		}
		every_vassal = {
			limit = { can_be_commander_now_trigger = { ARMY_OWNER = root } is_ai = yes }
			remove_variable = commander_gather_eta
			add_to_list = gathering_commanders
			location = { add_to_list = starting_location }
		}
		every_in_list = {
			list = starting_location
			limit = { NOT = { this = scope:army.location }}
			save_scope_as = starting_location
			create_character = {
				gender = male
				location = scope:starting_location
				culture = root.culture
				faith = root.faith
				after_creation = {
					set_location = scope:starting_location
					save_scope_as = eta_dummy
					start_travel_plan = {
						destination = scope:army.location
						players_use_planner = no
						return_trip = no
					}
					current_travel_plan = { add_travel_plan_modifier = PoW_gather_commanders }
				}
			}
			every_in_list = {
				list = gathering_commanders
				limit = { location = prev }
				set_variable = {
					name = commander_gather_eta
					value = {
						scope:eta_dummy.current_travel_plan = {
							every_future_path_location = {
								add = 1
							}
						}
						multiply = 10
					}
				}
			}
			scope:eta_dummy = {
				death = natural
			}
		}
	}
}

PoW_raise_all_before = {
	scope = character
	is_valid = { PoW_is_loaded_gather_commanders_enabled = yes }
	effect = {
		every_army = {
			set_variable = PoW_raise_before
		}
	}
}

PoW_raise_all_after = {
	scope = character
	is_valid = { PoW_is_loaded_gather_commanders_enabled = yes }
	effect = {
		every_army = {
			if = {
				limit = { has_variable = PoW_raise_before }
				remove_variable = PoW_raise_before
			}
			else = { remove_commander = yes }
		}
	}
}

PoW_disband_all = {
	scope = character
	effect = {
		every_army = {
			army_commander ?= {
				if = {
					limit = {
						PoW_is_loaded_gather_commanders_enabled = yes
						NOT = { owns_story_of_type = PoW_commander_story }
					}
					create_story = PoW_commander_story
				}
				var:commander_story ?= { end_story = yes }
			}
		}
	}
}

PoW_army_disband = {
	scope = army
	effect = {
		army_commander ?= {
			if = {
				limit = {
					PoW_is_loaded_gather_commanders_enabled = yes
					NOT = { owns_story_of_type = PoW_commander_story }
				}
				create_story = PoW_commander_story
			}
			var:commander_story ?= { end_story = yes }
		}
	}
}