﻿E_kCAFG_natural_faith_evolution_yearly = {
    if = {
        limit = { NOT = { has_game_rule = kCAFG_natural_faith_evolution_none } }

        #debug_log = "Kei CAFG - [GetCurrentDate.GetStringLong] - Natural Faith Evolution"
        every_province_w_barony = {
            change_variable = {
                name = kCAFG_natural_faith_evolution_progress
                add = {
                    fixed_range = {
                        # about every 5-12 years
                        min = 7
                        max = 19
                    }
                    # Increased by development
                    add = {
                        value = county.development_level
                        divide = max_development_level
                        subtract = 0.2
                        divide = 0.8
                        multiply = 15
                        min = 0
                        max = 10
                    }
                }
            }

            if = {
                limit = { var:kCAFG_natural_faith_evolution_progress > 100 }
                change_variable = {
                    name = kCAFG_natural_faith_evolution_progress
                    subtract = 100
                }

                save_scope_as = province
                trigger_event = {
                    id = kei_cafg_natural_faith_evolution_events.0001
                    days = { 0 365 }
                }
            }
        }
        #debug_log = "Kei CAFG - [GetCurrentDate.GetStringLong] - NFE Done"
    }
}

E_kCAFG_province_natural_faith_evolution = {
    #debug_log = "Kei CAFG - [GetCurrentDate.GetStringLong] - Natural Faith Evolution Triggered in Province [THIS.Province.GetNameNoTooltip]"

    # Random conversion within the province
    if = {
        limit = {
            T_kCAFG_province_faiths_count_at_least = { VALUE = 2 }
        }

        random = {
            chance = {
                value = 15
                add = {
                    value = var:kCAFG_faith_tension
                    multiply = 35
                }
                min = 1
            }

            # Decreased faith
            # Avoid making minorities disappear
            random_in_list = {
                variable = kCAFG_province_faith_structs
                limit = { var:factor > V_kCAFG_smaller_minority_factor }
                var:faith = { save_temporary_scope_as = kCAFG_converted_faith }
            }

            if = {
                limit = { exists = scope:kCAFG_converted_faith }

                # Increased faith
                random_in_list = {
                    variable = kCAFG_province_faiths
                    limit = { scope:kCAFG_converted_faith != this }
                    weight = {
                        base = 125
                        modifier = {
                            add = {
                                value = "faith_hostility_level(scope:kCAFG_converted_faith)" # Between 0 and 3
                                multiply = -33
                            }
                        }
                    }
                    save_temporary_scope_as = kCAFG_converting_faith
                }

                if = {
                    limit = { exists = scope:kCAFG_converting_faith }

                    #debug_log = "Kei CAFG - [GetCurrentDate.GetStringLong] - Random Faith Conversion in [THIS.Province.GetNameNoTooltip]"
                    E_kCAFG_convert_province_faith_factor = {
                        SOURCE = scope:kCAFG_converted_faith
                        TARGET = scope:kCAFG_converting_faith
                        FACTOR = V_kCAFG_smallest_minority_factor
                    }

                    county = {
                        E_kCAFG_update_county_faiths_list = yes
                        E_kCAFG_update_county_faith = { REASON = flag:kCAFG_natural_evolution }
                    }
                }
            }
        }
    }

    # If majority or very high fervor, spread
    if = {
        limit = {
            any_in_list = {
                variable = kCAFG_province_faith_structs
                OR = {
                    var:factor >= V_kCAFG_medium_minority_factor
                    AND = {
                        var:faith.V_kCAFG_fervor_factor >= 0.9
                        var:factor > V_kCAFG_very_small_minority_factor
                    }
                }
            }
        }
        random_in_list = {
            variable = kCAFG_province_faith_structs
            limit = {
                OR = {
                    var:factor >= V_kCAFG_medium_minority_factor
                    AND = {
                        var:faith.V_kCAFG_fervor_factor >= 0.9
                        var:factor > V_kCAFG_very_small_minority_factor
                    }
                }
            }

            weight = {
                base = 500
                modifier = {
                    add = {
                        value = var:factor
                        multiply = 500
                    }
                }
                
                modifier = {
                    add = {
                        value = var:faith.V_kCAFG_fervor_factor
                        multiply = 600
                    }
                }
            }

            save_temporary_scope_as = converting_struct
            var:faith = {
                save_temporary_scope_as = kCAFG_faith
                save_temporary_scope_as = kCAFG_converting_faith
                save_temporary_scope_as = kCAFG_target_faith # used by V_kCAFG_faith_tension_target_baseline
            }
        }

        set_local_variable = {
            name = kCAFG_natural_spread_converted_factor
            value = 0
        }
        # Base chance to convert
        random = {
            chance = {
                value = scope:kCAFG_converting_faith.V_kCAFG_fervor_factor
                subtract = 0.15
                divide = 0.85
                multiply = 120
                max = 100

                multiply = {
                    value = 0.4
                    add = {
                        value = scope:converting_struct.var:factor
                        subtract = 0.5
                        divide = 0.5
                        multiply = 0.6
                    }
                }

                multiply = {
                    value = 1.5
                    every_in_list = {
                        variable = kCAFG_province_faith_structs
                        limit = { this.var:faith != scope:kCAFG_converting_faith }

                        subtract = {
                            var:faith = { value = V_kCAFG_faith_tension_target_baseline }
                            multiply = var:factor
                        }
                    }
                    add = 1
                    divide = 2
                }

                if = {
                    limit = {
                        scope:province.barony = {
                            is_holy_site_of = scope:kCAFG_converting_faith
                        }
                    }
                    add = 20
                }
            }
            #debug_log = "Convert Common"
            change_local_variable = {
                name = kCAFG_natural_spread_converted_factor
                add = { 0.01 0.09 }
            }
        }
        # Chance to convert more
        random = {
            chance = {
                value = scope:kCAFG_converting_faith.V_kCAFG_fervor_factor
                subtract = 0.7
                divide = 0.3
                multiply = 32

                multiply = {
                    value = 0.6
                    add = {
                        value = scope:converting_struct.var:factor
                        subtract = 0.7
                        divide = 0.3
                        multiply = 0.4
                    }
                }

                multiply = {
                    value = 1.5
                    every_in_list = {
                        variable = kCAFG_province_faith_structs
                        limit = { this.var:faith != scope:kCAFG_converting_faith }

                        subtract = {
                            var:faith = { value = V_kCAFG_faith_tension_target_baseline }
                            multiply = var:factor
                        }
                    }
                    add = 1
                    divide = 2
                }

                if = {
                    limit = {
                        scope:province.barony = {
                            is_holy_site_of = scope:kCAFG_converting_faith
                        }
                    }
                    add = 10
                }
            }
            #debug_log = "Convert Rare"
            change_local_variable = {
                name = kCAFG_natural_spread_converted_factor
                add = { 0.03 0.06 }
            }
        }

        if = {
            limit = {
                local_var:kCAFG_natural_spread_converted_factor > 0
            }
            
            if = {
                limit = {
                    NOT = {
                        any_in_list = {
                            variable = kCAFG_province_faith_structs
                            var:faith = scope:kCAFG_converting_faith
                            var:factor >= V_kCAFG_inverted_smaller_minority_factor
                        }
                    }
                    var:kCAFG_faith_tension < V_kCAFG_province_natural_faith_conversion_max_tension
                }
                add_to_temporary_list = province_selection
            }

            every_neighboring_province = {
                limit = {
                    OR = {
                        AND = {
                            is_sea_province = yes # Chance to spread a bit further
                            local_var:kCAFG_natural_spread_converted_factor > 0.03
                        }
                        AND = {
                            exists = county
                            NOT = {
                                any_in_list = {
                                    variable = kCAFG_province_faith_structs
                                    var:faith = scope:kCAFG_converting_faith
                                    var:factor >= V_kCAFG_inverted_smaller_minority_factor
                                }
                            }
                            var:kCAFG_faith_tension < V_kCAFG_province_natural_faith_conversion_max_tension
                        }
                    }
                }

                if = {
                    limit = { is_sea_province = yes }
                    every_neighboring_province = {
                        limit = {
                            exists = county
                            NOT = {
                                any_in_list = {
                                    variable = kCAFG_province_faith_structs
                                    var:faith = scope:kCAFG_converting_faith
                                    var:factor >= V_kCAFG_inverted_smaller_minority_factor
                                }
                            }
                            var:kCAFG_faith_tension < V_kCAFG_province_natural_faith_conversion_max_tension
                        }
                        add_to_temporary_list = province_selection
                    }
                }
                else = {
                    add_to_temporary_list = province_selection
                }
            }

            if = {
                limit = { is_in_list = province_selection }
                remove_from_list = province_selection
            }

            if = {
                limit = { list_size:province_selection > 0 }

                #debug_log = "-- Selection --"
                every_in_list = {
                    list = province_selection
                    set_variable = {
                        name = kCAFG_tmp_selection_weight # Store weight to avoid both evaluations in the limit AND the randomized selection
                        value = V_kCAFG_province_natural_faith_conversion_weight
                    }
                    #debug_log = "[THIS.Province.GetNameNoTooltip] - Weight: [THIS.Var('kCAFG_tmp_selection_weight').GetValue]"
                }
                #debug_log = "-- --------- --"

                random_in_list = {
                    list = province_selection
                    limit = {
                        var:kCAFG_tmp_selection_weight > 0
                    }
                    weight = {
                        base = 0
                        modifier = { add = var:kCAFG_tmp_selection_weight }
                    }

                    #debug_log = "Province [THIS.Province.GetNameNoTooltip] with weight [THIS.Var('kCAFG_tmp_selection_weight').GetValue] selected."
                    # Conversion will happen
                    if = {
                        limit = { var:kCAFG_tmp_selection_weight > V_kCAFG_province_natural_faith_conversion_bad_weight_threshold }
                        save_temporary_scope_as = kCAFG_natural_faith_majority_converted_province
                    }
                    # Conversion is unappealing, randomized chance
                    else = {
                        random = {
                            chance = {
                                value = V_kCAFG_province_natural_faith_conversion_bad_weight_max_chance
                                multiply = {
                                    value = var:kCAFG_tmp_selection_weight
                                    divide = V_kCAFG_province_natural_faith_conversion_bad_weight_threshold
                                }
                            }
                            save_temporary_scope_as = kCAFG_natural_faith_majority_converted_province
                        }
                    }
                }

                every_in_list = {
                    list = province_selection
                    remove_variable = kCAFG_tmp_selection_weight
                }

                if = {
                    limit = {
                        exists = scope:kCAFG_natural_faith_majority_converted_province
                    }

                    scope:kCAFG_natural_faith_majority_converted_province = {
                        ## LOG
                        # debug_log = "Kei CAFG - [GetCurrentDate.GetStringLong] - Natural Faith Spread Conversion Happened in Province [THIS.Province.GetNameNoTooltip] from [province.GetNameNoTooltip]"
                        # scope:kCAFG_converting_faith = {
                        #     set_variable = { name = converted_factor value = local_var:kCAFG_natural_spread_converted_factor }
                        #     debug_log = "Faith Converts: +[THIS.Var('converted_factor').GetValue] [THIS.Faith.GetNameNoTooltip]"
                        #     remove_variable = converted_factor
                        # }
                        ###

                        E_kCAFG_province_weighted_faith_conversion = {
                            FACTOR = local_var:kCAFG_natural_spread_converted_factor
                            FAITH = scope:kCAFG_converting_faith
                        }

                        county = {
                            E_kCAFG_update_county_faiths_list = yes
                            E_kCAFG_update_county_faith = { REASON = flag:kCAFG_natural_evolution }
                        }
                    }
                }
                else = {
                    #debug_log = "Failure!"
                }
            }
        }
    }
}