﻿### Confederation Types ###

## NATO
TMA_nato_type = { #Made it's own type, so I can mix and change, same will be done for others.

	is_valid_member_character = {
		scope:character = {
			TMA_nato_goverments_allowed = yes 
			top_liege ?= this 
			TMA_has_nato_title_tier = yes 
		}
	}

	on_member_character_joined = {
		save_scope_as = confederation
		# Mark character and their vassals as new confederates.
		scope:character = {
			save_scope_as = joiner
			if = {
				limit = { has_character_flag = new_confederate }
				remove_character_flag = new_confederate
			}
			add_character_flag = {
				flag = new_confederate
				years = 3
			}
			every_vassal_or_below = {
				if = {
					limit = { has_character_flag = new_confederate }
					remove_character_flag = new_confederate
				}
				if = {
					limit = { highest_held_title_tier >= tier_county }
					add_character_flag = {
						flag = new_confederate
						years = 3
					}
				}
			}
		}
		# Notify all other members about them joining.
		every_player = {
			limit = { confederation = scope:confederation }
			send_interface_message = {
				type = msg_joined_confederation
				title = msg_joined_nato
				desc = msg_joined_nato_desc
				left_icon = scope:character
			}
		}
	}

	on_member_character_left = {
		save_scope_as = confederation
		scope:character = {
			save_scope_as = left_confederation
			if = {
				limit = { has_character_modifier = mpo_confederation_member_modifier }
				remove_character_modifier = mpo_confederation_member_modifier
			}
			if = {
				limit = { has_character_flag = new_confederate }
				remove_character_flag = new_confederate
			}
			every_vassal_or_below = {
				if = {
					limit = { has_character_modifier = mpo_confederation_member_modifier }
					remove_character_modifier = mpo_confederation_member_modifier
				}
				if = {
					limit = { has_character_flag = new_confederate }
					remove_character_flag = new_confederate
				}
			}
		}

		# If NATO is being destroyed, delete from global variable list
		if = {
			limit = {
				NOT = {
					any_confederation_member = { this != scope:left_confederation }
				}
			}
		}

		# Send notification for misc leaving
		if = {
			limit = {
				scope:left_confederation = {
					NOR = {
						has_character_flag = migrating_from_confederation
						has_variable = left_confederation
					}
				}
			}
			# Notify all members about them leaving.
			every_player = {
				limit = {
					confederation = scope:confederation
					this != scope:left_confederation
				}
				send_interface_message = {
					type = msg_broken_confederation
					title = msg_broken_nato
					desc = msg_broken_nato_desc
					left_icon = scope:left_confederation
				}
			}
			scope:left_confederation = {
			   send_interface_message = {
					type = msg_broken_confederation
					title = msg_broken_nato_root
					desc = msg_broken_nato_desc_root
					left_icon = confederation
			   }
			}
		}
	}
}

## CSTO
TMA_csto_type = { #Made it's own type, so I can mix and change, same will be done for others.

	is_valid_member_character = {
		scope:character = {
			TMA_csto_goverments_allowed = yes 
			top_liege ?= this 
			TMA_has_csto_title_tier = yes 
		}
	}

	on_member_character_joined = {
		save_scope_as = confederation
		# Mark character and their vassals as new confederates.
		scope:character = {
			save_scope_as = joiner
			if = {
				limit = { has_character_flag = new_confederate }
				remove_character_flag = new_confederate
			}
			add_character_flag = {
				flag = new_confederate
				years = 3
			}
			every_vassal_or_below = {
				if = {
					limit = { has_character_flag = new_confederate }
					remove_character_flag = new_confederate
				}
				if = {
					limit = { highest_held_title_tier >= tier_county }
					add_character_flag = {
						flag = new_confederate
						years = 3
					}
				}
			}
		}
		# Notify all other members about them joining.
		every_player = {
			limit = { confederation = scope:confederation }
			send_interface_message = {
				type = msg_joined_confederation
				title = msg_joined_csto
				desc = msg_joined_csto_desc
				left_icon = scope:character
			}
		}
	}

	on_member_character_left = {
		save_scope_as = confederation
		scope:character = {
			save_scope_as = left_confederation
			if = {
				limit = { has_character_modifier = mpo_confederation_member_modifier }
				remove_character_modifier = mpo_confederation_member_modifier
			}
			if = {
				limit = { has_character_flag = new_confederate }
				remove_character_flag = new_confederate
			}
			every_vassal_or_below = {
				if = {
					limit = { has_character_modifier = mpo_confederation_member_modifier }
					remove_character_modifier = mpo_confederation_member_modifier
				}
				if = {
					limit = { has_character_flag = new_confederate }
					remove_character_flag = new_confederate
				}
			}
		}

		# If CSTO is being destroyed, delete from global variable list
		if = {
			limit = {
				NOT = {
					any_confederation_member = { this != scope:left_confederation }
				}
			}
		}

		# Send notification for misc leaving
		if = {
			limit = {
				scope:left_confederation = {
					NOR = {
						has_character_flag = migrating_from_confederation
						has_variable = left_confederation
					}
				}
			}
			# Notify all members about them leaving.
			every_player = {
				limit = {
					confederation = scope:confederation
					this != scope:left_confederation
				}
				send_interface_message = {
					type = msg_broken_confederation
					title = msg_broken_csto
					desc = msg_broken_csto_desc
					left_icon = scope:left_confederation
				}
			}
			scope:left_confederation = {
			   send_interface_message = {
					type = msg_broken_confederation
					title = msg_broken_csto_root
					desc = msg_broken_csto_desc_root
					left_icon = confederation
			   }
			}
		}
	}
}