scripted_gui = {

	ukr_anarchist_legitimacy_decisions_scripted_gui = {
		context_type = decision_category
		window_name = "ukr_anarchist_legitimacy_decisions_gui_main"

		visible = {
            tag = UKR
            has_government = anarchy
            is_subject = no
            has_global_flag = ukr_turned_anarchist_free_state
            NOT = {
                has_global_flag = the_great_russian_collapse_is_here
            }
        }

        ai_enabled = {
            always = no #The AI will only use the click on state GUI
        }

        dynamic_lists = {
			ukr_anarchist_legitimacy_grid = {
				array = ukr.anarchist_states_current_view
				change_scope = yes
                entry_container = ukr_anarchist_legitimacy_entry
			}
        }

        effects = {
            first_option_checkbox_click = {
                if = {
                    limit = {
                        check_variable = {
                            var = ukr.view_anarchist_states_can_be_cored
                            value = 2
                            compare = equals
                        }
                    }
                    set_variable = { ukr.view_anarchist_states_can_be_cored = 1 }
                }
                if = {
                    limit = {
                        check_variable = {
                            var = ukr.view_anarchist_states_core
                            value = 2
                            compare = equals
                        }
                    }
                    set_variable = { ukr.view_anarchist_states_core = 1 }
                    ukr_update_state_cost = yes
                    ukr_update_anarchist_states = yes
                    ukr_update_current_view = yes
                }
                else = {
                    set_variable = { ukr.view_anarchist_states_core = 2 }
                    ukr_update_state_cost = yes
                    ukr_update_anarchist_states = yes
                    ukr_update_current_view = yes
                }
            }

            second_option_checkbox_click = {
                if = {
                    limit = {
                        check_variable = {
                            var = ukr.view_anarchist_states_can_be_cored
                            value = 2
                            compare = equals
                        }
                    }
                    set_variable = { ukr.view_anarchist_states_can_be_cored = 1 }
                }
                if = {
                    limit = {
                        check_variable = {
                            var = ukr.view_anarchist_states_converted
                            value = 2
                            compare = equals
                        }
                    }
                    set_variable = { ukr.view_anarchist_states_converted = 1 }
                    ukr_update_state_cost = yes
                    ukr_update_anarchist_states = yes
                    ukr_update_current_view = yes
                }
                else = {
                    set_variable = { ukr.view_anarchist_states_converted = 2 }
                    ukr_update_state_cost = yes
                    ukr_update_anarchist_states = yes
                    ukr_update_current_view = yes
                }
            }

            third_option_checkbox_click = {
                if = {
                    limit = {
                        check_variable = {
                            var = ukr.view_anarchist_states_core
                            value = 2
                            compare = equals
                        }
                    }
                    set_variable = { ukr.view_anarchist_states_core = 1 }
                }
                if = {
                    limit = {
                        check_variable = {
                            var = ukr.view_anarchist_states_converted
                            value = 2
                            compare = equals
                        }
                    }
                    set_variable = { ukr.view_anarchist_states_converted = 1 }
                }
                if = {
                    limit = {
                        check_variable = {
                            var = ukr.view_anarchist_states_can_be_cored
                            value = 2
                            compare = equals
                        }
                    }
                    set_variable = { ukr.view_anarchist_states_can_be_cored = 1 }
                    ukr_update_state_cost = yes
                    ukr_update_anarchist_states = yes
                    ukr_update_current_view = yes
                }
                else = {
                    set_variable = { ukr.view_anarchist_states_can_be_cored = 2 }
                    ukr_update_state_cost = yes
                    ukr_update_anarchist_states = yes
                    ukr_update_current_view = yes
                }
            }

            ukr_anarchist_legitimacy_state_goto_click = {
                ROOT = {
                    goto_state = PREV
                }
            }

            ukr_anarchist_legitimacy_state_boost_click = {
                set_temp_variable = { click_cost = ukr.anarchist_state_boost_cost@THIS }
                multiply_temp_variable = { click_cost = -1 }
                ROOT = {
                    add_political_power = var:click_cost
                }
                set_variable = { ukr.anarchist_support_boost@THIS = 30 }
                ukr_update_state_cost = yes
            }

            ukr_anarchist_legitimacy_state_core_click = {
                set_temp_variable = { click_cost = ukr.anarchist_state_core_cost@THIS }
                multiply_temp_variable = { click_cost = -1 }
                ROOT = {
                    add_political_power = var:click_cost
                }
                subtract_from_variable = { ukr.anarchist_coring_points = 1 }
                ROOT = {
                    add_state_core = PREV
                }
                if = {
                    limit = {
                        THIS = {
                            has_dynamic_modifier = {
                                modifier = ukr_anarchist_state_converted
                            }
                        }
                    }
                    THIS = {
                        remove_dynamic_modifier = {
                            modifier = ukr_anarchist_state_converted
                        }
                    }
                }
                if = {
                    limit = {
                        THIS = {
                            has_dynamic_modifier = {
                                modifier = ukr_anarchist_state_non_converted
                            }
                        }
                    }
                    THIS = {
                        remove_dynamic_modifier = {
                            modifier = ukr_anarchist_state_non_converted
                        }
                    }
                }
                ukr_update_state_cost = yes
                ukr_update_anarchist_states = yes
                ukr_update_current_view = yes
            }
        }

        triggers = {
            ukr_anarchist_legitimacy_state_boost_click_enabled = {
                THIS = {
                    is_controlled_by = UKR
                    NOT = {
                        is_core_of = UKR
                    }
                    check_variable = {
                        var = ukr.anarchist_support@THIS
                        value = 100
                        compare = less_than
                    }
                    check_variable = {
                        var = ukr.anarchist_support_boost@THIS
                        value = 0
                        compare = equals
                    }
                    check_variable = {
                        var = ukr.political_power
                        value = ukr.anarchist_state_boost_cost@THIS
                        compare = greater_than_or_equals
                    }
                }
            }

            ukr_anarchist_legitimacy_state_core_click_enabled = {
                THIS = {
                    is_controlled_by = UKR
                    NOT = {
                        is_core_of = UKR
                    }
                    check_variable = {
                        var = ukr.political_power
                        value = ukr.anarchist_state_core_cost@THIS
                        compare = greater_than_or_equals
                    }
                    any_neighbor_state = {
                        is_core_of = UKR
                    }
                    check_variable = {
                        var = ukr.anarchist_support@THIS
                        value = 50
                        compare = greater_than_or_equals
                    }
                    check_variable = {
                        var = ukr.anarchist_coring_points
                        value = 1
                        compare = greater_than_or_equals
                    }
                }
            }
        }

        properties = {
            first_option_checkbox = {
                frame = var:ukr.view_anarchist_states_core
            }

            second_option_checkbox = {
                frame = var:ukr.view_anarchist_states_converted
            }

            third_option_checkbox = {
                frame = var:ukr.view_anarchist_states_can_be_cored
            }

            ukr_anarchist_legitimacy_state_status = {
                image = [THIS.GetCurrentAnarchistStateStatus]
            }

            ukr_anarchist_legitimacy_state_progress = {
                frame = ukr.anarchist_support@THIS
            }

            ukr_anarchist_legitimacy_progress = {
                frame = var:ukr.num_core_states
            }
        }
	}

    ukr_anarchist_legitimacy_state_overview = {
        context_type = selected_state_context
        window_name = "ukr_anarchist_legitimacy_state_overview"

        visible = {
            ROOT = {
                tag = UKR
                has_government = anarchy
                is_subject = no
                has_global_flag = ukr_turned_anarchist_free_state
                NOT = {
                    has_global_flag = the_great_russian_collapse_is_here
                }
                if = {
                    limit = {
                        is_ai = no
                    }
                    has_country_flag = ukr_view_on_map_anarchist_states
                }
            }
            THIS = {
                is_controlled_by = UKR
                is_in_array = { ukr.anarchist_states = THIS }
            }
        }

        ai_enabled = {
			tag = UKR
		}

        ai_test_interval = 24
        ai_test_variance = 0.25
        ai_test_scopes = test_self_controlled_states

        ai_check = {
            always = yes
        }

        ai_check_scope = {
            NOT = {
                is_core_of = ROOT
            }
            OR = {
                AND = {
                    check_variable = {
                        var = ukr.anarchist_support@THIS
                        value = 100
                        compare = less_than
                    }
                    check_variable = {
                        var = ukr.anarchist_support_boost@THIS
                        value = 0
                        compare = equals
                    }
                    check_variable = {
                        var = ukr.political_power
                        value = ukr.anarchist_state_boost_cost@THIS
                        compare = greater_than_or_equals
                    }
                }
                AND = {
                    check_variable = {
                        var = ukr.political_power
                        value = ukr.anarchist_state_core_cost@THIS
                        compare = greater_than_or_equals
                    }
                    any_neighbor_state = {
                        is_core_of = UKR
                    }
                    check_variable = {
                        var = ukr.anarchist_support@THIS
                        value = 50
                        compare = greater_than_or_equals
                    }
                    check_variable = {
                        var = ukr.anarchist_coring_points
                        value = 1
                        compare = greater_than_or_equals
                    }
                }
            }
        }

        ai_max_weight_taken_per_test = 50

        ai_weights = {
            ukr_anarchist_legitimacy_state_goto_click = {
                ai_will_do = {
                    base = 0
                }
            }

            ukr_anarchist_legitimacy_state_boost_click = {
                ai_will_do = {
                    base = 0
                    modifier = {
                        add = 1000
                        check_variable = {
                            var = ukr.anarchist_support_daily@THIS
                            value = 1
                            compare = less_than
                        }
                        OR = {
                            AND = {
                                UKR = {
                                    has_active_mission = UKR_anarchism_timeout
                                }
                                if = {
                                    limit = {
                                        UKR = {
                                            has_political_power > 200
                                        }
                                    }
                                    check_variable = {
                                        var = ukr.anarchist_support@THIS
                                        value = 75
                                        compare = less_than
                                    }
                                    check_variable = {
                                        var = ukr.anarchist_state_boost_cost@THIS
                                        value = 125
                                        compare = less_than
                                    }
                                }
                                else = {
                                    check_variable = {
                                        var = ukr.anarchist_support@THIS
                                        value = 50
                                        compare = less_than
                                    }
                                    check_variable = {
                                        var = ukr.anarchist_state_boost_cost@THIS
                                        value = 75
                                        compare = less_than
                                    }
                                }
                            }
                            AND = {
                                NOT = {
                                    UKR = {
                                        has_active_mission = UKR_anarchism_timeout
                                    }
                                }
                                check_variable = {
                                    var = ukr.anarchist_support@THIS
                                    value = 80
                                    compare = less_than
                                }
                                check_variable = {
                                    var = ukr.anarchist_state_boost_cost@THIS
                                    value = 200
                                    compare = less_than
                                }
                            }
                        }
                    }
                }
            }

            ukr_anarchist_legitimacy_state_core_click = {
                ai_will_do = {
                    base = 0
                    modifier = {
                        add = 1000
                        OR = {
                            AND = {
                                UKR = {
                                    has_active_mission = UKR_anarchism_timeout
                                }
                                if = {
                                    limit = {
                                        NOT = {
                                            check_variable = {
                                                var = ukr.num_core_states
                                                value = 19
                                                compare = equals
                                            }
                                        }
                                    }
                                    OR = {
                                        check_variable = {
                                            var = ukr.anarchist_state_core_cost@THIS
                                            value = 300
                                            compare = less_than
                                        }
                                        check_variable = {
                                            var = ukr.anarchist_support@THIS
                                            value = 85
                                            compare = greater_than_or_equals
                                        }
                                        UKR = {
                                            has_manpower < 50000
                                        }
                                    }
                                }
                            }
                            AND = {
                                NOT = {
                                    UKR = {
                                        has_active_mission = UKR_anarchism_timeout
                                    }
                                }
                                OR = {
                                    check_variable = {
                                        var = ukr.anarchist_state_core_cost@THIS
                                        value = 150
                                        compare = less_than
                                    }
                                    check_variable = {
                                        var = ukr.anarchist_support@THIS
                                        value = 100
                                        compare = greater_than_or_equals
                                    }
                                    UKR = {
                                        has_manpower < 50000
                                    }
                                }
                            }
                        } 
                    }
                }
            }
        }

        effects = {
            ukr_anarchist_legitimacy_state_goto_click = {
                ROOT = {
                    goto_state = PREV
                }
            }

            ukr_anarchist_legitimacy_state_boost_click = {
                set_temp_variable = { click_cost = ukr.anarchist_state_boost_cost@THIS }
                multiply_temp_variable = { click_cost = -1 }
                ROOT = {
                    add_political_power = var:click_cost
                }
                set_variable = { ukr.anarchist_support_boost@THIS = 30 }
                ukr_update_state_cost = yes
            }

            ukr_anarchist_legitimacy_state_core_click = {
                set_temp_variable = { click_cost = ukr.anarchist_state_core_cost@THIS }
                multiply_temp_variable = { click_cost = -1 }
                ROOT = {
                    add_political_power = var:click_cost
                }
                subtract_from_variable = { ukr.anarchist_coring_points = 1 }
                ROOT = {
                    add_state_core = PREV
                }
                if = {
                    limit = {
                        THIS = {
                            has_dynamic_modifier = {
                                modifier = ukr_anarchist_state_converted
                            }
                        }
                    }
                    THIS = {
                        remove_dynamic_modifier = {
                            modifier = ukr_anarchist_state_converted
                        }
                    }
                }
                if = {
                    limit = {
                        THIS = {
                            has_dynamic_modifier = {
                                modifier = ukr_anarchist_state_non_converted
                            }
                        }
                    }
                    THIS = {
                        remove_dynamic_modifier = {
                            modifier = ukr_anarchist_state_non_converted
                        }
                    }
                }
                ukr_update_state_cost = yes
                ukr_update_anarchist_states = yes
                ukr_update_current_view = yes
            }
        }

        triggers = {
            ukr_anarchist_legitimacy_state_boost_click_enabled = {
                THIS = {
                    is_controlled_by = UKR
                    NOT = {
                        is_core_of = UKR
                    }
                    check_variable = {
                        var = ukr.anarchist_support@THIS
                        value = 100
                        compare = less_than
                    }
                    check_variable = {
                        var = ukr.anarchist_support_boost@THIS
                        value = 0
                        compare = equals
                    }
                    check_variable = {
                        var = ukr.political_power
                        value = ukr.anarchist_state_boost_cost@THIS
                        compare = greater_than_or_equals
                    }
                }
            }

            ukr_anarchist_legitimacy_state_core_click_enabled = {
                THIS = {
                    is_controlled_by = UKR
                    NOT = {
                        is_core_of = UKR
                    }
                    check_variable = {
                        var = ukr.political_power
                        value = ukr.anarchist_state_core_cost@THIS
                        compare = greater_than_or_equals
                    }
                    any_neighbor_state = {
                        is_core_of = UKR
                    }
                    check_variable = {
                        var = ukr.anarchist_support@THIS
                        value = 50
                        compare = greater_than_or_equals
                    }
                    check_variable = {
                        var = ukr.anarchist_coring_points
                        value = 1
                        compare = greater_than_or_equals
                    }
                }
            }
        }

        properties = {
            ukr_anarchist_legitimacy_state_status = {
                image = [THIS.GetCurrentAnarchistStateStatus]
            }

            ukr_anarchist_legitimacy_state_progress = {
                frame = ukr.anarchist_support@THIS
            }
        }
    }
}