#################################
#
# THE REPUBLIC
# - found Maritime republic events
#
#################################

namespace = MBF
#namespace = REP

#Written by:
#Sergei Ivanov

narrative_event = {
	id = MBF.2
	title = EVTNAME_MBF_2
	desc = EVTDESC_MBF_2
	picture = GFX_evt_gathering_of_minds#LT.20060
	border = GFX_event_narrative_frame_diplomacy
	
	ai = no
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_MBF_2
	}
}

# Final check trigger from minor title
character_event = {
	id = MBF.3
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		if = {
			limit = {
				liege = {
					is_feudal = yes
					higher_tier_than = DUKE
					any_courtier_or_vassal = {
						count = 4
						has_minor_title = title_cornicularius
					}
					NOT = {
						num_of_government_vassals = {
							government = merchant_republic_government
							value = 1
						}
					}
				}
				capital_scope = {
					port = yes
					NOT = {
						top_liege = {
							any_war = {
								war_title = PREVPREV # The county
							}
						}
					}
				}
				capital_holding = { # rare, but in case of mayor holds castle as his capital
					holding_type = city
				}
			}
			set_character_flag = mission_successful
			liege = {
				character_event = { id = MBF.5 }
			}
		}
		else = {
			liege = {
				character_event = { id = MBF.40 }
			}
		}
	}
}

# Fail
narrative_event = {
	id = MBF.4
	title = EVTNAME_MBF_4
	desc = EVTDESC_MBF_4
	picture = GFX_evt_bad_news
	border = GFX_event_narrative_frame_diplomacy
	
	ai = no
	
	is_triggered_only = yes
	
	immediate = {
		remove_title = title_princeps_officii
		remove_title = title_cornicularius
		remove_title = title_cornicularius
		remove_title = title_cornicularius
		remove_title = title_cornicularius
		clr_character_flag = preparing_cash_cow
	}
	
	option = {
		name = EVTOPTA_LT_20434 #Curses
		custom_tooltip = {
			text = TT_DEAD_CASH_COW
		}
	}
}

# Succ
narrative_event = {
	id = MBF.5
	title = EVTNAME_MBF_5
	desc = EVTDESC_MBF_5
	picture = GFX_evt_family_feud_republic
	border = GFX_event_narrative_frame_diplomacy
	
	ai = no
	
	is_triggered_only = yes
	
	immediate = {
		any_vassal = { limit = { has_minor_title = title_princeps_officii }
			if = {
				limit = { dynasty = none }
				dynasty = father_bastard 
			}
			if = {
				limit = { 
					culture_group = israelite
					NOT = { culture_group = ROOT }
				}
				capital_scope = {
					add_province_modifier = {
						name = jewish_settlement_province
						duration = -1
					}
				}
			}
			create_family_palace = yes
			primary_title = {
				create_title = {
					tier = DUKE
					name = "TITULAR_REPUBLIK"
					landless = no
					holder = PREV
					custom_created = yes
					base_title = THIS
					short_name = yes
				}
			}
			set_government_type = merchant_republic_government
			save_event_target_as = new_doge
		}
		any_courtier_or_vassal = {
			count = 4
			limit = {
				has_minor_title = title_cornicularius
			}
			set_character_flag = mission_successful
			move_character = event_target:new_doge
			create_family_palace = yes
			wealth = 500
		}
		clr_character_flag = preparing_cash_cow
	}
	
	option = {
		name = EVTOPTA_MBF_5
		clear_delayed_event = { id = MER.40 }
	}
	option = {
		name = EVTOPTB_HFP_41054 # If only it could have been me...
		clear_delayed_event = { id = MER.40 }
        trigger = {
			has_dlc = "The Republic"
			event_target:new_doge = {
				ai = yes
				is_alive = yes
				dynasty = PREV
			}
		}
		custom_tooltip = {
			text = player_maritime_character_TT
			set_player_character = event_target:new_doge
		}
	}
}

# Clean up title_princeps_officii, ROOT is old liege and FROM is the mayor
character_event = {
	id = MBF.40
	hide_window = yes
	ai = no
	only_playable = yes
	is_triggered_only = yes
	
	immediate = {
		if = {
			limit = {
				FROM = {
					has_character_flag = mission_successful
				}
			}
			FROM = {
				clr_character_flag = mission_successful
			}
			break = yes
		}
		else = {
			clr_character_flag = preparing_cash_cow
			character_event = { id = MBF.4 }
		}
	}
}

# Clean up title_cornicularius, ROOT is old liege and FROM is the patrician
character_event = {
	id = MBF.41
	hide_window = yes
	ai = no
	only_playable = yes
	is_triggered_only = yes
	
	immediate = {
		if = {
			limit = {
				FROM = {
					has_character_flag = mission_successful
				}
			}
			FROM = {
				clr_character_flag = mission_successful
			}
			break = yes
		}
		else = { # notify old liege title lose
			liege = {
				character_event = { id = MBF.42 }
			}
		}
	}
}

character_event = {
	id = MBF.42
	desc = EVTDESC_MBF_42

	picture = GFX_evt_bad_news

	is_triggered_only = yes
	
	option = {
		name = EVTOPTAHF20058 # I see...
	}
}