import 'package:flutter/material.dart';
import '../../game/enums.dart';
import '../../game/models.dart';
import '../../game/config.dart';

class RiskSelectionDialog extends StatelessWidget {
  final ActionType actionType;
  final Character player;
  final Function(RiskLevel) onSelected;

  const RiskSelectionDialog({
    super.key,
    required this.actionType,
    required this.player,
    required this.onSelected,
  });

  @override
  Widget build(BuildContext context) {
    final baseValue = actionType == ActionType.attack
        ? player.totalAtk
        : player.totalDefense;

    return SimpleDialog(
      title: Text("Select Risk Level for ${actionType.name}"),
      children: RiskLevel.values.map((risk) {
        String infoText = "";
        Color infoColor = Colors.black;
        double efficiency = 0.0;
        int expectedValue = 0;

        switch (risk) {
          case RiskLevel.safe:
            efficiency = actionType == ActionType.attack
                ? BattleConfig.attackSafeEfficiency
                : BattleConfig.defendSafeEfficiency;
            infoColor = ThemeConfig.riskSafe;
            break;
          case RiskLevel.normal:
            efficiency = actionType == ActionType.attack
                ? BattleConfig.attackNormalEfficiency
                : BattleConfig.defendNormalEfficiency;
            infoColor = ThemeConfig.riskNormal;
            break;
          case RiskLevel.risky:
            efficiency = actionType == ActionType.attack
                ? BattleConfig.attackRiskyEfficiency
                : BattleConfig.defendRiskyEfficiency;
            infoColor = ThemeConfig.riskRisky;
            break;
        }

        expectedValue = (baseValue * efficiency).toInt();
        String valueUnit = actionType == ActionType.attack ? "Dmg" : "Armor";

        double baseChance = 0.0;
        switch (risk) {
          case RiskLevel.safe:
            baseChance = BattleConfig.safeBaseChance;
            break;
          case RiskLevel.normal:
            baseChance = BattleConfig.normalBaseChance;
            break;
          case RiskLevel.risky:
            baseChance = BattleConfig.riskyBaseChance;
            break;
        }

        double finalChance = baseChance + (player.totalLuck / 100.0);
        if (finalChance > 1.0) finalChance = 1.0;
        String successRate = "${(finalChance * 100).toInt()}%";

        infoText =
            "Success: $successRate, Eff: ${(efficiency * 100).toInt()}% ($expectedValue $valueUnit)";

        return SimpleDialogOption(
          onPressed: () => onSelected(risk),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                risk.name.toUpperCase(),
                style: const TextStyle(fontWeight: FontWeight.bold),
              ),
              Text(infoText, style: TextStyle(fontSize: 12, color: infoColor)),
            ],
          ),
        );
      }).toList(),
    );
  }
}
