﻿#Change
# 重建为城堡
qing_htool_holding_change_castle = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            OR = {
                has_game_rule = qing_htool_main_switch_holding_visible_on
                exists = province_owner
            }
        }
        has_game_rule = qing_htool_main_switch_holding_feudalize_on
        qing_htool_main_scope_value = 1
    }

    is_valid = {
        scope:gui_holding = {
            has_ongoing_construction = no
            NOT = { has_holding_type = castle_holding }
            # 冷却检查
            trigger_if = {
                limit = { qing_htool_holding_change_cooldown_value > 0 }
                NOT = { has_province_modifier = qing_htool_holding_change_cooldown_modifier }
            }
        }
        # 金钱检查
        trigger_if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            trigger_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root.gold >= qing_htool_holding_change_cost_value
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    root.treasury >= qing_htool_holding_change_cost_value
                }
                trigger_else = {
                    root.gold >= qing_htool_holding_change_cost_value
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_has_treasury_error"
                        OR = {
                            root.gold >= qing_htool_holding_change_cost_value
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                    }
                }
                trigger_else = {
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_no_treasury_error"
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                custom_tooltip = {
                    text = "qing_htool_holding_change_pay_method_treasury_first_error"
                    OR = {
                        AND = {
                            root = { has_treasury = yes }
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
        }
    }

    effect = {
        scope:gui_holding = {
            set_holding_type = castle_holding
            # 冷却修正
            if = {
                limit = {
                    qing_htool_holding_change_cooldown_value > 0
                }
                add_province_modifier = {
                    modifier = qing_htool_holding_change_cooldown_modifier
                    months = qing_htool_holding_change_cooldown_value
                }
            }
        }
        # 负面影响修正
        if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_low }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_low_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_medium }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_medium_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_high }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_high_modifier
                    years = 1
                }
            }
        }
        # 花费
        if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                if = {
                    limit = { root.gold >= qing_htool_holding_change_cost_value }
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
                else_if = {
                    limit = {
                        root = { has_treasury = yes }
                        root.treasury >= qing_htool_holding_change_cost_value
                    }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
        }
        custom_tooltip = "qing_htool_holding_change_tip"
    }
}

# 重建为城市
qing_htool_holding_change_city = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            OR = {
                has_game_rule = qing_htool_main_switch_holding_visible_on
                exists = province_owner
            }
        }
        has_game_rule = qing_htool_main_switch_holding_feudalize_on
        qing_htool_main_scope_value = 1
    }

    is_valid = {
        scope:gui_holding = {
            has_ongoing_construction = no
            NOT = { has_holding_type = city_holding }
            trigger_if = {
                limit = { qing_htool_holding_change_cooldown_value > 0 }
                NOT = { has_province_modifier = qing_htool_holding_change_cooldown_modifier }
            }
        }
        trigger_if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            trigger_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root.gold >= qing_htool_holding_change_cost_value
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    root.treasury >= qing_htool_holding_change_cost_value
                }
                trigger_else = {
                    root.gold >= qing_htool_holding_change_cost_value
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_has_treasury_error"
                        OR = {
                            root.gold >= qing_htool_holding_change_cost_value
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                    }
                }
                trigger_else = {
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_no_treasury_error"
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                custom_tooltip = {
                    text = "qing_htool_holding_change_pay_method_treasury_first_error"
                    OR = {
                        AND = {
                            root = { has_treasury = yes }
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
        }
    }

    effect = {
        scope:gui_holding = {
            set_holding_type = city_holding
            if = {
                limit = {
                    qing_htool_holding_change_cooldown_value > 0
                }
                add_province_modifier = {
                    modifier = qing_htool_holding_change_cooldown_modifier
                    months = qing_htool_holding_change_cooldown_value
                }
            }
        }
        if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_low }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_low_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_medium }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_medium_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_high }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_high_modifier
                    years = 1
                }
            }
        }
        if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                if = {
                    limit = { root.gold >= qing_htool_holding_change_cost_value }
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
                else_if = {
                    limit = {
                        root = { has_treasury = yes }
                        root.treasury >= qing_htool_holding_change_cost_value
                    }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
        }
        custom_tooltip = "qing_htool_holding_change_tip"
    }
}

# 重建为神殿
qing_htool_holding_change_church = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            OR = {
                has_game_rule = qing_htool_main_switch_holding_visible_on
                exists = province_owner
            }
        }
        has_game_rule = qing_htool_main_switch_holding_feudalize_on
        qing_htool_main_scope_value = 1
    }

    is_valid = {
        scope:gui_holding = {
            has_ongoing_construction = no
            # 曼荼罗制检查寺庙城塞，否则检查神殿
            trigger_if = {
                limit = {
                    root = { government_has_flag = government_is_mandala }
                }
                NOT = { has_holding_type = temple_citadel_holding }
            }
            trigger_else = {
                NOT = { has_holding_type = church_holding }
            }
            trigger_if = {
                limit = { qing_htool_holding_change_cooldown_value > 0 }
                NOT = { has_province_modifier = qing_htool_holding_change_cooldown_modifier }
            }
        }
        trigger_if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            trigger_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root.gold >= qing_htool_holding_change_cost_value
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    root.treasury >= qing_htool_holding_change_cost_value
                }
                trigger_else = {
                    root.gold >= qing_htool_holding_change_cost_value
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_has_treasury_error"
                        OR = {
                            root.gold >= qing_htool_holding_change_cost_value
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                    }
                }
                trigger_else = {
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_no_treasury_error"
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                custom_tooltip = {
                    text = "qing_htool_holding_change_pay_method_treasury_first_error"
                    OR = {
                        AND = {
                            root = { has_treasury = yes }
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
        }
    }

    effect = {
        scope:gui_holding = {
            # 曼荼罗制 → 寺庙城塞
            if = {
                limit = {
                    root = { government_has_flag = government_is_mandala }
                }
                set_holding_type = temple_citadel_holding
            }
            else = {
                set_holding_type = church_holding
            }
        }
        if = {
            limit = {
                qing_htool_holding_change_cooldown_value > 0
            }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_cooldown_modifier
                    months = qing_htool_holding_change_cooldown_value
                }
            }
        }
        if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_low }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_low_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_medium }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_medium_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_high }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_high_modifier
                    years = 1
                }
            }
        }
        if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                if = {
                    limit = { root.gold >= qing_htool_holding_change_cost_value }
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
                else_if = {
                    limit = {
                        root = { has_treasury = yes }
                        root.treasury >= qing_htool_holding_change_cost_value
                    }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
        }
        custom_tooltip = "qing_htool_holding_change_tip"
    }
}

# 重建为部落地产
qing_htool_holding_change_tribal = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            OR = {
                has_game_rule = qing_htool_main_switch_holding_visible_on
                exists = province_owner
            }
        }
        has_game_rule = qing_htool_main_switch_holding_tribalize_on
        qing_htool_main_scope_value = 1
    }

    is_valid = {
        scope:gui_holding = {
            has_ongoing_construction = no
            NOT = { has_holding_type = tribal_holding }
            trigger_if = {
                limit = { qing_htool_holding_change_cooldown_value > 0 }
                NOT = { has_province_modifier = qing_htool_holding_change_cooldown_modifier }
            }
        }
        trigger_if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            trigger_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root.gold >= qing_htool_holding_change_cost_value
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    root.treasury >= qing_htool_holding_change_cost_value
                }
                trigger_else = {
                    root.gold >= qing_htool_holding_change_cost_value
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_has_treasury_error"
                        OR = {
                            root.gold >= qing_htool_holding_change_cost_value
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                    }
                }
                trigger_else = {
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_no_treasury_error"
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                custom_tooltip = {
                    text = "qing_htool_holding_change_pay_method_treasury_first_error"
                    OR = {
                        AND = {
                            root = { has_treasury = yes }
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
        }
    }

    effect = {
        scope:gui_holding = {
            set_holding_type = tribal_holding
            if = {
                limit = {
                    qing_htool_holding_change_cooldown_value > 0
                }
                add_province_modifier = {
                    modifier = qing_htool_holding_change_cooldown_modifier
                    months = qing_htool_holding_change_cooldown_value
                }
            }
        }
        if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_low }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_low_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_medium }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_medium_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_high }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_high_modifier
                    years = 1
                }
            }
        }
        if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                if = {
                    limit = { root.gold >= qing_htool_holding_change_cost_value }
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
                else_if = {
                    limit = {
                        root = { has_treasury = yes }
                        root.treasury >= qing_htool_holding_change_cost_value
                    }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
        }
        custom_tooltip = "qing_htool_holding_change_tip"
    }
}

# 重建为游牧聚落
qing_htool_holding_change_nomad = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            OR = {
                has_game_rule = qing_htool_main_switch_holding_visible_on
                exists = province_owner
            }
        }
        has_game_rule = qing_htool_main_switch_holding_nomadize_on
        qing_htool_main_scope_value = 1
    }

    is_valid = {
        scope:gui_holding = {
            has_ongoing_construction = no
            NOT = { has_holding_type = nomad_holding }
            trigger_if = {
                limit = { qing_htool_holding_change_cooldown_value > 0 }
                NOT = { has_province_modifier = qing_htool_holding_change_cooldown_modifier }
            }
        }
        trigger_if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            trigger_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root.gold >= qing_htool_holding_change_cost_value
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    root.treasury >= qing_htool_holding_change_cost_value
                }
                trigger_else = {
                    root.gold >= qing_htool_holding_change_cost_value
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_has_treasury_error"
                        OR = {
                            root.gold >= qing_htool_holding_change_cost_value
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                    }
                }
                trigger_else = {
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_no_treasury_error"
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                custom_tooltip = {
                    text = "qing_htool_holding_change_pay_method_treasury_first_error"
                    OR = {
                        AND = {
                            root = { has_treasury = yes }
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
        }
    }

    effect = {
        scope:gui_holding = {
            set_holding_type = nomad_holding
            if = {
                limit = {
                    qing_htool_holding_change_cooldown_value > 0
                }
                add_province_modifier = {
                    modifier = qing_htool_holding_change_cooldown_modifier
                    months = qing_htool_holding_change_cooldown_value
                }
            }
        }
        if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_low }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_low_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_medium }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_medium_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_high }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_high_modifier
                    years = 1
                }
            }
        }
        if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                if = {
                    limit = { root.gold >= qing_htool_holding_change_cost_value }
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
                else_if = {
                    limit = {
                        root = { has_treasury = yes }
                        root.treasury >= qing_htool_holding_change_cost_value
                    }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
        }
        custom_tooltip = "qing_htool_holding_change_tip"
    }
}

# 重建为牧民定居点
qing_htool_holding_change_herder = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            OR = {
                has_game_rule = qing_htool_main_switch_holding_visible_on
                exists = province_owner
            }
        }
        has_game_rule = qing_htool_main_switch_holding_nomadize_on
        qing_htool_main_scope_value = 1
    }

    is_valid = {
        scope:gui_holding = {
            has_ongoing_construction = no
            NOT = { has_holding_type = herder_holding }
            trigger_if = {
                limit = { qing_htool_holding_change_cooldown_value > 0 }
                NOT = { has_province_modifier = qing_htool_holding_change_cooldown_modifier }
            }
        }
        trigger_if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            trigger_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root.gold >= qing_htool_holding_change_cost_value
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    root.treasury >= qing_htool_holding_change_cost_value
                }
                trigger_else = {
                    root.gold >= qing_htool_holding_change_cost_value
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_has_treasury_error"
                        OR = {
                            root.gold >= qing_htool_holding_change_cost_value
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                    }
                }
                trigger_else = {
                    custom_tooltip = {
                        text = "qing_htool_holding_change_pay_method_gold_first_no_treasury_error"
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                custom_tooltip = {
                    text = "qing_htool_holding_change_pay_method_treasury_first_error"
                    OR = {
                        AND = {
                            root = { has_treasury = yes }
                            root.treasury >= qing_htool_holding_change_cost_value
                        }
                        root.gold >= qing_htool_holding_change_cost_value
                    }
                }
            }
        }
    }

    effect = {
        scope:gui_holding = {
            set_holding_type = herder_holding
            if = {
                limit = {
                    qing_htool_holding_change_cooldown_value > 0
                }
                add_province_modifier = {
                    modifier = qing_htool_holding_change_cooldown_modifier
                    months = qing_htool_holding_change_cooldown_value
                }
            }
        }
        if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_low }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_low_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_medium }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_medium_modifier
                    years = 1
                }
            }
        }
        else_if = {
            limit = { has_game_rule = qing_htool_holding_change_influence_high }
            scope:gui_holding = {
                add_province_modifier = {
                    modifier = qing_htool_holding_change_influence_high_modifier
                    years = 1
                }
            }
        }
        if = {
            limit = { qing_htool_holding_change_cost_value > 0 }
            if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                if = {
                    limit = { root.gold >= qing_htool_holding_change_cost_value }
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
                else_if = {
                    limit = {
                        root = { has_treasury = yes }
                        root.treasury >= qing_htool_holding_change_cost_value
                    }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_holding_change_cost_value }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_holding_change_cost_value }
                }
            }
        }
        custom_tooltip = "qing_htool_holding_change_tip"
    }
}

# 拆除地产
qing_htool_holding_remove = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            exists = province_owner
        }
        has_game_rule = qing_htool_main_switch_holding_remove_on
        qing_htool_main_scope_value = 1
    }

    is_valid = {
        scope:gui_holding = {
            has_ongoing_construction = no
            NOT = { is_county_capital = yes }
        }
    }

    effect = {
        hidden_effect = {
            scope:gui_holding = {
                remove_holding = yes
                barony = {   
                    save_scope_as = target_barony
                }
            }
        }
        custom_tooltip = "qing_htool_holding_remove_tip"
    }
}