scripted_gui = {
  gd_open_window_button = {
    parent_window_name  = countryofficercorpview
    window_name = "gd_open_window_button"
    context_type = player_context

    visible = {
      always = yes
    }

    effects = {
     gd_open_window_button_click = {
       if = {
         limit = { has_country_flag = open_general_generator_window }
         clr_country_flag = open_general_generator_window
       }
       else = {
         set_country_flag = open_general_generator_window
       }
      }
    }

    triggers = {
    }

    dynamic_lists = {
    
	  }

    properties = {
     
    }

    ai_enabled = {
		always = no
    }

    ai_test_interval = 290  # The higher, the long it takes the ai to do actions
	  ai_test_variance = 0.5

    ai_check = {
    }

    ai_check_scope = {
    }

    ai_weights = {
    }
  }
	general_generator_gui = {
		window_name = "general_generator_window"
		context_type = player_context

		visible = {
			has_country_flag = open_general_generator_window
		}

		effects = {

			#############################
			# TRAIT TOGGLE BUTTONS
			# Click to select/deselect a trait (toggles flag)
			#############################

			army_leader_trait_1_click = {
				if = {
					limit = { has_country_flag = gd_trait_brilliant_strategist }
					clr_country_flag = gd_trait_brilliant_strategist
				}
				else = {
					set_country_flag = gd_trait_brilliant_strategist
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_2_click = {
				if = {
					limit = { has_country_flag = gd_trait_inflexible_strategist }
					clr_country_flag = gd_trait_inflexible_strategist
				}
				else = {
					set_country_flag = gd_trait_inflexible_strategist
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_3_click = {
				if = {
					limit = { has_country_flag = gd_trait_old_guard }
					clr_country_flag = gd_trait_old_guard
				}
				else = {
					set_country_flag = gd_trait_old_guard
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_4_click = {
				if = {
					limit = { has_country_flag = gd_trait_politically_connected }
					clr_country_flag = gd_trait_politically_connected
				}
				else = {
					set_country_flag = gd_trait_politically_connected
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_5_click = {
				if = {
					limit = { has_country_flag = gd_trait_career_officer }
					clr_country_flag = gd_trait_career_officer
				}
				else = {
					set_country_flag = gd_trait_career_officer
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_6_click = {
				if = {
					limit = { has_country_flag = gd_trait_cautious }
					clr_country_flag = gd_trait_cautious
				}
				else = {
					set_country_flag = gd_trait_cautious
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_7_click = {
				if = {
					limit = { has_country_flag = gd_trait_reckless }
					clr_country_flag = gd_trait_reckless
				}
				else = {
					set_country_flag = gd_trait_reckless
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_8_click = {
				if = {
					limit = { has_country_flag = gd_trait_media_personality }
					clr_country_flag = gd_trait_media_personality
				}
				else = {
					set_country_flag = gd_trait_media_personality
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_9_click = {
				if = {
					limit = { has_country_flag = gd_trait_brave }
					clr_country_flag = gd_trait_brave
				}
				else = {
					set_country_flag = gd_trait_brave
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_10_click = {
				if = {
					limit = { has_country_flag = gd_trait_harsh_leader }
					clr_country_flag = gd_trait_harsh_leader
				}
				else = {
					set_country_flag = gd_trait_harsh_leader
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_11_click = {
				if = {
					limit = { has_country_flag = gd_trait_infantry_officer }
					clr_country_flag = gd_trait_infantry_officer
				}
				else = {
					set_country_flag = gd_trait_infantry_officer
				}
				gd_recalculate_cost = yes
			}
			army_leader_trait_12_click = {
				if = {
					limit = { has_country_flag = gd_trait_armor_officer }
					clr_country_flag = gd_trait_armor_officer
				}
				else = {
					set_country_flag = gd_trait_armor_officer
				}
				gd_recalculate_cost = yes
			}

			#############################
			# SKILL PLUS BUTTONS
			#############################

			army_leader_skill_1_plus_button_click = {
				add_to_variable = { gd_attack_skill = 1 }
				gd_recalculate_cost = yes
			}
			army_leader_skill_2_plus_button_click = {
				add_to_variable = { gd_defense_skill = 1 }
				gd_recalculate_cost = yes
			}
			army_leader_skill_3_plus_button_click = {
				add_to_variable = { gd_planning_skill = 1 }
				gd_recalculate_cost = yes
			}
			army_leader_skill_4_plus_button_click = {
				add_to_variable = { gd_logistics_skill = 1 }
				gd_recalculate_cost = yes
			}

			#############################
			# SKILL MINUS BUTTONS
			#############################

			army_leader_skill_1_minus_button_click = {
				subtract_from_variable = { gd_attack_skill = 1 }
				gd_recalculate_cost = yes
			}
			army_leader_skill_2_minus_button_click = {
				subtract_from_variable = { gd_defense_skill = 1 }
				gd_recalculate_cost = yes
			}
			army_leader_skill_3_minus_button_click = {
				subtract_from_variable = { gd_planning_skill = 1 }
				gd_recalculate_cost = yes
			}
			army_leader_skill_4_minus_button_click = {
				subtract_from_variable = { gd_logistics_skill = 1 }
				gd_recalculate_cost = yes
			}

			#############################
			# PORTRAIT NAVIGATION
			# Cycles through 5 portraits (index 0-4)
			#############################

			army_leader_left_selection_button_click = {
				if = {
					limit = { check_variable = { gd_portrait_index > 0 } }
					subtract_from_variable = { gd_portrait_index = 1 }
				}
				else = {
					set_variable = { gd_portrait_index = 4 }
				}
			}
			army_leader_right_selection_button_click = {
				if = {
					limit = { check_variable = { gd_portrait_index < 4 } }
					add_to_variable = { gd_portrait_index = 1 }
				}
				else = {
					set_variable = { gd_portrait_index = 0 }
				}
			}

			#############################
			# NAME RANDOMIZE BUTTONS
			# Randomly picks from 30 first/last name options
			#############################

			randomize_character_name_left_button_click = {
				random_list = {
					1 = { set_variable = { gd_first_name_index = 0 } }
					1 = { set_variable = { gd_first_name_index = 1 } }
					1 = { set_variable = { gd_first_name_index = 2 } }
					1 = { set_variable = { gd_first_name_index = 3 } }
					1 = { set_variable = { gd_first_name_index = 4 } }
					1 = { set_variable = { gd_first_name_index = 5 } }
					1 = { set_variable = { gd_first_name_index = 6 } }
					1 = { set_variable = { gd_first_name_index = 7 } }
					1 = { set_variable = { gd_first_name_index = 8 } }
					1 = { set_variable = { gd_first_name_index = 9 } }
					1 = { set_variable = { gd_first_name_index = 10 } }
					1 = { set_variable = { gd_first_name_index = 11 } }
					1 = { set_variable = { gd_first_name_index = 12 } }
					1 = { set_variable = { gd_first_name_index = 13 } }
					1 = { set_variable = { gd_first_name_index = 14 } }
					1 = { set_variable = { gd_first_name_index = 15 } }
					1 = { set_variable = { gd_first_name_index = 16 } }
					1 = { set_variable = { gd_first_name_index = 17 } }
					1 = { set_variable = { gd_first_name_index = 18 } }
					1 = { set_variable = { gd_first_name_index = 19 } }
					1 = { set_variable = { gd_first_name_index = 20 } }
					1 = { set_variable = { gd_first_name_index = 21 } }
					1 = { set_variable = { gd_first_name_index = 22 } }
					1 = { set_variable = { gd_first_name_index = 23 } }
					1 = { set_variable = { gd_first_name_index = 24 } }
					1 = { set_variable = { gd_first_name_index = 25 } }
					1 = { set_variable = { gd_first_name_index = 26 } }
					1 = { set_variable = { gd_first_name_index = 27 } }
					1 = { set_variable = { gd_first_name_index = 28 } }
					1 = { set_variable = { gd_first_name_index = 29 } }
				}
			}
			randomize_character_name_right_button_click = {
				random_list = {
					1 = { set_variable = { gd_last_name_index = 0 } }
					1 = { set_variable = { gd_last_name_index = 1 } }
					1 = { set_variable = { gd_last_name_index = 2 } }
					1 = { set_variable = { gd_last_name_index = 3 } }
					1 = { set_variable = { gd_last_name_index = 4 } }
					1 = { set_variable = { gd_last_name_index = 5 } }
					1 = { set_variable = { gd_last_name_index = 6 } }
					1 = { set_variable = { gd_last_name_index = 7 } }
					1 = { set_variable = { gd_last_name_index = 8 } }
					1 = { set_variable = { gd_last_name_index = 9 } }
					1 = { set_variable = { gd_last_name_index = 10 } }
					1 = { set_variable = { gd_last_name_index = 11 } }
					1 = { set_variable = { gd_last_name_index = 12 } }
					1 = { set_variable = { gd_last_name_index = 13 } }
					1 = { set_variable = { gd_last_name_index = 14 } }
					1 = { set_variable = { gd_last_name_index = 15 } }
					1 = { set_variable = { gd_last_name_index = 16 } }
					1 = { set_variable = { gd_last_name_index = 17 } }
					1 = { set_variable = { gd_last_name_index = 18 } }
					1 = { set_variable = { gd_last_name_index = 19 } }
					1 = { set_variable = { gd_last_name_index = 20 } }
					1 = { set_variable = { gd_last_name_index = 21 } }
					1 = { set_variable = { gd_last_name_index = 22 } }
					1 = { set_variable = { gd_last_name_index = 23 } }
					1 = { set_variable = { gd_last_name_index = 24 } }
					1 = { set_variable = { gd_last_name_index = 25 } }
					1 = { set_variable = { gd_last_name_index = 26 } }
					1 = { set_variable = { gd_last_name_index = 27 } }
					1 = { set_variable = { gd_last_name_index = 28 } }
					1 = { set_variable = { gd_last_name_index = 29 } }
				}
			}

			#############################
			# GENERATE BUTTON
			# Creates the corps commander with selected portrait, skills, and traits
			#############################

			army_leader_generate_button_click = {

				# Mark all existing army leaders so we can find the new one after creation
				every_army_leader = {
					set_unit_leader_flag = gd_existing_leader
				}

				# Calculate name combo index: first_name * 30 + last_name
				set_variable = { gd_name_combo = gd_first_name_index }
				multiply_variable = { var = gd_name_combo value = 30 }
				add_to_variable = { gd_name_combo = gd_last_name_index }

				# Create corps commander with resolved name (no portrait - game auto-generates)
				gd_create_commander = yes

				# Find the newly created leader and add traits + skills
				every_army_leader = {
					limit = {
						NOT = { has_unit_leader_flag = gd_existing_leader }
					}

					# Add selected traits (inlined - scripted effects lose unit leader scope)
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_brilliant_strategist } }
						add_unit_leader_trait = brilliant_strategist
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_inflexible_strategist } }
						add_unit_leader_trait = inflexible_strategist
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_old_guard } }
						add_unit_leader_trait = old_guard
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_politically_connected } }
						add_unit_leader_trait = politically_connected
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_career_officer } }
						add_unit_leader_trait = career_officer
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_cautious } }
						add_unit_leader_trait = trait_cautious
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_reckless } }
						add_unit_leader_trait = trait_reckless
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_media_personality } }
						add_unit_leader_trait = media_personality
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_brave } }
						add_unit_leader_trait = brave
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_harsh_leader } }
						add_unit_leader_trait = harsh_leader
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_infantry_officer } }
						add_unit_leader_trait = infantry_officer
					}
					if = {
						limit = { ROOT = { has_country_flag = gd_trait_armor_officer } }
						add_unit_leader_trait = armor_officer
					}

					# Add skills
					gd_add_selected_skills = yes
				}

				# Clean up the temporary flag from all leaders
				every_army_leader = {
					clr_unit_leader_flag = gd_existing_leader
				}

				# Deduct army XP
				set_variable = { gd_xp_to_deduct = gd_total_cost }
				multiply_variable = { var = gd_xp_to_deduct value = -1 }
				army_experience = var:gd_xp_to_deduct

				# Reset all selections for next use
				gd_reset_selections = yes

				# Auto-randomize name for next general
				gd_randomize_name = yes
			}
		}

		triggers = {

			#############################
			# GLOW VISIBILITY
			# Each glow appears only when its trait is selected
			#############################

			army_leader_trait_1_glow_visible = {
				has_country_flag = gd_trait_brilliant_strategist
			}
			army_leader_trait_2_glow_visible = {
				has_country_flag = gd_trait_inflexible_strategist
			}
			army_leader_trait_3_glow_visible = {
				has_country_flag = gd_trait_old_guard
			}
			army_leader_trait_4_glow_visible = {
				has_country_flag = gd_trait_politically_connected
			}
			army_leader_trait_5_glow_visible = {
				has_country_flag = gd_trait_career_officer
			}
			army_leader_trait_6_glow_visible = {
				has_country_flag = gd_trait_cautious
			}
			army_leader_trait_7_glow_visible = {
				has_country_flag = gd_trait_reckless
			}
			army_leader_trait_8_glow_visible = {
				has_country_flag = gd_trait_media_personality
			}
			army_leader_trait_9_glow_visible = {
				has_country_flag = gd_trait_brave
			}
			army_leader_trait_10_glow_visible = {
				has_country_flag = gd_trait_harsh_leader
			}
			army_leader_trait_11_glow_visible = {
				has_country_flag = gd_trait_infantry_officer
			}
			army_leader_trait_12_glow_visible = {
				has_country_flag = gd_trait_armor_officer
			}

			#############################
			# GENERATE BUTTON - disabled if not enough army XP
			#############################

			army_leader_generate_button_click_enabled = {
				check_variable = { army_experience > gd_total_cost }
			}

			#############################
			# SKILL BUTTON ENABLE/DISABLE
			# Plus disabled at 10, minus disabled at 0
			#############################

			army_leader_skill_1_plus_button_click_enabled = {
				check_variable = { gd_attack_skill < 10 }
			}
			army_leader_skill_1_minus_button_click_enabled = {
				check_variable = { gd_attack_skill > 0 }
			}
			army_leader_skill_2_plus_button_click_enabled = {
				check_variable = { gd_defense_skill < 10 }
			}
			army_leader_skill_2_minus_button_click_enabled = {
				check_variable = { gd_defense_skill > 0 }
			}
			army_leader_skill_3_plus_button_click_enabled = {
				check_variable = { gd_planning_skill < 10 }
			}
			army_leader_skill_3_minus_button_click_enabled = {
				check_variable = { gd_planning_skill > 0 }
			}
			army_leader_skill_4_plus_button_click_enabled = {
				check_variable = { gd_logistics_skill < 10 }
			}
			army_leader_skill_4_minus_button_click_enabled = {
				check_variable = { gd_logistics_skill > 0 }
			}
		}

		properties = {
			attack_skill_level = {
				text = [GetGDAttackSkill]
			}
			defense_skill_level = {
				text = [GetGDDefenseSkill]
			}
			planning_skill_level = {
				text = [GetGDPlanningSkill]
			}
			supply_skill_level = {
				text = [GetGDLogisticsSkill]
			}
			left_generated_leader_name = {
				text = [GetGDFirstName]
			}
			right_generated_leader_name = {
				text = [GetGDLastName]
			}
			generated_leader_cost = {
				text = [GetGDTotalCost]
			}
		}

		dynamic_lists = {
		}

		ai_enabled = {
			always = no
		}

		ai_test_interval = 290
		ai_test_variance = 0.5

		ai_check = {
		}

		ai_weights = {
		}
	}
}
