#########################################################
#	Production Strategies
#########################################################

#########################################################
#	Diplomacy Strategies
#########################################################

# Ensure a historical alliace between PRC and CHI
EAI_PRC_CHI_alliance = {
	allowed = { tag = PRC }
	enable = {
		is_historical_focus_on = yes
		
		CHI = { 
			OR = {
				has_completed_focus = CHI_united_front 
				focus_progress = { focus = CHI_united_front progress > 0.9 }
				CHI = { has_war_with = JAP }
			}
		}
		
		NOT = {
			AND = {
				date > 1940.1.1
			
				NOT = { CHI = { has_war_with = JAP } }
			}
		}
	}
	
	abort_when_not_enabled = yes
	
	ai_strategy = { type = diplo_action_desire id = CHI target = join_faction value = 1000 }
	ai_strategy = { type = diplo_action_acceptance id = CHI target = offer_join_faction value = 1000 }
}

#########################################################
#	Unit Strategies
#########################################################

EAI_CHI_no_coastal_garrison = {
	allowed = { 
		is_literally_china = yes
	}
	enable = {
		JAP = { is_ai = yes }
		has_war_with = JAP
		NOT = { check_variable = { global.EAI_sino_japanese_war = 1 } } # favor china
	}

	abort_when_not_enabled = yes

	ai_strategy = { type = garrison value = -2000 }
}

###########################
# 	Japanese invasion
###########################

EAI_CHI_ignore_warlords = {
	allowed = { 
		is_literally_china = yes
	}
	enable = {
		is_historical_focus_on = yes
	}

	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_unit_request
		value = -200

		country_trigger = {
			is_ai = yes

			NOT = { has_war_with = FROM }

			is_literally_china = yes
		}
	}
}

EAI_CHI_ignore_other_countries = {
	allowed = { 
		is_literally_china = yes
	}
	enable = {
		is_historical_focus_on = yes
		has_war_with = JAP
	}

	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_unit_request
		value = -200

		country_trigger = {
			is_ai = yes

			NOT = { has_war_with = FROM }
		}
	}
}

### Misc

EAI_CHI_china_MISC_dont_rush_indochina = {
	allowed = {
		is_literally_china = yes
	}
	enable = {
		has_global_flag = EAI_JAP_transferred_indochina_60d
		JAP = { is_ai = yes }
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		ordertype = front
		ratio = 0.5
		priority = 800
		ordertype = front
		execute_order = no
		manual_attack = no

		strategic_region = 142
	}
}

EAI_CHI_china_MISC_expand_army_instead_of_attacking = {
	allowed = {
		is_literally_china = yes
	}
	enable = {
		check_variable = { EAI_fielded_eq_ratio < 0.95 }
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		ordertype = front
		ratio = 0.25
		priority = 1000
		ordertype = front
		execution_type = careful
		execute_order = no

		state_trigger = {
			is_in_array = { global.EAI_MAP_china_theatre = THIS.id }
			NOT = {
				region = 164
				region = 247
            	region = 248
				state = 743
				state = 597
			}
		}
	}

	ai_strategy = {
		type = force_concentration_factor
		value = -100
	}
}

### Prepare

EAI_CHI_china_PREPARE_JAP = {
	allowed = { 
		is_literally_china = yes
	}
	enable = {
		is_literally_china = yes

		NOT = { has_war_with = CHI }
		
		date < 1939.1.1
		
		has_war = no
		
		OR = {
			JAP = { is_justifying_wargoal_against = CHI }
			JAP = { has_wargoal_against = CHI }
			JAP = { focus_progress = { focus = JAP_reinforce_the_beijing_garrison progress > 0 }  }
		}
	}
	abort = {
		OR = {
			has_war = yes
			
			NOT = { date < 1939.1.1 }
		}
	}
	
	ai_strategy = { type = prepare_for_war id = "JAP" value = 200 }
	ai_strategy = { type = prepare_for_war id = "MEN" value = 200 }
	ai_strategy = { type = prepare_for_war id = "MAN" value = 200 }
}

### Deploy

EAI_CHI_china_DEPLOY_garrison_1 = {
	allowed = { 
		is_literally_china = yes
	}
	enable = {
		if = {
			limit = {
				OR = {
					is_literally_china = yes
				}
			}
		
			NOT = { EAI_MILITARY_home_threatened_TRIGGER = yes }
		}

		OR = {
			has_war_with = JAP
			has_global_flag = { flag = EAI_JAP_blitz_china_1000d value = 1 days < 90 }
			AND = {
				JAP = {
					focus_progress = { focus = JAP_reinforce_the_beijing_garrison progress > 0 } 
					NOT = { has_completed_focus = JAP_reinforce_the_beijing_garrison }
				}
			}
		}

		OR = {
			JAP = { is_ai = no }
			check_variable = { global.EAI_sino_japanese_war = 1 } # favor china
		}

		OR = {
			has_country_flag = EAI_MILITARY_trend_stagnant_war
			NOT = {
				AND = {
					OR = {
						AND = {
							surrender_progress > 0.1
							NOT = { surrender_progress > 0.2 }
						}
						AND = {
							has_country_flag = { flag = EAI_MILITARY_trend_losing_war value = 1 days > 60 }
							NOT = { check_variable = { EAI_MILITARY_trend_value > 0.02 } }
						}
					}
				}
				AND = {
					OR = {
						AND = {
							surrender_progress > 0.2
						}
						AND = {
							has_country_flag = { flag = EAI_MILITARY_trend_losing_war value = 1 days > 60 }
							check_variable = { EAI_MILITARY_trend_value > 0.02 }
						}
					}
				}
			}
		}
	}

	abort_when_not_enabled = yes

	ai_strategy = {
		type = put_unit_buffers
		ratio = 0.4
		states = {
			594
			591
			592
			593
			595
			596
			613
			598
			597
			743
		}
		subtract_invasions_from_need = no
		subtract_fronts_from_need = no
	}
}

EAI_CHI_china_DEPLOY_garrison_2 = {
	allowed = { 
		is_literally_china = yes
	}
	enable = {
		if = {
			limit = {
				OR = {
					is_literally_china = yes
				}
			}
		
			NOT = { EAI_MILITARY_home_threatened_TRIGGER = yes }
		}

		OR = {
			has_war_with = JAP
			has_global_flag = { flag = EAI_JAP_blitz_china_1000d value = 1 days < 90 }
			AND = {
				JAP = {
					focus_progress = { focus = JAP_reinforce_the_beijing_garrison progress > 0 } 
					NOT = { has_completed_focus = JAP_reinforce_the_beijing_garrison }
				}
			}
		}

		OR = {
			JAP = { is_ai = no }
			check_variable = { global.EAI_sino_japanese_war = 1 } # favor china
		}

		OR = {
			AND = {
				surrender_progress > 0.1
				NOT = { surrender_progress > 0.2 }
			}
			AND = {
				has_country_flag = { flag = EAI_MILITARY_trend_losing_war value = 1 days > 60 }
				NOT = { check_variable = { EAI_MILITARY_trend_value > 0.02 } }
			}
		}
	}

	abort_when_not_enabled = yes

	ai_strategy = {
		type = put_unit_buffers
		ratio = 0.3
		states = {
			594
			591
			592
			593
			595
			596
			613
			598
			597
			743
		}
		subtract_invasions_from_need = no
		subtract_fronts_from_need = no
	}
}

EAI_CHI_china_DEPLOY_garrison_3 = {
	allowed = { 
		is_literally_china = yes
	}
	enable = {
		if = {
			limit = {
				OR = {
					is_literally_china = yes
				}
			}
		
			NOT = { EAI_MILITARY_home_threatened_TRIGGER = yes }
		}
		
		OR = {
			has_war_with = JAP
			has_global_flag = { flag = EAI_JAP_blitz_china_1000d value = 1 days < 90 }
			AND = {
				JAP = {
					focus_progress = { focus = JAP_reinforce_the_beijing_garrison progress > 0 } 
					NOT = { has_completed_focus = JAP_reinforce_the_beijing_garrison }
				}
			}
		}

		OR = {
			JAP = { is_ai = no }
			check_variable = { global.EAI_sino_japanese_war = 1 } # favor china
		}

		OR = {
			AND = {
				surrender_progress > 0.2
			}
			AND = {
				has_country_flag = { flag = EAI_MILITARY_trend_losing_war value = 1 days > 60 }
				check_variable = { EAI_MILITARY_trend_value > 0.02 }
			}
		}
	}

	abort_when_not_enabled = yes

	ai_strategy = {
		type = put_unit_buffers
		ratio = 0.2
		states = {
			594
			591
			592
			593
			595
			596
			613
			598
			597
			743
		}
		subtract_invasions_from_need = no
		subtract_fronts_from_need = no
	}
}

### Defensive

EAI_CHI_china_DEFENSIVE_dont_push_north = {
	allowed = {
		is_literally_china = yes
	}
	enable = {
		has_war_with = JAP

		OR = {
			JAP = { is_ai = no }
			check_variable = { global.EAI_sino_japanese_war = 1 } # favor china
			AND = {
				check_variable = { global.EAI_sino_japanese_war = 2 } # stalemate
				CHI = { surrender_progress > 0.5 }
			}
		}

		has_defensive_war = yes
		OR = {
			has_country_flag = EAI_MILITARY_trend_losing_war
			has_country_flag = EAI_japanese_war_defense
		}
		NOT = { JAP = { has_country_flag = EAI_MILITARY_trend_losing_war } }
		
		EAI_MILITARY_army_shortage_MINOR = yes
	}
	abort = {
		NOT = {
			AND = {
				has_war_with = JAP

				OR = {
					JAP = { is_ai = no }
					check_variable = { global.EAI_sino_japanese_war = 1 } # favor china
					AND = {
						check_variable = { global.EAI_sino_japanese_war = 2 } # stalemate
						CHI = { surrender_progress > 0.5 }
					}
				}

				has_defensive_war = yes
				OR = {
					has_country_flag = EAI_MILITARY_trend_losing_war
					has_country_flag = EAI_japanese_war_defense
				}
				NOT = { JAP = { has_country_flag = EAI_MILITARY_trend_losing_war } }
				
				EAI_MILITARY_army_shortage_MINOR_abort = yes
			}
		}
	}

	ai_strategy = {
		type = front_control
		ordertype = front
		ratio = 0.5
		priority = 800
		ordertype = front
		execute_order = no

		state_trigger = {
			NOT = { is_coastal = yes }
			CONTROLLER = {
				OR = {
					tag = JAP
					has_war_together_with = JAP
				}
			}
		}
	}
}

EAI_CHI_china_DEFENSIVE_initial_defense = {
	allowed = {
		is_literally_china = yes
	}
	enable = {
		OR = {
			has_global_flag = { flag = EAI_JAP_blitz_china_1000d value = 1 days < 90 }
			AND = {
				JAP = {
					focus_progress = { focus = JAP_reinforce_the_beijing_garrison progress > 0 } 
					NOT = { has_completed_focus = JAP_reinforce_the_beijing_garrison }
				}
			}
		}
		
		OR = {
			JAP = { is_ai = no }
			check_variable = { global.EAI_sino_japanese_war = 1 } # favor china
			AND = {
				check_variable = { global.EAI_sino_japanese_war = 2 } # stalemate
				CHI = { surrender_progress > 0.5 }
			}
		}

		NOT = { has_country_flag = EAI_MILITARY_trend_losing_war }
		NOT = { JAP = { has_country_flag = EAI_MILITARY_trend_losing_war } }
	}

	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		ordertype = front
		ratio = 0.1
		priority = 900
		execute_order = no
		
		state_trigger = {
			NOT = { is_coastal = yes }
			CONTROLLER = {
				OR = {
					tag = JAP
					has_war_together_with = JAP
				}
			}
		}
	}
}

### Offensive

EAI_CHI_china_OFFENSIVE_operational_resume = {
	enable = {
		is_in_faction_with = CHI
		has_war_with = JAP
		has_country_flag = EAI_operational_initial_resume_timer
		NOT = { has_country_flag = EAI_MILITARY_operational_pause_timer }
		EAI_MILITARY_army_readiness_HIGH = yes
		NOT = { check_variable = { EAI_fielded_eq_ratio < 0.95 } }
	}
	abort = {
		NOT = {
			AND = {
				is_in_faction_with = CHI
		has_war_with = JAP
				has_country_flag = EAI_operational_initial_resume_timer
				NOT = { has_country_flag = EAI_MILITARY_operational_pause_timer }
				EAI_MILITARY_army_readiness_HIGH_abort = yes
			}
		}
	}

	ai_strategy = {
		type = front_control
		ordertype = front
		ratio = 0.75
		priority = 800
		execution_type = rush_weak
		execute_order = yes
		
		country_trigger = { 
			OR = { 
				tag = CHI 
				has_war_together_with = CHI 
			} 
		}
	}
}

EAI_CHI_china_OFFENSIVE_protect_coast = {
	allowed = {
		is_literally_china = yes
	}
	enable = {
		has_war_with = JAP
		EAI_MILITARY_army_readiness_MEDIUM = yes
	}
	abort = {
		NOT = {
			AND = {
				has_war_with = JAP
				EAI_MILITARY_army_readiness_MEDIUM_abort = yes
			}
		}
	}

	abort_when_not_enabled = yes

	ai_strategy = {
		type = force_concentration_front_factor
		ratio = 0.5
		value = 50
		
		state_trigger = {
			OR = {
				region = 164
				region = 247
            	region = 248
				state = 743
				state = 597
			}
		}
	}
	ai_strategy = {
		type = force_concentration_target_weight
		value = 50
		
		state_trigger = {
			OR = {
				region = 164
				region = 247
            	region = 248
				state = 743
				state = 597
			}
		}
	}

	ai_strategy = {
		type = front_control
		ordertype = front
		ratio = 0.5
		priority = 900
		execution_type = rush_weak

		state_trigger = {
			OR = {
				region = 164
				region = 247
            	region = 248
				state = 743
				state = 597
			}
		}
	}

	ai_strategy = {
		type = front_control
		ordertype = front
		ratio = 0.5
		priority = 900
		execution_type = rush_weak

		state_trigger = {
			OR = {
				region = 164
				region = 247
            	region = 248
				state = 743
				state = 597
			}
		}
	}

	ai_strategy = {
		type = front_unit_request
		value = 25

		state_trigger = {
			OR = {
				region = 164
				region = 247
            	region = 248
				state = 743
				state = 597
			}
		}
	}
}

### EAI settings

EAI_CHI_china_DEFENSIVE_stalemate = {
	allowed = {
		is_literally_china = yes
	}
	enable = {
		is_historical_focus_on = yes
		check_variable = { global.EAI_sino_japanese_war = 2 } # stalemate
		has_war_with = JAP
		JAP = { is_ai = yes }
		
		### Conditions

		date < 1946.1.1
		if = { limit = { NOT = { has_global_flag = EAI_JAP_blitz_pacific_1000d } }
			alliance_strength_ratio < 3
		}
		NOT = { JAP = { surrender_progress > 0.1 } }
		CHI = { surrender_progress < 0.5 }
		NOT = { 
			any_enemy_country = { 
				NOT = { tag = JAP } 
				is_neighbor_of = ROOT
				is_major = yes 
			} 
		}
		NOT = { has_country_flag = EAI_operational_initial_resume_timer }
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = front_control
		ordertype = front
		ratio = 0.5
		priority = 1000
		execution_type = careful
		
		tag = JAP
		tag = MAN
		tag = MEN
	}

	ai_strategy = {
		type = force_concentration_front_factor
		ratio = 0.5
		value = -100
		
		tag = JAP
		tag = MAN
		tag = MEN
	}

	ai_strategy = {
		type = force_concentration_target_weight
		value = -100
		
		tag = JAP
		tag = MAN
		tag = MEN
	}
}