﻿###################################
# Defend Caliphate Ending
###################################

# Make it a Pious playthrough that encourage the Caliph to get support from the Muslim world
struggle_persia_ending_assertion_decision = {
	decision_group_type = major
	title = struggle_persia_ending_assertion
	picture = {
		reference = "gfx/interface/illustrations/decisions/fp3/fp3_decision_supremacy.dds"
	}
	extra_picture = "gfx/interface/illustrations/struggle_decision_buttons/fp3_decision_strength.dds"
	desc = struggle_persia_ending_assertion_decision_desc
	selection_tooltip = struggle_persia_ending_assertion_tooltip
	confirm_click_sound = "event:/DLC/FP2/SFX/UI/fp2_struggle_ending_decision_confirm"
	is_invisible = yes
	sort_order = 80

	is_valid = {
		custom_tooltip = {
			text = struggle_persia_ending_tension_decision_correct_phase_tt
			any_character_struggle = {
				involvement = involved
				is_struggle_type = persian_struggle
				is_struggle_phase = struggle_persia_phase_stabilisation
			}
		}
		has_trait = fp3_struggle_supporter

		custom_tooltip = {
			text = struggle_persia_ending_decision_caliphate_exists_tt
			exists = title:e_arabia.holder
			title:d_sunni.holder ?= title:e_arabia.holder
		}

		# > 50% of involved rulers are supporters of the Caliph
		custom_tooltip = {
			text = struggle_persia_ending_assertion.l
			fp3_persia_supporter_rulers_count_value > fp3_persia_supporter_rulers_half_value
		}
		
		custom_tooltip = {
			text = ptt_persia_held_by_caliph_allies
			trigger_if = { limit = { is_ai = no }
				ptt_persia_owned_by_caliph_allies_decimal > 0.8
				}
				trigger_if = { limit = { is_ai = yes }
					ptt_persia_owned_by_caliph_allies_decimal > 0.7
				}
		}
		

		# They need to be super pious
		trigger_if = { limit = { is_ai = no }
			piety_level >= very_high_piety_level
		}
		trigger_if = { limit = { is_ai = yes }
			piety_level >= high_piety_level
		}

		# Specific to the caliph themselves
		trigger_if = {
			limit = { fp3_struggle_is_the_caliph = yes }
			# They need to fully control the abbasids empire
			custom_tooltip = {
				text = ptt_struggle_persia_ending_abbasid_arabia_owned_tt
				trigger_if = { limit = { is_ai = no }
					ptt_current_owned_arabia_decimal >= 0.9
				}
				trigger_if = { limit = { is_ai = yes }
					ptt_current_owned_arabia_decimal >= 0.75
				}
			}
		}
		trigger_else = {
			custom_tooltip = {
				text = struggle_persia_strengthen_decision_right_hof_tt
				faith.religious_head = title:d_sunni.holder
			}
			custom_tooltip = {
				text = struggle_persia_strengthen_decision_alliance_tt
				any_ally = {
					this = title:e_arabia.holder
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		trigger_if = { limit = { is_ai = no }
			is_available_adult = yes
		}
	}

	effect = {
		if = {
			limit = { has_dlc_feature = legends }
			legend_seed_struggle_ending_effect = {
				ENDER = root
				STRUGGLE = persian_struggle
			}
		}
			limit = { has_ach_dlc_trigger = yes }
			add_character_flag = ach_ended_struggle
		}
		show_as_tooltip = {
			fp3_ending_effects_assertion = yes
		}
		trigger_event = fp3_struggle.0071
		
		if = { # If you can adopt admin government, show a tooltip to explain it - Actual effect is executed in the event
			limit = {
				has_dlc_feature = roads_to_power
				fp3_struggle_is_the_caliph = yes
				NOT = { government_has_flag = government_is_administrative }
			}
			custom_tooltip = adopt_admin_government_tt
		}

		title:d_sunni = { remove_variable = struggle_block_dissolution_faction }
		title:e_arabia = { remove_variable = struggle_block_dissolution_faction }

	}
	
	ai_check_interval_by_tier = {
		barony = 0
		county = 0
		duchy = 36
		kingdom = 36
		empire = 36
		hegemony = 36
	}
	ai_potential = { always = yes }
	ai_will_do = { base = 100 }
}


###################################
# Humiliate Caliphate Ending
###################################

struggle_persia_ending_foundation_decision = {
	decision_group_type = major
	title = struggle_persia_ending_foundation
	picture = {
		reference = "gfx/interface/illustrations/decisions/fp3/fp3_decision_sunder.dds"
	}
	extra_picture = "gfx/interface/illustrations/struggle_decision_buttons/fp3_decision_temper.dds"
	desc = struggle_persia_ending_foundation_decision_desc
	selection_tooltip = struggle_persia_ending_foundation_tooltip
	confirm_click_sound = "event:/DLC/FP2/SFX/UI/fp2_struggle_ending_decision_confirm"
	is_invisible = yes
	sort_order = 80
	
	is_valid_showing_failures_only = {
		trigger_if = { limit = { is_ai = no }
			is_available_adult = yes
		}
		# Independent
		top_liege = this
		# Muslim
		custom_tooltip = {
			text = struggle_persia_ending_decision_caliphate_muslim_tt
			religion = religion:islam_religion
		}
		trigger_if = {
			limit = { exists = title:d_sunni.holder }
			NOT = { title:d_sunni.holder = root }
		}
		trigger_else = {
			custom_tooltip = {
				text = struggle_persia_strengthen_decision_right_hof_tt
				faith.religious_head = title:d_sunni.holder
			}
		}
		
		# HERE GOES THE SPECIFIC TRIGGERS FOR EACH OPTION - It goes here to make the buttons selectable (UI issue workaround)
		trigger_if = { # CHALLENGER
			limit = {  scope:struggle_persia_ending_foundation_decision_option_shia = yes }
			# Only valid if faith permits HoF
			custom_tooltip = {
				text = struggle_persia_ending_foundation_decision_option_hof_tt
				NOT = {
					faith = { has_doctrine = doctrine_no_head }
				}
			}
		}
	}

	is_valid = { # We can skip some of the more elementary checks due to this being struggle-only
		# Involved & Unrest phase
		custom_tooltip = {
			text = struggle_persia_ending_anarchy_decision_correct_phase_tt
			any_character_struggle = {
				involvement = involved
				is_struggle_type = persian_struggle
				is_struggle_phase = struggle_persia_phase_unrest
			}
		}
		
		# Detractor
		has_trait = fp3_struggle_detractor
		trigger_if = { # CHALLENGER
			limit = {  scope:struggle_persia_ending_foundation_decision_option_shia = yes }
			# Only valid if not Ashari
			custom_tooltip = {
				text = struggle_persia_ending_foundation_decision_option_succession_tt
				NOT = {
					faith = { has_doctrine = muhammad_succession_sunni_doctrine }
				}
			}
		}
		
		# King or above
		highest_held_title_tier >= tier_kingdom
		
		# own faith controls more than threshold of persia
		custom_tooltip = {
			text = ptt_persia_faith_of_self
			trigger_if = { limit = { is_ai = no }
				ptt_persia_faith_of_self_decimal >= 0.4
			}
			trigger_if = { limit = { is_ai = yes }
				ptt_persia_faith_of_self_decimal >= 0.35
			}
		}
			
		# At least 2 king tier allies
		OR = {
			custom_tooltip = {
				text = struggle_persia_ending_foundation_decision_nb_ally_tt
				any_ally = {
					count >= 2
					highest_held_title_tier >= tier_kingdom
				}
			}
			trigger_if = { limit = { is_ai = no }
				sub_realm_size >= 50
			}
			trigger_if = { limit = { is_ai = yes }
				sub_realm_size >= 40
			}
		}

		# HERE GOES THE SPECIFIC TRIGGERS FOR EACH OPTION - It goes here to make the buttons selectable (UI issue workaround)
		trigger_if = { # CHALLENGER - if you change this, also change the `ai_chance = {}` of the specific widget option below!
			limit = {  scope:struggle_persia_ending_foundation_decision_option_shia = yes }
			
			
			
			# Realm must be bigger than the Caliphate within Persia
			trigger_if = {
				limit = {
					exists = title:d_sunni.holder
				}
				custom_tooltip = {
					text = ptt_larger_realm_than_caliph
					ptt_held_counties_in_persia >= ptt_held_counties_in_persia_caliph
				}
			}
			
			OR = {
				any_realm_province = {
					OR = {
						custom_tooltip = {
							text = holy_site_of_root_faith_tt
							barony = {
								is_holy_site_of = root.faith
							}
						}
						has_building_or_higher = the_friday_mosque_01
						has_building_or_higher = house_of_wisdom_01
						has_building_or_higher = holy_site_great_mosque_of_samarra_01
						trigger_if = {
							limit = {
								root.faith = {
									has_doctrine = muhammad_succession_shia_doctrine
								}
							}
							has_building_or_higher = holy_site_imam_ali_mosque_01
						}
						trigger_if = {
							limit = {
								root.faith = {
									has_doctrine = muhammad_succession_shia_doctrine
								}
							}
							has_building_or_higher = imam_reza_shrine_01
						}
					}
				}
			}
			# Muhakkima
			trigger_if = {
				limit = {
					faith = { has_doctrine = muhammad_succession_muhakkima_doctrine }
				}
				fp3_valid_muhakkima_head = yes
			}
			# Shia/Zandaqa
			trigger_else = { fp3_valid_shia_head = yes }
			
			# HoF does not already exist, or isn't of your faith
			OR = {
				custom_tooltip = {
					text = hof_must_not_exist_tt
					NOT = { exists = faith.religious_head }
				}
				custom_tooltip = {
					text = hof_must_not_be_your_faith_tt
					faith.religious_head.faith != root.faith
				}
				faith.religious_head = root
			}
		}
		trigger_if = { # VASSALIZE - if you change this, also change the `ai_chance = {}` of the specific widget option below!
			limit = { scope:struggle_persia_ending_foundation_decision_option_vassalize = yes }
			
			
			# Piety
			piety_level >= high_piety_level
			
			# Sunni Caliph still holds Arabia
			trigger_if = {
				limit = {
				exists = title:d_sunni.holder
				exists = title:e_arabia.holder
				title:d_sunni.holder = title:e_arabia.holder
				}
				
				# Realm must be bigger than the Caliphate
				trigger_if = {
					limit = {
						exists = title:d_sunni.holder
					}
					custom_tooltip = {
						text = larger_realm_than_caliph
						sub_realm_size >= title:d_sunni.holder.realm_size
					}
				}
				
				# Control historic capital of Caliph
				completely_controls = title:d_samarra
				OR = {
					completely_controls = title:c_baghdad
					title:d_sunni.holder = { is_imprisoned_by = root }
					has_hook = title:d_sunni.holder
				}
			}
			# iv doesnt hold arabia, grant claim instead
			trigger_else = {
				
				# Control historic capital of Caliph
				completely_controls = title:c_samarra
				OR = {
					completely_controls = title:c_baghdad
					title:d_sunni.holder = { is_imprisoned_by = root }
					has_strong_hook = title:d_sunni.holder
					# new: or he is your vassal
					title:d_sunni.holder.liege = root
				}
			}
		}
	}

	widget = {
		gui = "decision_view_widget_fp3_foundation_ending"
		controller = decision_option_list_controller
		decision_to_second_step_button = "struggle_persia_ending_foundation_button_text"
		show_from_start = yes

		item = { # SHIA CALIPHATE
			value = struggle_persia_ending_foundation_decision_option_shia

			current_description = struggle_persia_ending_foundation_decision_option_shia_desc
			localization = struggle_persia_ending_foundation_decision_option_shia
			icon = "gfx/interface/icons/faith/shia.dds"

			# ADD THE OPTION-SPECIFIC REQUIREMENTS FOR THE OPTION - This makes the AI actually be select the 'second' option if it is available
            # ALSO - This also fixes alerts for Players, since their default option will be the one with the highest score
			ai_chance = {
				value = ai_zeal

				if = { # CHALLENGER - if you change this, also change the `is_valid = {}` of the decision!
	                limit = {
	                    OR = {
	                        any_realm_province = {
	                            OR = {
	                                custom_tooltip = {
	                                    text = holy_site_of_root_faith_tt
	                                    barony = {
	                                        is_holy_site_of = root.faith
	                                    }
	                                }
	                                has_building_or_higher = the_friday_mosque_01
	                                has_building_or_higher = house_of_wisdom_01
	                                has_building_or_higher = holy_site_great_mosque_of_samarra_01
	                                trigger_if = {
	                                    limit = {
	                                        root.faith = {
	                                            has_doctrine = muhammad_succession_shia_doctrine
	                                        }
	                                    }
	                                    has_building_or_higher = holy_site_imam_ali_mosque_01
	                                }
	                                trigger_if = {
	                                    limit = {
	                                        root.faith = {
	                                            has_doctrine = muhammad_succession_shia_doctrine
	                                        }
	                                    }
	                                    has_building_or_higher = imam_reza_shrine_01
	                                }
	                            }
	                        }
	                    }
	                    # Muhakkima
	                    trigger_if = {
	                        limit = {
	                            faith = { has_doctrine = muhammad_succession_muhakkima_doctrine }
	                        }
	                        fp3_valid_muhakkima_head = yes
	                    }
	                    # Shia/Zandaqa
	                    trigger_else = { fp3_valid_shia_head = yes }
	                    # HoF does not already exist, or isn't of your faith
	                    OR = {
	                        custom_tooltip = {
	                            text = hof_must_not_exist_tt
	                            NOT = { exists = faith.religious_head }
	                        }
	                        custom_tooltip = {
	                            text = hof_must_not_be_your_faith_tt
	                            faith.religious_head.faith != root.faith
	                        }
	                        faith.religious_head = root
	                    }
	                }
	                add = 100
	            }
			}
 		}

		item = { # VASSALIZE THE CALIPH
			value = struggle_persia_ending_foundation_decision_option_vassalize

			current_description = struggle_persia_ending_foundation_decision_option_vassalize_desc
			localization = struggle_persia_ending_foundation_decision_option_vassalize
			icon = "gfx/interface/icons/faith/sunni.dds"

			# ADD THE OPTION-SPECIFIC REQUIREMENTS FOR THE OPTION - This makes the AI actually be select the 'second' option if it is available
       		# ALSO - This also fixes alerts for Players, since their default option will be the one with the highest score
			ai_chance = {
				value = ai_zeal

	            if = { # VASSALIZE - if you change this, also change the `is_valid = {}` of the decision!
                    limit = {
                        # Piety
						piety_level >= high_piety_level
			
						# Sunni Caliph still holds Arabia
						trigger_if = {
							limit = {
							exists = title:d_sunni.holder
							exists = title:e_arabia.holder
							title:d_sunni.holder = title:e_arabia.holder
							}
				
							# Realm must be bigger than the Caliphate
							trigger_if = {
								limit = {
									exists = title:d_sunni.holder
								}
								custom_tooltip = {
									text = larger_realm_than_caliph
									sub_realm_size >= title:d_sunni.holder.realm_size
								}
							}
				
							# Control historic capital of Caliph
							completely_controls = title:d_samarra
							OR = {
								completely_controls = title:c_baghdad
								title:d_sunni.holder = { is_imprisoned_by = root }
								has_hook = title:d_sunni.holder
							}
						}
						# iv doesnt hold arabia, grant claim instead
						trigger_else = {
							
							# Control historic capital of Caliph
							completely_controls = title:c_samarra
							OR = {
								completely_controls = title:c_baghdad
								title:d_sunni.holder = { is_imprisoned_by = root }
								has_strong_hook = title:d_sunni.holder
								# new: or he is your vassal
								title:d_sunni.holder.liege = root
							}
						}
					}
                    add = 100
				}
            }
		}
	}

	effect = {
		if = {
			limit = { has_dlc_feature = legends }
			legend_seed_struggle_ending_effect = {
				ENDER = root
				STRUGGLE = persian_struggle
			}
		}
		ptt_ext_intermezzo_capital_damascus = yes
		if = {
			limit = { has_ach_dlc_trigger = yes }
			add_character_flag = ach_ended_struggle
		}
		if = { # Shia Caliphate
			limit = { scope:struggle_persia_ending_foundation_decision_option_shia = yes }
			custom_tooltip = struggle_persia_ending_foundation_decision_option.t.shia

			trigger_event = fp3_struggle.0095

			show_as_tooltip = {
				fp3_struggle_ending_shia_caliphate_effects = yes
				struggle:persian_struggle = { end_struggle = yes }
			}
		}
		else_if = { # Vassalize Caliphate
			limit = { scope:struggle_persia_ending_foundation_decision_option_vassalize = yes }
			custom_tooltip = struggle_persia_ending_foundation_decision_option.t.vassalize
			
			if = { # caliph holds arabia, standard effects
				limit = {
					exists = title:d_sunni.holder
					exists = title:e_arabia.holder
					title:d_sunni.holder = title:e_arabia.holder
				}

				trigger_event = fp3_struggle.0100

				show_as_tooltip = {
					fp3_struggle_ending_vassalize_caliph_effects = yes
					struggle:persian_struggle = { end_struggle = yes }
				}
			}
			
			else_if = { # caliph is independant
				limit =  {
					exists = title:d_sunni.holder
					title:d_sunni.holder = { is_independent_ruler = yes }
				}
				
				trigger_event = ptt_struggle.0101
				
				show_as_tooltip = {
					ptt_ext_intermezzo_vassalize_caliph = yes
					ptt_ext_intermezzo_dominate_caliph_fame_and_claim = yes
					struggle:persian_struggle = { end_struggle = yes }
				}
			}
			
			else = { # caliph is someones vassal or doesnt exist
			
				trigger_event = ptt_struggle.0102
				
				show_as_tooltip = {
					ptt_ext_intermezzo_dominate_caliph_fame_and_claim = yes
					struggle:persian_struggle = { end_struggle = yes }
				}
				
			}
		}
		else = {
			custom_tooltip = struggle_persia_ending_foundation_decision_option.t_selected_foundation.none # QoL addition
			assert_if = { # Just in case
				limit = { always = yes }
				text = "Persian Struggle ended with foundation ending but had an invalid or missing widget item value!"
			}
		}

		title:d_sunni = { remove_variable = struggle_block_dissolution_faction }
		title:e_arabia = { remove_variable = struggle_block_dissolution_faction }
		# Silk Road bonus towards prosperity if and when Persia's turmoil comes to an end
		tgp_silk_road_iranian_intermezzo_ending_effect = yes							  
	}
	
	# Kingdom+ only
	ai_check_interval_by_tier = {
		barony = 0
		county = 0
		duchy = 0
		kingdom = 36
		empire = 36
		hegemony = 36
	ai_potential = { always = yes }
	ai_will_do = { base = 100 }
}

###################################
# Resurgent Persia Ending
###################################

struggle_persia_ending_rekindle_iran_decision = {
	decision_group_type = major
	title = struggle_persia_ending_rekindle_iran
	picture = {
		reference = "gfx/interface/illustrations/decisions/fp3/fp3_decision_foundation.dds"
	}
	extra_picture = "gfx/interface/illustrations/struggle_decision_buttons/fp3_decision_foundation.dds"
	desc = struggle_persia_ending_rekindle_iran_decision_desc
	selection_tooltip = struggle_persia_ending_rekindle_iran_tooltip
	confirm_click_sound = "event:/DLC/FP2/SFX/UI/fp2_struggle_ending_decision_confirm"
	is_invisible = yes
	sort_order = 80

	is_valid_showing_failures_only = {
		trigger_if = { limit = { is_ai = no }
			is_available_adult = yes
		}
		# Independent
		top_liege = this
	}

	is_valid = { # We can skip some of the more elementary checks due to this being struggle-only
		# Involved
		custom_tooltip = {
			text = struggle_ending_decision_correct_involvement_tt
			any_character_struggle = {
				involvement = involved
				is_struggle_type = persian_struggle
			}
		}
		
		# Iranian
		custom_tooltip = {
			text = struggle_persia_ending_rekindle_iran_decision_heritage_tt
			culture = { has_cultural_pillar = heritage_iranian }
		}
		
		# At least a king
		highest_held_title_tier >= tier_kingdom
		
		# Famed
		prestige_level >= very_high_prestige_level
		
		# Control enough of Persia
		custom_tooltip = {
			text = ptt_struggle_persia_ending_rekindle_iran_decision_region_control_tt
			any_county_in_region = {
				region = world_persian_empire
				percent >= fp3_persia_owned_for_rekindle_persia_decimal_value
				holder.top_liege = root
			}
		}
		OR = {
			# Iranian allies
			custom_tooltip = {
				text = struggle_persia_ending_rekindle_iran_decision_nb_ally_tt
				any_ally = {
					count >= 2
					top_liege = this
					primary_title = { is_mercenary_company = no }
					primary_title = { is_holy_order = no }
					highest_held_title_tier = tier_kingdom
					culture = { has_cultural_pillar = heritage_iranian }
				}
			}
			custom_tooltip = {
				text = struggle_persia_ending_rekindle_iran_decision_iranian_sub_realm_counties_tt
				any_sub_realm_county = {
					count >= 40
					culture = { has_cultural_pillar = heritage_iranian }
				}
			}
		}
		# Iranian rulers
		custom_tooltip = {
			text = ptt_struggle_persia_ending_rekindle_iran_decision_iranian_rulers_tt
			trigger_if = { limit = { is_ai = no }
				ptt_persia_owned_by_iranians_decimal >= 0.6
			}
			trigger_if = { limit = { is_ai = yes }
				ptt_persia_owned_by_iranians_decimal >= 0.55
			}
		}

		trigger_if = {
			limit = { scope:struggle_persia_ending_rekindle_iran_decision_option_secret_faith = yes }
			custom_tooltip = {
				text = struggle_persia_ending_rekindle_iran_decision_secret_tt
				any_secret = { type = secret_crypto_religionist }
			}
		}
	}

	widget = {
		gui = "decision_view_widget_fp3_foundation_ending"
		controller = decision_option_list_controller
		decision_to_second_step_button = "struggle_persia_ending_rekindle_iran_button_text"

		item = { # PUBLIC FAITH
			value = struggle_persia_ending_rekindle_iran_decision_option_public_faith

			current_description = struggle_persia_ending_rekindle_iran_decision_option_public_faith_desc
			localization = struggle_persia_ending_rekindle_iran_decision_option_public_faith
			icon = "gfx/interface/icons/character_interactions/religious.dds"
			ai_chance = { value = ai_zeal } #todo_cd_polish
 		}

		item = { # SECRET FAITH
			value = struggle_persia_ending_rekindle_iran_decision_option_secret_faith
			is_valid = {
				exists = secret_faith
			}
			current_description = struggle_persia_ending_rekindle_iran_decision_option_secret_faith_desc
			localization = struggle_persia_ending_rekindle_iran_decision_option_secret_faith
			icon = "gfx/interface/icons/character_interactions/icon_scheme_crypto_religion.dds"
			ai_chance = { value = ai_zeal } #todo_cd_polish
		}
	}

	effect = {
		if = {
			limit = { has_dlc_feature = legends }
			legend_seed_struggle_ending_effect = {
				ENDER = root
				STRUGGLE = persian_struggle
			}
		}
		
		ptt_ext_intermezzo_capital_damascus = yes
		
		if = {
			limit = { has_ach_dlc_trigger = yes }
			add_character_flag = ach_ended_struggle
		}
		if = {
			limit = { scope:struggle_persia_ending_rekindle_iran_decision_option_secret_faith = yes }
			if = {
				limit = { exists = secret_faith }
				secret_faith = { save_scope_as = propagated_faith }
			}
			else = {
				faith =  { save_scope_as = propagated_faith }
			}
		}
		else = {
			faith =  { save_scope_as = propagated_faith }
		}
		trigger_event = fp3_struggle.0091
		show_as_tooltip = { # Fake effects, custom-made to be readable (and also a summary of what happens across several events for OOP/UX reasons)
			struggle:persian_struggle = { end_struggle = yes }

			fp3_struggle_rekindle_iran_effects = yes
		}

		title:d_sunni = { remove_variable = struggle_block_dissolution_faction }
		title:e_arabia = { remove_variable = struggle_block_dissolution_faction }
		# Silk Road bonus towards prosperity if and when Persia's turmoil comes to an end
		tgp_silk_road_iranian_intermezzo_ending_effect = yes
	}

	# Kingdom+ only
	ai_check_interval_by_tier = {
		barony = 0
		county = 0
		duchy = 0
		kingdom = 36
		empire = 36
		hegemony = 36
	}
	ai_potential = { always = yes }
	ai_will_do = { base = 100 }
}

