// Copyright Sven-Olof Nystrom Uppsala University 2025

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

class SimpleGame extends JPanel implements MouseListener, MouseMotionListener {

    private ArrayList<Pile> piles = new ArrayList<Pile>();

    private Pile stockPile;
    private ThrowPile throwPile;
    private Pile redPile;
    private Pile otherPile;
    private Pile upPile;
    
    static final int X_SPACE = Card.WIDTH + 10;
    static final int Y_SPACE = Card.HEIGHT + 10;
    static final int TAB = 10;
    
    private Card selected;
    private Pile source;
    private int xOffset;
    private int yOffset;

    
    public SimpleGame () {

        throwPile = new ThrowPile(X_SPACE*2, Y_SPACE);
        stockPile = new StockPile(X_SPACE*1, Y_SPACE, throwPile);
        redPile =   new RedPile(X_SPACE*1, Y_SPACE*2);
        otherPile = new OtherPile(X_SPACE*2, Y_SPACE*2);
        upPile =    new UpPile(X_SPACE*2, Y_SPACE*3);
        
        piles.add(stockPile);
        piles.add(throwPile);
        piles.add(redPile);
        piles.add(otherPile);
        piles.add(upPile);
        
        Deck d = new Deck ();
        d.shuffle();
        for (Card c : d.cardSet()) {
            stockPile.add(c);
        }
    }
            
    public void paintComponent (Graphics g) {
        super.paintComponent(g);

        g.setColor( Color.GREEN );
        
        g.fillRect( 0, 0, getWidth(), getHeight() );

        for (Pile p : piles) {
            p.draw(g);
        }

        if (selected != null){
            selected.draw(g);
        }
    }
    
    public void mouseClicked (MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        for (Pile p : piles) {
            if (p.contains(x,y)){
                p.click();
                repaint();
                return;
            }
        }                
    }
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}

    public void mousePressed(MouseEvent e){
        int x = e.getX();
        int y = e.getY();

        for (Pile p : piles) {
            if (p.contains(x,y)
                && !p.isEmpty()
                && p.canRelease()) {
                source = p;
                selected=p.pop();
                xOffset = x - selected.getX();
                yOffset = y - selected.getY();
                
                return; // only select card from one pile
            }
        }

    }
    public void mouseReleased(MouseEvent e){
        int x = e.getX();
        int y = e.getY();

        if (selected!=null){
            for (Pile p : piles) {
                if (p.contains(x,y)) {
                    
                    if ( p.canAccept(selected)) {
                        p.add(selected);
                    }
                    else {
                        source.add(selected);
                        // Move failed. Put the card back.
                    }
                    selected=null;
                    repaint();
                    return;
                }

            }
            source.add(selected); // Move failed.
            repaint();
        }
    }

    public void mouseDragged(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();

        if (selected!=null) {
            selected.move(x-xOffset, y-yOffset);
            repaint();
        }
    }

    public void mouseMoved(MouseEvent e) {
    }     
    
    static public void main(String[] args) { 
        SimpleGame p = new SimpleGame();
        JFrame f = new JFrame();

        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(new ControlPanel(), BorderLayout.NORTH);
        f.getContentPane().add(p, BorderLayout.CENTER);

        p.addMouseListener(p);
        p.addMouseMotionListener(p);
        
	f.setBounds(100, 100, 800, 500);
        
        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
        f.setVisible(true);

    }
}
