import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.List;

public class TeamRostersGUI extends JFrame {
    private JPanel mainPanel;
    private FootballTeamRostersPage teamRostersPage;

    public TeamRostersGUI() {
        setTitle("Football Team Rosters Generator");
        setSize(1000, 1000);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        mainPanel = new JPanel(new CardLayout());
        teamRostersPage = new FootballTeamRostersPage();

        mainPanel.add(createCoverPage(), "cover");
        mainPanel.add(teamRostersPage, "teamRosters");

        getContentPane().add(mainPanel, BorderLayout.CENTER);
    }

    private JPanel createCoverPage() {
        JPanel coverPanel = new JPanel(new BorderLayout());

        // Set a nostalgic retro background image (replace with your image file)
        ImageIcon backgroundImage = new ImageIcon("C:\\Users\\Vusum\\OneDrive\\Documents\\13774adb-2bfa-4582-8253-d21f4c4b7944_clipdrop-enhance.jpg");
        JLabel backgroundLabel = new JLabel(backgroundImage);
        coverPanel.add(backgroundLabel, BorderLayout.CENTER);

        // Create a "Start" button
        JButton startButton = new JButton("Start");
        startButton.setFont(new Font("Arial", Font.BOLD, 24));
        startButton.setForeground(Color.RED);
        startButton.setPreferredSize(new Dimension(200, 60));
        startButton.addActionListener(e -> showTeamRostersPage());

        JPanel buttonPanel = new JPanel();
        buttonPanel.setOpaque(false);
        buttonPanel.add(startButton);

        coverPanel.add(buttonPanel, BorderLayout.SOUTH);

        return coverPanel;
    }
 public class ManagerCareerPage extends JPanel {
    private Team team;
    private JTextArea teamInfoTextArea;
    private JButton signContractButton;

    public ManagerCareerPage(Team team) {
        this.team = team;
        setBackground(new Color(0, 102, 204));
        setLayout(new BorderLayout());

        teamInfoTextArea = new JTextArea(20, 40);
        teamInfoTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(teamInfoTextArea);
        add(scrollPane, BorderLayout.CENTER);

        // Display team information
        displayTeamInfo();

        // Create a button to sign the contract
        signContractButton = new JButton("Sign Contract");
        signContractButton.addActionListener(e -> signContract());
        add(signContractButton, BorderLayout.SOUTH);
    }

    private void displayTeamInfo() {
    StringBuilder teamInfoBuilder = new StringBuilder();
    teamInfoBuilder.append("Team Name: ").append(team.getName()).append("\n");
    teamInfoBuilder.append("Formation: ").append(team.getFormation()).append("\n");
    teamInfoBuilder.append("Transfer Budget: $").append(team.getTransferBudget()).append("m\n");
    teamInfoBuilder.append("Players:\n");

    List<Player> players = team.getPlayers();
    for (Player player : players) {
        teamInfoBuilder.append("  ").append(player.getName()).append(" (")
                .append(player.getPosition()).append("), Rating: ").append(player.getRating()).append("\n");
    }

    teamInfoTextArea.setText(teamInfoBuilder.toString());
}

    private void signContract() {
        // Add your logic here to handle the contract signing process.
        // You can display a confirmation message or perform any other actions as needed.
        JOptionPane.showMessageDialog(this, "Contract signed successfully!", "Contract Signed", JOptionPane.INFORMATION_MESSAGE);
    }
}
    private void showTeamRostersPage() {
        ((CardLayout) mainPanel.getLayout()).show(mainPanel, "teamRosters");
        teamRostersPage.loadTeamsFromFile("C:\\Users\\Vusum\\OneDrive\\Documents\\teams.txt");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }

            TeamRostersGUI app = new TeamRostersGUI();
            app.setVisible(true);
        });
    }
}

class FootballTeamRostersPage extends JPanel {
    private List<Team> teams;
    private List<Player> players;
    private Map<String, List<Player>> teamRosters;
    private JTextArea rosterTextArea;
    private JTextArea unpickedPlayersTextArea;
    private JButton generateButton;
    private JButton transferMarketButton;
    private JButton viewTeamButton;
    private JTextArea transferMarketTextArea;
    private JPanel managerCareerPanel = null;
    private JButton managerCareerButton;
    private JTextField searchField;
    private JButton searchButton;

    public FootballTeamRostersPage() {
        setBackground(new Color(0, 102, 204));
        setLayout(null);

        

        teamRosters = new HashMap<>();
        players = new ArrayList<>();
        rosterTextArea = new JTextArea(20, 40);
        rosterTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(rosterTextArea);
        scrollPane.setBounds(50, 50, 300, 400);

        unpickedPlayersTextArea = new JTextArea(20, 40);
        unpickedPlayersTextArea.setEditable(false);
        JScrollPane unpickedScrollPane = new JScrollPane(unpickedPlayersTextArea);
        unpickedScrollPane.setBounds(50, 530, 300, 200);

        generateButton = new JButton("Generate Rosters");
        generateButton.setBounds(50, 480, 200, 40);
        generateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loadPlayersFromFile("C:\\Users\\Vusum\\OneDrive\\Documents\\Players2.txt");
                generateRosters();
                displayRosters();
            }
        });

        
        
        transferMarketButton = new JButton("Transfer Market");
        transferMarketButton.setBounds(270, 480, 200, 40);
        transferMarketButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openTransferMarket();
            }
        });

        viewTeamButton = new JButton("View Team");
        viewTeamButton.setBounds(490, 480, 200, 40);
        viewTeamButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openViewTeam();
            }
        });

        transferMarketTextArea = new JTextArea(20, 40);
        transferMarketTextArea.setEditable(false);
        JScrollPane transferMarketScrollPane = new JScrollPane(transferMarketTextArea);
        transferMarketScrollPane.setBounds(400, 50, 300, 400);

        add(scrollPane);
        add(unpickedScrollPane);
        add(generateButton);
        add(transferMarketButton);
        add(viewTeamButton);
        add(transferMarketScrollPane);
        managerCareerButton = new JButton("Manager Career");
managerCareerButton.setBounds(720, 550, 200, 40);
managerCareerButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        showManagerCareer();
    }
});
add(managerCareerButton);



    }

    private void showManagerCareer() {
    managerCareerPanel = new ManagerCareerPage();
    getParent().add(managerCareerPanel, "managerCareer");
    
    // Switch to the manager career panel
    CardLayout cardLayout = (CardLayout) getParent().getLayout();
    cardLayout.show(getParent(), "managerCareer");
}


    private void showTeamRosters() {
        // Switch back to the team rosters page
        ((CardLayout) getParent().getLayout()).show(getParent(), "teamRosters");
    }

    public void loadTeamsFromFile(String filename) {
        teams = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(":");
                if (parts.length == 3) {
                    String teamName = parts[0].trim();
                    double transferBudget = Double.parseDouble(parts[1].trim().replace("m", ""));
                    String formation = parts[2].trim();
                    Team team = new Team(teamName, transferBudget, formation);
                    teams.add(team);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void loadPlayersFromFile(String filename) {
        players.clear();
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(":");
                if (parts.length == 4) {
                    String playerName = parts[0].trim();
                    String playerPosition = parts[1].trim();
                    int transferPrice = Integer.parseInt(parts[2].trim().replace("m", ""));
                    int rating = Integer.parseInt(parts[3].trim()); // Parse the rating
                    Player player = new Player(playerName, playerPosition, transferPrice, rating, null);

                    players.add(player);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void generateRosters() {
    // Clear previous roster information
    teamRosters.clear();
    List<Player> unpickedPlayers = new ArrayList<>(players);

    for (Team team : teams) {
        List<Player> roster = new ArrayList<>();
        boolean hasGK = false; // Flag to track if the team already has a goalkeeper

        // Fill the roster with random players, ensuring only one goalkeeper
        for (int i = 0; i < 13; i++) { // Maximum 11 players per team (10 outfield + 1 GK)
            if (!unpickedPlayers.isEmpty()) {
                int index = new Random().nextInt(unpickedPlayers.size());
                Player player = unpickedPlayers.get(index);

                if (player.getPosition().equalsIgnoreCase("GK") && hasGK) {
                    // Skip adding another goalkeeper
                    continue;
                }

                if (player.getPosition().equalsIgnoreCase("GK")) {
                    hasGK = true;
                }

                // Track the previous club for transferred players
                if (!team.getName().equals(player.getPreviousClub())) {
                    player.setPreviousClub(team.getName());
                    player.setTransferStatus("Transferred");
                } else {
                    player.setTransferStatus("Free Agent");
                }

                roster.add(player);
                unpickedPlayers.remove(index);
            } else {
                break; // No more players to pick from
            }
        }

        teamRosters.put(team.getName(), roster);
    }

    // Display unpicked players
    displayUnpickedPlayers(unpickedPlayers);
}


    public void displayRosters() {
    JPanel rosterPanel = new JPanel(new GridLayout(teams.size(), 1, 10, 10)); // Rows for each team
    rosterPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    for (Map.Entry<String, List<Player>> entry : teamRosters.entrySet()) {
    String teamName = entry.getKey();
    List<Player> roster = entry.getValue();

    JPanel teamRosterPanel = new JPanel(new GridLayout(6, 5)); // 6 rows x 5 columns for player info
    teamRosterPanel.setBorder(BorderFactory.createTitledBorder(teamName));

    for (Player player : roster) {
        JPanel playerPanel = new JPanel(new GridLayout(3, 1)); // 3 rows for name, position, and rating
        playerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JLabel nameLabel = new JLabel(player.getName());
        nameLabel.setHorizontalAlignment(SwingConstants.CENTER);

        JLabel positionLabel = new JLabel("Position: " + player.getPosition());
        positionLabel.setHorizontalAlignment(SwingConstants.CENTER);

        JLabel ratingLabel = new JLabel("Rating: " + player.getRating());
        ratingLabel.setHorizontalAlignment(SwingConstants.CENTER);

        playerPanel.add(nameLabel);
        playerPanel.add(positionLabel);
        playerPanel.add(ratingLabel);

        teamRosterPanel.add(playerPanel);
    }

    rosterPanel.add(teamRosterPanel);
}


    JScrollPane rosterScrollPane = new JScrollPane(rosterPanel);
    rosterTextArea.setText(""); // Clear the old text
    rosterTextArea.setLayout(new BorderLayout());
    rosterTextArea.add(rosterScrollPane, BorderLayout.CENTER);
}
    public void displayUnpickedPlayers(List<Player> unpickedPlayers) {
    StringBuilder unpickedPlayersBuilder = new StringBuilder("Unpicked Players:\n");

    for (Player player : unpickedPlayers) {
        unpickedPlayersBuilder.append(player.getName()).append(" (")
                .append(player.getPosition()).append("), Rating: ").append(player.getRating());

        // Display transfer status and previous club for realism
        String transferStatus = player.getTransferStatus();
        String previousClub = player.getPreviousClub();
        if (transferStatus != null) {
            unpickedPlayersBuilder.append(", Transfer Status: ").append(transferStatus);
            if (transferStatus.equals("Transferred") && previousClub != null) {
                unpickedPlayersBuilder.append(", Previous Club: ").append(previousClub);
            }
        }

        unpickedPlayersBuilder.append("\n");
    }

    unpickedPlayersTextArea.setText(unpickedPlayersBuilder.toString());
}



   public void openTransferMarket() {
        JFrame transferMarketFrame = new JFrame("Transfer Market");
        transferMarketFrame.setSize(800, 600);
        transferMarketFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        transferMarketFrame.setLocationRelativeTo(this);

        JPanel transferMarketPanel = new JPanel();
        transferMarketPanel.setLayout(new BorderLayout());

        JTextArea transferMarketTextArea = new JTextArea(20, 40);
        transferMarketTextArea.setEditable(false);
        JScrollPane transferMarketScrollPane = new JScrollPane(transferMarketTextArea);
        transferMarketPanel.add(transferMarketScrollPane, BorderLayout.CENTER);

        // Add a "Simulate Transfer Window" button
        JButton simulateButton = new JButton("Simulate Transfer Window");
        simulateButton.setBounds(50, 480, 200, 40);
        simulateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                simulateTransferWindow(transferMarketTextArea);
            }
        });
        transferMarketPanel.add(simulateButton, BorderLayout.SOUTH);

        transferMarketFrame.add(transferMarketPanel);
        transferMarketFrame.setVisible(true);
    }

    public void simulateTransferWindow(JTextArea transferMarketTextArea) {
    // Define transfer window parameters
    int transferWindowHours = 12; // Total transfer window duration (hours)
    int maxTransfersPerHour = 7; // Maximum transfers per hour
    int transferDeadlineHour = 12; // Hour at which the transfer window closes

    // Track transferred players and their destinations
    Map<Player, Team> transferHistory = new HashMap<>();
    Map<Player, Team> freeAgentTransfers = new HashMap<>();
    Map<Team, List<Player>> playersBought = new HashMap<>();
    Map<Team, Double> moneySpent = new HashMap<>();

    for (int hour = 1; hour <= transferWindowHours; hour++) {
        if (hour <= transferDeadlineHour) {
            // Simulate negotiations and transfers within the transfer window
            int negotiationsThisHour = new Random().nextInt(maxTransfersPerHour + 1); // Randomize negotiations per hour

            for (int i = 0; i < negotiationsThisHour; i++) {
                if (!players.isEmpty() && !teams.isEmpty()) {
                    int randomPlayerIndex = new Random().nextInt(players.size());
                    Player playerToTransfer = players.get(randomPlayerIndex);
                    players.remove(randomPlayerIndex);

                    int randomTeamIndex = new Random().nextInt(teams.size());
                    Team destinationTeam = teams.get(randomTeamIndex);

                    if (playerToTransfer.getTransferPrice() <= destinationTeam.getTransferBudget()) {
                        // Simulate negotiation for transfer fee
                        int negotiatedFee = simulateNegotiation(playerToTransfer.getTransferPrice());
                        
                        if (negotiatedFee > 0) {
                            // Transfer fee paid from the destination team's budget
                            destinationTeam.deductTransferBudget(negotiatedFee);

                            // Track transfer history
                            transferHistory.put(playerToTransfer, destinationTeam);

                            // Track money spent by the destination team
                            moneySpent.put(destinationTeam, moneySpent.getOrDefault(destinationTeam, 0.0) + negotiatedFee);

                            // Add the player to the destination team's roster
                            destinationTeam.addPlayer(playerToTransfer);

                            // Add the player to the playersBought map
                            playersBought.computeIfAbsent(destinationTeam, k -> new ArrayList<>()).add(playerToTransfer);

                            // Display the transfer in the log
                            transferMarketTextArea.append("Hour " + hour + ": ");
                            transferMarketTextArea.append(playerToTransfer.getName() + " has transferred to " + destinationTeam.getName()
                                    + " for $" + negotiatedFee + "m\n");
                        } else {
                            // Transfer negotiation failed
                            transferMarketTextArea.append("Hour " + hour + ": ");
                            transferMarketTextArea.append(playerToTransfer.getName() + " failed to negotiate a deal with " + destinationTeam.getName() + "\n");
                        }
                    } else {
                        // Player's transfer fee is too high for the destination team, so it's a free agent transfer
                        freeAgentTransfers.put(playerToTransfer, destinationTeam);

                        // Display the free agent transfer in the log
                        transferMarketTextArea.append("Hour " + hour + ": ");
                        transferMarketTextArea.append(playerToTransfer.getName() + " has transferred as a free agent to " + destinationTeam.getName() + "\n");
                    }
                }
            }
        } else {
            // Transfer window is closed
            transferMarketTextArea.append("Transfer window is closed.\n");

            // End the transfer window when the deadline is reached
            break;
        }

        // Update the transfer market display every hour
        try {
            Thread.sleep(1000); // Sleep for 1 second to simulate the passing of time
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // Display free agent transfers
    transferMarketTextArea.append("\nFree Agent Transfers:\n");
    for (Map.Entry<Player, Team> entry : freeAgentTransfers.entrySet()) {
        Player player = entry.getKey();
        Team destinationTeam = entry.getValue();
        transferMarketTextArea.append(player.getName() + " transferred as a free agent to " + destinationTeam.getName() + "\n");
    }

    // Display money spent by each team
    transferMarketTextArea.append("\nMoney Spent by Teams:\n");
    for (Map.Entry<Team, Double> entry : moneySpent.entrySet()) {
        Team team = entry.getKey();
        double spentAmount = entry.getValue();
        transferMarketTextArea.append(team.getName() + ": $" + spentAmount + "m\n");
    }

    // Display players bought by each team
    transferMarketTextArea.append("\nPlayers Bought by Teams:\n");
    for (Map.Entry<Team, List<Player>> entry : playersBought.entrySet()) {
        Team team = entry.getKey();
        List<Player> boughtPlayers = entry.getValue();
        transferMarketTextArea.append(team.getName() + ":\n");
        for (Player player : boughtPlayers) {
            transferMarketTextArea.append("  " + player.getName() + " (" + player.getPosition() + ") - $" + player.getTransferPrice() + "m\n");
        }
    }

    // At the end of the transfer window, assign remaining players to teams
    for (Player player : players) {
        int randomTeamIndex = new Random().nextInt(teams.size());
        Team destinationTeam = teams.get(randomTeamIndex);
        destinationTeam.addPlayer(player);
    }

    // Update the team list and transfer market display
    updateTeamList();
    displayTransferMarket();
}

private int simulateNegotiation(int initialFee) {
    // Simulate a negotiation for the transfer fee
    int currentFee = initialFee;
    double negotiationFactor = 0.9; // 10% reduction in each round
    int rounds = 0;

    while (rounds < 3) {
        int proposedFee = (int) (currentFee * negotiationFactor);

        // Simulate acceptance probability (adjust as needed)
        double acceptanceProbability = 0.7; // 70% chance of acceptance
        if (Math.random() < acceptanceProbability) {
            // Deal accepted
            return proposedFee;
        }

        // Deal rejected, continue negotiation
        currentFee = proposedFee;
        negotiationFactor -= 0.1; // Reduce the reduction factor in each round
        rounds++;
    }

    // Negotiation failed
    return 0;
}




    public void openViewTeam() {
        // Create a dialog to select a team to view
        String[] teamNames = new String[teams.size()];
        for (int i = 0; i < teams.size(); i++) {
            teamNames[i] = teams.get(i).getName();
        }

        String selectedTeam = (String) JOptionPane.showInputDialog(this, "Select a team to view:", "View Team",
                JOptionPane.QUESTION_MESSAGE, null, teamNames, teamNames[0]);

        if (selectedTeam != null) {
            // Find the selected team and display its information
            for (Team team : teams) {
                if (team.getName().equals(selectedTeam)) {
                    showTeamInfo(team);
                    break;
                }
            }
        }
    }

    public void showTeamInfo(Team team) {
    JFrame teamInfoFrame = new JFrame(team.getName() + " Formation");
    teamInfoFrame.setSize(600, 600);
    teamInfoFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    teamInfoFrame.setLocationRelativeTo(this);

    JPanel teamInfoPanel = new JPanel(new BorderLayout());
    teamInfoFrame.add(teamInfoPanel);

    JLabel formationLabel = new JLabel("Formation: " + team.getFormation());
    formationLabel.setFont(new Font("Arial", Font.BOLD, 20));
    formationLabel.setHorizontalAlignment(SwingConstants.CENTER);
    teamInfoPanel.add(formationLabel, BorderLayout.NORTH);

    JPanel playerInfoPanel = new JPanel(new GridLayout(0, 2, 10, 10)); // 2 columns for player name and rating
    teamInfoPanel.add(playerInfoPanel, BorderLayout.CENTER);

    List<Player> teamRoster = teamRosters.get(team.getName());

    if (teamRoster != null) {
        for (Player player : teamRoster) {
            JLabel playerNameLabel = new JLabel(player.getName());
            playerNameLabel.setFont(new Font("Arial", Font.PLAIN, 16));
            playerNameLabel.setHorizontalAlignment(SwingConstants.LEFT);

            JLabel playerRatingLabel = new JLabel("Rating: " + player.getRating());
            playerRatingLabel.setFont(new Font("Arial", Font.PLAIN, 16));
            playerRatingLabel.setHorizontalAlignment(SwingConstants.RIGHT);

            playerInfoPanel.add(playerNameLabel);
            playerInfoPanel.add(playerRatingLabel);
        }
    }

    teamInfoFrame.setVisible(true);
}

    public void updateTeamList() {
        StringBuilder transferMarketStringBuilder = new StringBuilder();

        for (Team team : teams) {
            transferMarketStringBuilder.append(team.getName()).append(" - Budget: $")
                    .append(team.getTransferBudget()).append("m\n");

            List<Player> teamRoster = teamRosters.get(team.getName());
            if (teamRoster != null) {
                for (Player player : teamRoster) {
                    transferMarketStringBuilder.append("  ").append(player.getName()).append(" (")
                            .append(player.getPosition()).append(")\n");
                }
            }

            transferMarketStringBuilder.append("\n");
        }

        transferMarketTextArea.setText(transferMarketStringBuilder.toString());
    }

    public void displayTransferMarket() {
        updateTeamList();
        repaint();
    }

private static class ManagerCareerPage extends JPanel {
    public ManagerCareerPage() {
        // Your manager career page initialization code here
    }
}
}

class Team {
    private String name;
    private double transferBudget;
    private String formation;
    private List<Player> players;

    public Team(String name, double transferBudget, String formation) {
        this.name = name;
        this.transferBudget = transferBudget;
        this.formation = formation;
        this.players = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public double getTransferBudget() {
        return transferBudget;
    }

    public String getFormation() {
        return formation;
    }

    public List<Player> getPlayers() {
        return players;
    }

    public void addPlayer(Player player) {
        players.add(player);
    }

    public void deductTransferBudget(double amount) {
        transferBudget -= amount;
    }
}

class Player {
    private String name;
    private String position;
    private int transferPrice;
    private int rating;
    private String previousClub;
    private String transferStatus;

    public Player(String name, String position, int transferPrice, int rating, String previousClub) {
        this.name = name;
        this.position = position;
        this.transferPrice = transferPrice;
        this.rating = rating;
        this.previousClub = previousClub;
        this.transferStatus = "Unassigned";
    }

    public String getName() {
        return name;
    }

    public String getPosition() {
        return position;
    }

    public int getTransferPrice() {
        return transferPrice;
    }

    public int getRating() {
        return rating;
    }

    public String getPreviousClub() {
        return previousClub;
    }

    public void setPreviousClub(String previousClub) {
        this.previousClub = previousClub;
    }

    public String getTransferStatus() {
        return transferStatus;
    }

    public void setTransferStatus(String transferStatus) {
        this.transferStatus = transferStatus;
    }
} add a simulate season which will simulate a season and show how players and teams performed