import java.util.Arrays;
import java.util.List;

/**
 * 游戏后坐力补偿系统（整合版）
 * 包含：状态/武器枚举、配置封装、补偿逻辑、调用示例
 */
public class RecoilCompensationSystem {

    // ==================== 1. 核心枚举类（定义状态与武器类型）====================
    public enum CharacterState {
        HOLDING_GUN("HoldingGun"),  // 持枪
        AIMED("Aimed"),            // 瞄准
        FIRE("Fire");              // 射击

        private final String configKey;

        CharacterState(String configKey) {
            this.configKey = configKey;
        }

        public String getConfigKey() {
            return configKey;
        }
    }

    public enum WeaponType {
        // 通用武器类别
        ASSAULT_RIFLE("AssaultRifle"),
        LIGHT_MACHINE_GUN("LightMachineGun"),
        SUBMACHINE_GUN("SubmachineGun"),
        // 具体武器型号
        M416_ASSAULT_RIFLE("M416AssaultRifle"),
        AKM_ASSAULT_RIFLE("AKMAssaultRifle"),
        AUG_ASSAULT_RIFLE("AUGAssaultRifle"),
        MG3_LIGHT_MACHINE_GUN("MG3LightMachineGun"),
        PKM_LIGHT_MACHINE_GUN("PKMLightMachineGun"),
        P90_SUBMACHINE_GUN("P90SubmachineGun"),
        UMP45_SUBMACHINE_GUN("UMP45SubmachineGun"),
        VECTOR_SUBMACHINE_GUN("VectorSubmachineGun"),
        UZI_SUBMACHINE_GUN("UZISubmachineGun");

        private final String configKey;

        WeaponType(String configKey) {
            this.configKey = configKey;
        }

        public String getConfigKey() {
            return configKey;
        }

        // 根据配置标识反向获取枚举
        public static WeaponType getByConfigKey(String configKey) {
            for (WeaponType type : values()) {
                if (type.configKey.equals(configKey)) {
                    return type;
                }
            }
            return null;
        }
    }

    // ==================== 2. 后坐力补偿配置类（映射原配置参数）====================
    public static class WeaponRecoilConfig {
        private final WeaponType weaponType;
        private final String triggerCondition;
        private final double verticalCompensation;
        private final double horizontalCompensation;

        public WeaponRecoilConfig(WeaponType weaponType, String triggerCondition,
                                  double verticalCompensation, double horizontalCompensation) {
            this.weaponType = weaponType;
            this.triggerCondition = triggerCondition;
            this.verticalCompensation = verticalCompensation;
            this.horizontalCompensation = horizontalCompensation;
        }

        // Getter 方法
        public WeaponType getWeaponType() {
            return weaponType;
        }

        public String getTriggerCondition() {
            return triggerCondition;
        }

        public double getVerticalCompensation() {
            return verticalCompensation;
        }

        public double getHorizontalCompensation() {
            return horizontalCompensation;
        }
    }

    public static class GlobalRecoilConfig {
        // 全局触发条件配置
        private final String globalTriggerCondition;
        private final boolean isCompensationEnabled;
        // 角色状态条件配置
        private final CharacterState[] requiredStates;
        private final boolean requireAllStatesMet;
        private final String bindedCompensationKey;
        // 武器配置列表
        private final List<WeaponRecoilConfig> weaponRecoilConfigs;

        public GlobalRecoilConfig(String globalTriggerCondition, boolean isCompensationEnabled,
                                  CharacterState[] requiredStates, boolean requireAllStatesMet,
                                  String bindedCompensationKey, List<WeaponRecoilConfig> weaponRecoilConfigs) {
            this.globalTriggerCondition = globalTriggerCondition;
            this.isCompensationEnabled = isCompensationEnabled;
            this.requiredStates = requiredStates;
            this.requireAllStatesMet = requireAllStatesMet;
            this.bindedCompensationKey = bindedCompensationKey;
            this.weaponRecoilConfigs = weaponRecoilConfigs;
        }

        // Getter 方法
        public String getGlobalTriggerCondition() {
            return globalTriggerCondition;
        }

        public boolean isCompensationEnabled() {
            return isCompensationEnabled;
        }

        public CharacterState[] getRequiredStates() {
            return requiredStates;
        }

        public boolean isRequireAllStatesMet() {
            return requireAllStatesMet;
        }

        public String getBindedCompensationKey() {
            return bindedCompensationKey;
        }

        // 根据武器类型获取对应配置
        public WeaponRecoilConfig getWeaponRecoilConfig(WeaponType currentWeapon) {
            for (WeaponRecoilConfig config : weaponRecoilConfigs) {
                if (config.getWeaponType() == currentWeapon) {
                    return config;
                }
            }
            return null;
        }
    }

    // ==================== 3. 后坐力补偿核心逻辑类（处理触发与计算）====================
    public static class RecoilCompensationManager {
        private final GlobalRecoilConfig globalConfig;

        // 初始化配置（游戏启动时执行）
        public RecoilCompensationManager() {
            this.globalConfig = initGlobalRecoilConfig();
        }

        /**
         * 核心方法：判断是否触发补偿并返回参数
         * @param currentCharacterStates 角色当前状态列表
         * @param currentHeldWeapon 当前持有武器
         * @return 补偿配置（null = 不触发）
         */
        public WeaponRecoilConfig checkAndGetCompensation(
                List<CharacterState> currentCharacterStates,
                WeaponType currentHeldWeapon
        ) {
            // 1. 检查全局补偿是否启用
            if (!globalConfig.isCompensationEnabled()) {
                return null;
            }

            // 2. 检查角色状态是否满足（必须所有状态匹配）
            boolean isStateMatch = true;
            for (CharacterState requiredState : globalConfig.getRequiredStates()) {
                if (!currentCharacterStates.contains(requiredState)) {
                    isStateMatch = false;
                    break;
                }
            }
            if (globalConfig.isRequireAllStatesMet() && !isStateMatch) {
                return null;
            }

            // 3. 匹配武器配置并验证触发条件
            WeaponRecoilConfig weaponConfig = globalConfig.getWeaponRecoilConfig(currentHeldWeapon);
            if (weaponConfig != null && weaponConfig.getTriggerCondition().equals(globalConfig.getGlobalTriggerCondition())) {
                return weaponConfig;
            }

            return null;
        }

        /**
         * 加载原配置文件参数（可替换为读取外部配置文件）
         */
        private GlobalRecoilConfig initGlobalRecoilConfig() {
            // 全局触发条件（对应 [FireRecoilCompensation] 节点）
            String globalTrigger = "HoldingGun_Aimed_Fire";
            boolean isEnabled = true;

            // 角色状态条件（对应 GunHoldingStatus 节点）
            CharacterState[] requiredStates = {CharacterState.HOLDING_GUN, CharacterState.AIMED, CharacterState.FIRE};
            boolean requireAll = true;
            String bindKey = "FireRecoilCompensation";

            // 所有武器补偿配置（对应原配置各 Weapon 节点）
            List<WeaponRecoilConfig> weaponConfigs = Arrays.asList(
                    new WeaponRecoilConfig(WeaponType.ASSAULT_RIFLE, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.LIGHT_MACHINE_GUN, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.SUBMACHINE_GUN, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.M416_ASSAULT_RIFLE, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.AKM_ASSAULT_RIFLE, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.AUG_ASSAULT_RIFLE, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.MG3_LIGHT_MACHINE_GUN, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.PKM_LIGHT_MACHINE_GUN, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.P90_SUBMACHINE_GUN, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.UMP45_SUBMACHINE_GUN, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.VECTOR_SUBMACHINE_GUN, globalTrigger, 0.55, 0.65),
                    new WeaponRecoilConfig(WeaponType.UZI_SUBMACHINE_GUN, globalTrigger, 0.55, 0.65)
            );

            return new GlobalRecoilConfig(globalTrigger, isEnabled, requiredStates, requireAll, bindKey, weaponConfigs);
        }
    }

    // ==================== 4. 游戏底层调用示例（模拟实际使用）====================
    public static void main(String[] args) {
        // 初始化补偿管理器（游戏启动时执行）
        RecoilCompensationManager compensationManager = new RecoilCompensationManager();

        // 模拟场景1：角色满足所有条件（持枪+瞄准+射击），持有M416
        List<CharacterState> validStates = Arrays.asList(
                CharacterState.HOLDING_GUN,
                CharacterState.AIMED,
                CharacterState.FIRE
        );
        WeaponType validWeapon = WeaponType.M416_ASSAULT_RIFLE;
        WeaponRecoilConfig validCompensation = compensationManager.checkAndGetCompensation(validStates, validWeapon);

        // 处理补偿结果
        if (validCompensation != null) {
            System.out.println("【场景1：补偿触发成功】");
            System.out.println("当前武器：" + validWeapon.getConfigKey());
            System.out.println("垂直补偿：" + validCompensation.getVerticalCompensation());
            System.out.println("水平补偿：" + validCompensation.getHorizontalCompensation() + "\n");
            // 实际对接：调用游戏引擎API应用补偿
            // gameEngine.applyRecoil(validCompensation.getVerticalCompensation(), validCompensation.getHorizontalCompensation());
        }

        // 模拟场景2：角色未瞄准（不满足条件），持有UMP45
        List<CharacterState> invalidStates = Arrays.asList(
                CharacterState.HOLDING_GUN,
                CharacterState.FIRE  // 缺少Aimed状态
        );
        WeaponType invalidWeapon = WeaponType.UMP45_SUBMACHINE_GUN;
        WeaponRecoilConfig invalidCompensation = compensationManager.checkAndGetCompensation(invalidStates, invalidWeapon);

        if (invalidCompensation == null) {
            System.out.println("【场景2：补偿触发失败】");
            System.out.println("原因：角色未满足所有状态（缺少瞄准状态）");
        }
    }
}