#include <vector>
#include <string>
#include <map>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;

// 枚举类型定义
enum Position { GK, DF, MF, FW };
enum Personality { AMBITIOUS, TEAM_PLAYER, LAZY };
enum Mentality { AGGRESSIVE, BALANCED, DEFENSIVE }; // 修正拼写错误

// 转换枚举值为字符串的辅助函数
string getPositionName(Position p) {
    switch(p) {
        case GK: return "门将";
        case DF: return "后卫";
        case MF: return "中场";
        case FW: return "前锋";
    }
    return "";
}

string getPersonalityName(Personality p) {
    switch(p) {
        case AMBITIOUS:  return "雄心勃勃";
        case TEAM_PLAYER: return "团队型";
        case LAZY:      return "懒散";
    }
    return "";
}

string getMentalityName(Mentality m) {
    switch(m) {
        case AGGRESSIVE: return "进攻型";
        case BALANCED:   return "平衡型";
        case DEFENSIVE:  return "防守型";
    }
    return "";
}

// 球员类
class Player {
public:
    string name;
    int age;
    Position position;
    int currentAbility;
    int potential;
    Personality personality;
    Mentality mentality;
    bool isInjured;

    Player(string n, int a, Position p, int ca, int pot, 
           Personality per, Mentality men)
        : name(n), age(a), position(p), currentAbility(ca),
          potential(pot), personality(per), mentality(men),
          isInjured(false) {}
};

// 球队类
class Team {
public:
    string name;
    int budget;
    vector<Player> players;
    vector<Player> startingXI;

    Team(string n) : name(n), budget(10000000) {}

    void addPlayer(const Player& p) {
        players.push_back(p);
    }

    void sellPlayer(int index) {
        if(index >= 0 && index < players.size()) {
            budget += players[index].currentAbility * 1000;
            players.erase(players.begin() + index);
        }
    }

    bool buyPlayer(Player& p) {
        int price = p.currentAbility * 1200;
        if (budget >= price) {
            budget -= price;
            players.push_back(p);
            return true;
        }
        return false;
    }
};

// 联赛类
class League {
public:
    vector<Team> teams;
    map<string, int> standings;

    void simulateMatch(Team& home, Team& away) {
        int homeStrength = 0, awayStrength = 0;
        for (auto& p : home.startingXI) homeStrength += p.currentAbility;
        for (auto& p : away.startingXI) awayStrength += p.currentAbility;

        homeStrength += rand() % 20;
        awayStrength += rand() % 20;

        if (homeStrength > awayStrength) {
            standings[home.name] += 3;
        } else if (homeStrength < awayStrength) {
            standings[away.name] += 3;
        } else {
            standings[home.name] += 1;
            standings[away.name] += 1;
        }
    }
};

// 游戏管理类
class GameManager {
private:
    Team userTeam;
    League currentLeague;
    vector<Player> transferMarket;

    // 初始化用户球队
    void initializeUserTeam() {
        vector<string> firstNames = {"张", "王", "李", "赵", "陈", "刘", "杨", "黄", "周", "吴"};
        vector<string> lastNames = {"强", "伟", "芳", "娜", "敏", "杰", "磊", "洋", "勇", "艳"};
        
        for(int i = 0; i < 25; ++i) {
            string name = firstNames[rand()%firstNames.size()] + 
                         lastNames[rand()%lastNames.size()];
            int age = 18 + rand()%10;
            Position pos = static_cast<Position>(rand()%4);
            int ability = 60 + rand()%20;
            int potential = ability + 5 + rand()%15;
            
            userTeam.players.push_back(Player(
                name, age, pos, ability, potential,
                static_cast<Personality>(rand()%3),
                static_cast<Mentality>(rand()%3)
            ));
        }
    }

    // 生成随机球员
    Player generateRandomPlayer() {
        vector<string> enFirstNames = {"James", "John", "Robert", "Michael", "David", 
                                      "Emma", "Olivia", "Ava", "Isabella", "Sophia"};
        vector<string> enLastNames = {"Smith", "Johnson", "Williams", "Brown", "Jones",
                                     "Miller", "Davis", "Garcia", "Rodriguez", "Wilson"};
        
        string name = enFirstNames[rand()%enFirstNames.size()] + " " +
                     enLastNames[rand()%enLastNames.size()];
        
        int age = 17 + rand()%23;
        Position pos = static_cast<Position>(rand()%4);
        
        int baseAbility = 50;
        if(age < 20) baseAbility = 60 + rand()%15;
        else if(age < 28) baseAbility = 70 + rand()%20;
        else baseAbility = 65 + rand()%15;
        
        int potential = baseAbility + 5 + rand()%15;
        if(age > 25) potential = baseAbility + rand()%5;
        
        return Player(
            name, age, pos,
            baseAbility, 
            min(99, potential),
            static_cast<Personality>(rand()%3),
            static_cast<Mentality>(rand()%3)
        );
    }

    void initializeTransferMarket() {
        for(int i = 0; i < 100; ++i) {
            transferMarket.push_back(generateRandomPlayer());
        }
        
        // 添加明星球员
        transferMarket.push_back(Player("C. Ronaldo", 37, FW, 88, 90, AMBITIOUS, AGGRESSIVE));
        transferMarket.push_back(Player("L. Messi", 35, FW, 92, 95, TEAM_PLAYER, BALANCED));
        transferMarket.push_back(Player("Neymar Jr", 30, FW, 89, 91, AMBITIOUS, AGGRESSIVE));
    }

    void initializeLeague() {
        currentLeague.teams.push_back(userTeam);
        
        Team ai1("AI Team 1"), ai2("AI Team 2");
        ai1.players = {
            Player("AI1_Player1", 25, GK, 80, 85, TEAM_PLAYER, BALANCED),
            Player("AI1_Player2", 27, DF, 82, 86, AMBITIOUS, DEFENSIVE)
        };
        ai2.players = {
            Player("AI2_Player1", 26, FW, 85, 88, AMBITIOUS, AGGRESSIVE),
            Player("AI2_Player2", 24, MF, 84, 90, TEAM_PLAYER, BALANCED)
        };
        currentLeague.teams.push_back(ai1);
        currentLeague.teams.push_back(ai2);
    }

public:
    GameManager() : userTeam("我的球队") {
        srand(time(0));
        initializeUserTeam();
        initializeTransferMarket();
        initializeLeague();
    }

    void showMainMenu() {
        while(true) {
            cout << "\n===== 足球经理 =====" << endl;
            cout << "1. 查看阵容\n2. 安排阵容\n3. 转会市场\n4. 训练球员\n5. 进行比赛\n6. 退出\n选择: ";
            
            int choice;
            cin >> choice;
            
            switch(choice) {
                case 1: viewSquad(); break;
                case 2: arrangeFormation(); break;
                case 3: transferMarketMenu(); break;
                case 4: trainPlayers(); break;
                case 5: playMatch(); break;
                case 6: return;
                default: cout << "无效选择!" << endl;
            }
        }
    }

private:
    void viewSquad() {
        cout << "\n=== 当前阵容 (" << userTeam.players.size() << "人) ===" << endl;
        cout << "预算: $" << userTeam.budget << endl;
        for (size_t i = 0; i < userTeam.players.size(); ++i) {
            auto& p = userTeam.players[i];
            cout << i+1 << ". " << p.name 
                 << "\n   年龄: " << p.age
                 << "  位置: " << getPositionName(p.position)
                 << "\n   能力: " << p.currentAbility
                 << "  潜力: " << p.potential
                 << "\n   性格: " << getPersonalityName(p.personality)
                 << "  心态: " << getMentalityName(p.mentality)
                 << endl << endl;
        }
    }

    void arrangeFormation() {
        cout << "\n=== 安排阵容 ===" << endl;
        viewSquad();
        
        userTeam.startingXI.clear();
        cout << "选择11名首发球员(输入编号,空格分隔):";
        
        vector<int> choices(11);
        for (int i = 0; i < 11; ++i) {
            cin >> choices[i];
            if (choices[i] > 0 && choices[i] <= userTeam.players.size()) {
                userTeam.startingXI.push_back(userTeam.players[choices[i]-1]);
            }
        }
        cout << "阵容安排完成!" << endl;
    }

    void transferMarketMenu() {
        while(true) {
            cout << "\n=== 转会市场 ===" << endl;
            cout << "1. 购买球员\n2. 出售球员\n3. 返回\n选择: ";
            
            int choice;
            cin >> choice;
            
            if(choice == 1) {
                cout << "\n可购买球员:" << endl;
                for(size_t i = 0; i < transferMarket.size(); ++i) {
                    auto& p = transferMarket[i];
                    cout << i+1 << ". " << p.name 
                         << " | 能力:" << p.currentAbility 
                         << " | 价格: $" << p.currentAbility*1200 
                         << endl;
                }
                cout << "选择要购买的球员编号:";
                int index;
                cin >> index;
                if(index > 0 && index <= transferMarket.size()) {
                    if(userTeam.buyPlayer(transferMarket[index-1])) {
                        cout << "购买成功!" << endl;
                        transferMarket.erase(transferMarket.begin()+index-1);
                    } else {
                        cout << "预算不足!" << endl;
                    }
                }
            }
            else if(choice == 2) {
                viewSquad();
                cout << "选择要出售的球员编号:";
                int index;
                cin >> index;
                if(index > 0 && index <= userTeam.players.size()) {
                    userTeam.sellPlayer(index-1);
                    cout << "出售成功!" << endl;
                }
            }
            else break;
        }
    }

    void trainPlayers() {
        cout << "\n=== 球员训练 ===" << endl;
        viewSquad();
        cout << "选择要训练的球员编号:";
        int index;
        cin >> index;
        
        if(index > 0 && index <= userTeam.players.size()) {
            Player& p = userTeam.players[index-1];
            if(p.currentAbility < p.potential) {
                int improvement = 1 + rand()%2;
                p.currentAbility = min(p.potential, p.currentAbility + improvement);
                cout << p.name << " 能力提升至 " << p.currentAbility << endl;
            } else {
                cout << "该球员已达到潜力上限" << endl;
            }
        }
    }

    void playMatch() {
        cout << "\n=== 比赛日 ===" << endl;
        if(userTeam.startingXI.size() != 11) {
            cout << "错误:请先安排11人首发阵容!" << endl;
            return;
        }
        
        Team& opponent = currentLeague.teams[rand()%currentLeague.teams.size()];
        cout << "对阵: " << opponent.name << endl;
        
        currentLeague.simulateMatch(userTeam, opponent);
        cout << "\n=== 当前积分榜 ===" << endl;
        for (auto& [name, points] : currentLeague.standings) {
            cout << name << ": " << points << "分" << endl;
        }
    }
};

int main() {
    GameManager game;
    game.showMainMenu();
    return 0;
}