namespace = population_bureau

# usurp the capital holding when inheriting or usurping an empire title with imperial administration
character_event = {
	id = population_bureau.1
	hide_window = yes
	is_triggered_only = yes
	desc = POPBUREAU1_DESC

    immediate = {
        character_event = { id = population_bureau.2 days = 1 }
    }

}

character_event = {
	id = population_bureau.2
	is_triggered_only = yes
	desc = POPBUREAU1_DESC

	trigger = { 
		FROMFROM = {
			higher_tier_than = COUNT
			is_primary_holder_title = yes
            has_law = ze_administration_laws_2
            persistent_event_target:capital_of_title = { holder_scope = { 
                any_liege = { character = ROOT }
                NOT = { character = ROOT }
            } }
		}
	}
    
    immediate = {
        ROOT = {
            capital_holding = {
                save_event_target_as = personal_capital
            }
        }
    }

	option = {
		name = POPBUREAU1_OP1
		ai_chance = {
			factor = 1
		}

        FROMFROM = { 
            persistent_event_target:capital_of_title = {
                ROOT = { 
                    usurp_title_only = PREV 
                    capital = PREV
                }
            }
        }
	}
    option = {
		name = POPBUREAU1_OP2
		ai_chance = {
			factor = 0
		}
        FROM = { 
            save_persistent_event_target = { name = capital_of_title scope = event_target:personal_capital }
        }
	}
}

character_event = {
	id = population_bureau.3
	is_triggered_only = yes
    desc = POP35EVENTDESC
    
    trigger = {
        had_global_flag = {
            flag = population_setting_ran 
            years >= 1
        }
    }

    immediate = {
        # save the cost variables
        event_target:capital_holding_bureaucracy = {
            set_variable = { which = local_admin_supply which = admin_prod }
            set_variable = { which = local_admin_cost which = admin_cost }
            set_variable = { which = local_admin_cost_pay which = admin_cost }
            multiply_variable = { which = local_admin_cost_pay value = -1 }
        }
    }

    option = {
		name = POP35EVENTOP1
        trigger = {
            wealth > 0
        }
		ai_chance = {
			factor = 10
			modifier = {
				factor = 0
                wealth < local_admin_cost
                war = yes
			}
		}
        wealth = local_admin_cost_pay
    }
    
    option = {
		name = POP35EVENTOP2
		ai_chance = {
			factor = 1
			modifier = {
				factor = 0
                wealth > local_admin_cost
			}
		}
        # delete the current building
        ROOT = { primary_title = {
            trigger_switch = { 
                on_trigger = has_law
                centralization_1 = { 
                    add_law = {
                        law = centralization_0
                        cooldown = no
                        opinion_effect = no
                    }
                }
                centralization_2 = { 
                    add_law = {
                        law = centralization_1
                        cooldown = no
                        opinion_effect = no
                    }
                }
                centralization_3 = { 
                    add_law = {
                        law = centralization_2
                        cooldown = no
                        opinion_effect = no
                    }
                }
                centralization_4 = { 
                    add_law = {
                        law = centralization_3
                        cooldown = no
                        opinion_effect = no
                    }
                }
                centralization_5 = { 
                    add_law = {
                        law = centralization_4
                        cooldown = no
                        opinion_effect = no
                    }
                }
            }
        } }
        wealth = local_admin_cost_pay
    }
}

character_event = {
	id = population_bureau.4
	is_triggered_only = yes
    hide_window = yes
    
    trigger = { 
		FROM = {
			tier = BARON
            holding_type = family_palace
		}
	}

    immediate = {
        character_event = { id = population_bureau.5 days = 1 }
    }
    
}

character_event = {
	id = population_bureau.5
	is_triggered_only = yes
    hide_window = yes

	trigger = { 
		ROOT = { 
            NOT = { is_merchant_republic = yes }
            any_demesne_title = {
                higher_tier_than = BARON
            }
        }
	}
    
    immediate = {
        ROOT = {
            any_demesne_title = {
                limit = { higher_tier_than = BARON }
                ROOT = { liege = { 
                    grant_title_no_opinion = PREVPREV 
                } }
            }
        }
    }
    
}

character_event = {
	id = population_bureau.6
	is_triggered_only = yes
    hide_window = yes
    
    trigger = { 
		FROMFROM = {
			government = roman_imperial_government
			higher_tier_than = BARON
		}
        FROM = { is_primary_holder_title = yes }
	}

    immediate = {
		FROMFROM = {
            any_demesne_title = {
                limit = { 
                    higher_tier_than = BARON 
                    NOT = { title = PREV }
                }
                
                ROOT = { 
                    grant_title_no_opinion = {
						target = PREV
						type = election
					}
                }
            }
        }
    }
}