﻿on_title_gain = {
    on_actions = {
        deal_with_title_gain
    }
}

deal_with_title_gain = {
    effect = {
        if = {
            limit = {
                has_trait = oathkeeper
                num_of_relation_oathholder > 0
            }
            # Clear Boss Flags (if boss exists)
            # Transfer the vassal over to their new liege.
            create_title_and_vassal_change = {
                type = granted
                save_scope_as = change
                add_claim_on_loss = no
            }
            root = {
                change_liege = {
                    liege = var:val_oathholder
                    change = scope:change
                }
            }
            resolve_title_and_vassal_change = scope:change


            create_alliance = {
                target = var:val_oathholder
                allied_through_owner = root
                allied_through_target = var:val_oathholder
            }

            var:val_oathholder = {
                add_opinion = {
                    modifier = perk_negotiated_alliance_opinion
                    target = root
                }
            }


            remove_relation_oathholder = var:val_oathholder

            remove_trait = oathkeeper

            var:val_oathholder = {
                remove_trait = oathholder_male
                remove_trait = oathholder_female
            }


            if = {
                limit = { exists = var:val_oathholder }
                var:val_oathholder = {
                    remove_character_flag = oathholder_travel_job_set
                    remove_character_flag = oathholder_stationary_job_set
                    remove_character_flag = oathholder_sway_job_set
                    remove_character_flag = oathholder_kill_job_set
                    remove_character_flag = oathholder_duel_job_set
                    remove_character_flag = oathholder_prefers_duel
                    remove_character_flag = oathholder_prefers_murder
                    remove_character_flag = i_decided_kill_type
                }
            }

            # Specifically target the task county if it exists as a variable
            if = {
                limit = { has_variable = oathbound_control_task_county_var }
                var:oathbound_control_task_county_var = {
                    remove_county_modifier = oathbound_enforcing_order_weak
                    remove_county_modifier = oathbound_enforcing_order_normal
                    remove_county_modifier = oathbound_enforcing_order_strong
                }
            }

            remove_variable = oathbound_patrolled_county_var
            remove_variable = oathbound_control_task_county_var
            remove_variable = temp_patrol_effectiveness

            # Clear Character Variables
            remove_variable = val_oathholder
            remove_variable = val_oathkeeper
            remove_variable = val_oathholder_heir
            remove_variable = oathbound_sway_task_target_var
            remove_variable = oathbound_kill_task_target_var
            remove_variable = oathbound_duel_task_target_var

            # Clear Character Flags
            remove_character_flag = oathbound_on_travel_job
            remove_character_flag = oathbound_control_job
            remove_character_flag = oathbound_on_stationary_job
            remove_character_flag = oathbound_sway_job
            remove_character_flag = oathbound_kill_job
            remove_character_flag = oathbound_duel_job
            remove_character_flag = oathbound_kill_scheme_discovered


            trigger_event = oathbound_became_leader_events.0001
        }
    }
}