﻿#Slot
# 新增建筑槽
qing_htool_building_slot_add = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            exists = province_owner
            NOR = {
                has_holding_type = nomad_holding #游牧聚落地产
                has_holding_type = herder_holding #牧民定居点
            }
        }
        has_game_rule = qing_htool_main_switch_building_slot_add_on
        qing_htool_main_scope_value = 1
    }

        is_valid = {
        scope:gui_holding = {
            has_ongoing_construction = no
            # 冷却检查
            trigger_if = {
                limit = { qing_htool_building_slot_add_cooldown_value > 0 }
                NOT = { has_province_modifier = qing_htool_building_slot_add_cooldown_modifier }
            }
            # 初始化变量
            if = {
                limit = {
                    NOT = { has_variable = qing_htool_building_slot_information_count }
                }
                set_variable = {
                    name = qing_htool_building_slot_information_count
                    value = 0
                }
            }
            # 上限检查
            trigger_if = {
                limit = {
                    qing_htool_building_slot_add_max_value > 0
                }
                custom_tooltip = {
                    text = qing_htool_building_slot_add_max_error
                    NOT = {
                        var:qing_htool_building_slot_information_count >= qing_htool_building_slot_add_max_value
                    }
                }
            }
        }
        # 金钱检查
        trigger_if = {
            limit = { qing_htool_building_slot_cost_total > 0 }
            trigger_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root.gold >= qing_htool_building_slot_cost_total
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                trigger_if = {
                    limit = { root = { has_treasury = yes } }
                    root.treasury >= qing_htool_building_slot_cost_total
                }
                trigger_else = {
                    root.gold >= qing_htool_building_slot_cost_total
                }
            }
            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_building_slot_add_pay_method_gold_first_has_treasury_error"
                        OR = {
                            root.gold >= qing_htool_building_slot_cost_total
                            root.treasury >= qing_htool_building_slot_cost_total
                        }
                    }
                }
                trigger_else = {
                    custom_tooltip = {
                        text = "qing_htool_building_slot_add_pay_method_gold_first_no_treasury_error"
                        root.gold >= qing_htool_building_slot_cost_total
                    }
                }
            }
            trigger_else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                custom_tooltip = {
                    text = "qing_htool_building_slot_add_pay_method_treasury_first_error"
                    OR = {
                        AND = {
                            root = { has_treasury = yes }
                            root.treasury >= qing_htool_building_slot_cost_total
                        }
                        root.gold >= qing_htool_building_slot_cost_total
                    }
                }
            }
        }
    }

    effect = {
        scope:gui_holding = {
            # 初始化变量
            if = {
                limit = {
                    NOT = { has_variable = qing_htool_building_slot_information_count }
                }
                set_variable = {
                    name = qing_htool_building_slot_information_count
                    value = 0
                }
            }
            # 冷却修正
            if = {
                limit = {
                    qing_htool_building_slot_add_cooldown_value > 0
                }
                add_province_modifier = {
                    modifier = qing_htool_building_slot_add_cooldown_modifier
                    months = qing_htool_building_slot_add_cooldown_value
                }
            }
            # 新增建筑槽修正
            add_province_modifier = qing_htool_building_slot_add_modifier
            change_variable = {
                name = qing_htool_building_slot_information_count
                add = 1
            }
        }
                # 花费
        if = {
            limit = { qing_htool_building_slot_cost_total > 0 }
            if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold }
                root = { remove_long_term_gold = qing_htool_building_slot_cost_total }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_building_slot_cost_total }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_building_slot_cost_total }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_gold_first }
                if = {
                    limit = { root.gold >= qing_htool_building_slot_cost_total }
                    root = { remove_long_term_gold = qing_htool_building_slot_cost_total }
                }
                else_if = {
                    limit = {
                        root = { has_treasury = yes }
                        root.treasury >= qing_htool_building_slot_cost_total
                    }
                    root = { remove_treasury = qing_htool_building_slot_cost_total }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_building_slot_cost_total }
                }
            }
            else_if = {
                limit = { has_game_rule = qing_htool_main_pay_method_treasury_first }
                if = {
                    limit = { root = { has_treasury = yes } }
                    root = { remove_treasury = qing_htool_building_slot_cost_total }
                }
                else = {
                    root = { remove_long_term_gold = qing_htool_building_slot_cost_total }
                }
            }
        }
    }
}

qing_htool_building_build_marge = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            exists = province_owner
            NOR = {
                has_holding_type = nomad_holding
                has_holding_type = herder_holding
            }
        }
        has_game_rule = qing_htool_main_switch_building_build_marge_on
        qing_htool_main_scope_value = 1
    }

    is_valid = {
        scope:gui_holding = {
            custom_tooltip = {
                text = "qing_htool_building_build_marge_error"
                OR = {
                    AND = {
                        has_free_building_slot = yes
                        qing_htool_building_has_constructible_trigger = yes
                    }
                    AND = {
                        has_game_rule = qing_htool_building_build_construct_max_switch_on
                        qing_htool_building_has_constructible_trigger = yes
                    }
                    qing_htool_building_has_upgradable_trigger = yes
                }
            }
        }
    }

    effect = {
        hidden_effect = {
            scope:gui_holding = {
                # 一键建造
                if = { limit = { has_game_rule = qing_htool_building_build_construct_max_switch_on }
                    qing_htool_building_build_construct_all_to_max = yes
                }
                else = {
                    qing_htool_building_build_construct_all = yes
                }
                # 一键升级
                if = { limit = { has_game_rule = qing_htool_building_build_upgrade_max_switch_on }
                    qing_htool_building_build_upgrade_all_to_max = yes
                }
                else = {
                    qing_htool_building_build_upgrade_all = yes
                }
            }
        }
        custom_tooltip = "qing_htool_building_build_marge_tip"
    }
}

qing_htool_building_build_construct = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            exists = province_owner
            NOR = {
                has_holding_type = nomad_holding #游牧聚落地产
                has_holding_type = herder_holding #牧民定居点
            }
        }
        has_game_rule = qing_htool_main_switch_building_build_construct_on
        qing_htool_main_scope_value = 1
    }

    is_valid = {
        scope:gui_holding = {
            custom_tooltip = {
                text = "qing_htool_building_build_construct_error"
                OR = {
                    has_free_building_slot = yes
                    has_game_rule = qing_htool_building_build_construct_max_switch_on
                }
                qing_htool_building_has_constructible_trigger = yes
            }
        }
    }

    effect = {
        hidden_effect = {
            scope:gui_holding = {
                if = {
                    limit = { has_game_rule = qing_htool_building_build_construct_max_switch_on }
                    qing_htool_building_build_construct_all_to_max = yes
                }
                else = {
                    qing_htool_building_build_construct_all = yes
                }
            }
            if = {
                limit = { scope:gui_holding.var:qing_htool_building_build_cost_total > 0 qing_htool_building_build_construct_cost_switch_trigger = yes }
                if = { limit = { qing_htool_building_pay_gold_trigger = yes }
                    root = { remove_long_term_gold = scope:gui_holding.var:qing_htool_building_build_cost_total }
                }
                else_if = { limit = { qing_htool_building_pay_treasury_trigger = yes }
                    if = { limit = { root = { has_treasury = yes } }
                        root = { remove_treasury = scope:gui_holding.var:qing_htool_building_build_cost_total }
                    }
                    else = { root = { remove_long_term_gold = scope:gui_holding.var:qing_htool_building_build_cost_total } }
                }
                else_if = { limit = { qing_htool_building_pay_gold_first_trigger = yes }
                    if = { limit = { root.gold >= scope:gui_holding.var:qing_htool_building_build_cost_total }
                        root = { remove_long_term_gold = scope:gui_holding.var:qing_htool_building_build_cost_total }
                    }
                    else_if = { limit = { root = { has_treasury = yes } root.treasury >= scope:gui_holding.var:qing_htool_building_build_cost_total }
                        root = { remove_treasury = scope:gui_holding.var:qing_htool_building_build_cost_total }
                    }
                    else = { root = { remove_long_term_gold = scope:gui_holding.var:qing_htool_building_build_cost_total } }
                }
                else_if = { limit = { qing_htool_building_pay_treasury_first_trigger = yes }
                    if = { limit = { root = { has_treasury = yes } root.treasury >= scope:gui_holding.var:qing_htool_building_build_cost_total }
                        root = { remove_treasury = scope:gui_holding.var:qing_htool_building_build_cost_total }
                    }
                    else = { root = { remove_long_term_gold = scope:gui_holding.var:qing_htool_building_build_cost_total } }
                }
            }
        }
        custom_tooltip = "qing_htool_building_build_construct_tip"
    }
}

qing_htool_building_build_upgrade = {
    scope = character
    saved_scopes = { gui_holding }

    is_shown = {
        scope:gui_holding = {
            exists = province_owner
            NOR = {
                has_holding_type = nomad_holding #游牧聚落地产
                has_holding_type = herder_holding #牧民定居点
            }
        }
        has_game_rule = qing_htool_main_switch_building_build_upgrade_on
        qing_htool_main_scope_value = 1
    }

    is_valid = {
        scope:gui_holding = {
            custom_tooltip = {
                text = "qing_htool_building_build_upgrade_error"
                qing_htool_building_has_upgradable_trigger = yes
            }
        }
    }

    effect = {
        hidden_effect = {
            scope:gui_holding = {
                if = {
                    limit = { has_game_rule = qing_htool_building_build_upgrade_max_switch_on }
                    qing_htool_building_build_upgrade_all_to_max = yes
                }
                else = {
                    qing_htool_building_build_upgrade_all = yes
                }
            }
            if = {
                limit = { scope:gui_holding.var:qing_htool_building_build_cost_total > 0 qing_htool_building_build_upgrade_cost_switch_trigger = yes }
                if = { limit = { qing_htool_building_pay_gold_trigger = yes }
                    root = { remove_long_term_gold = scope:gui_holding.var:qing_htool_building_build_cost_total }
                }
                else_if = { limit = { qing_htool_building_pay_treasury_trigger = yes }
                    if = { limit = { root = { has_treasury = yes } }
                        root = { remove_treasury = scope:gui_holding.var:qing_htool_building_build_cost_total }
                    }
                    else = { root = { remove_long_term_gold = scope:gui_holding.var:qing_htool_building_build_cost_total } }
                }
                else_if = { limit = { qing_htool_building_pay_gold_first_trigger = yes }
                    if = { limit = { root.gold >= scope:gui_holding.var:qing_htool_building_build_cost_total }
                        root = { remove_long_term_gold = scope:gui_holding.var:qing_htool_building_build_cost_total }
                    }
                    else_if = { limit = { root = { has_treasury = yes } root.treasury >= scope:gui_holding.var:qing_htool_building_build_cost_total }
                        root = { remove_treasury = scope:gui_holding.var:qing_htool_building_build_cost_total }
                    }
                    else = { root = { remove_long_term_gold = scope:gui_holding.var:qing_htool_building_build_cost_total } }
                }
                else_if = { limit = { qing_htool_building_pay_treasury_first_trigger = yes }
                    if = { limit = { root = { has_treasury = yes } root.treasury >= scope:gui_holding.var:qing_htool_building_build_cost_total }
                        root = { remove_treasury = scope:gui_holding.var:qing_htool_building_build_cost_total }
                    }
                    else = { root = { remove_long_term_gold = scope:gui_holding.var:qing_htool_building_build_cost_total } }
                }
            }
        }
        custom_tooltip = "qing_htool_building_build_upgrade_tip"
    }
}