package genericcard;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;

/**
 *
 @author Oran
 */
public class CardPanel extends JPanel implements MouseListener,MouseMotionListener{
    
    CardPile[] pile;          //the card piles on the panel
    int pilenumber;
    MousePile mousePile;         //the card pile being dragged around by the mouse
    CardPile mouseHome;         //a refrence to where the mousePile came from
    
    /** Creates a new instance of CardPanel */
    public CardPanel() {
        super();
        
        addMouseListener(this);
        addMouseMotionListener(this);
        newCardPanel();
        repaint();
    }
    public void newCardPanel(){
        mousePile= new MousePile();
        
        pile = new CardPile[100];
        pilenumber=0;
        CardPile temp;
        temp=new SolitareDeck();
        temp.setLocation(10,5);
        addPile(temp);
        temp=new SolitareHand();
        temp.setLocation(160,5);
        addPile(temp);
        for(int i=0;i<4;i++){
            temp=new SolitareScorePile();
            temp.setLocation(370+i*120,5);
            addPile(temp);
        }
        for(int i=1;i<=7;i++){
            temp=new SolitareStack(pile[0],i);
            temp.setLocation(120*i-100,180);
            temp.mouseClicked(null,null);
            addPile(temp);
        }
    }
    public int getScore(){
        int i=0,n=0;
        
        for(i=2;i<5;i++){
            if(pile[i]!=null)
                n+=pile[i].size();
            else System.out.println("pile["+i+"] is null!");
        }
        return pile[2].size()+pile[3].size()+pile[4].size()+pile[5].size();
    }
    public int getPlayed(){
        int i=0,n=0;
        n=getScore();
        for(i=6;i<13;i++){
            if(pile[i]!=null)
                n+=((SolitareStack)pile[i]).getCardsPlayed();
            else System.out.println("pile["+i+"] is null!");
        }
            
        if(mouseHome!=null && mousePile!=null 
                && !mouseHome.getName().equals("hand"))n+=mousePile.size();
        return n;
    }
    public void addPile(CardPile p){
        if(p!=null){
            pile[pilenumber]=p;
            pilenumber++;
        }
    }
    public void paint(Graphics g){
        super.paint(g);
        //paint the regular piles
        for(int i=0;i<pilenumber;i++){
            pile[i].paint(g);
        }
        //paint the mousepile LAST
        if(!mousePile.isEmpty())
            mousePile.paint(g);
        
        
    }
    //returns the topmost pile containing p; null if none do
    public CardPile pileAt(Point p){
        CardPile r=null;
        for(int i=0;i<pilenumber;i++)
            if(pile[i].contains(p))
                r=pile[i];
        //System.out.println(r.getName());
        return r;
    }
    public void mouseClicked(MouseEvent e){
        if(e.getButton()==MouseEvent.BUTTON1){
            CardPile clickedpile=pileAt(e.getPoint());
            if(clickedpile!=null){
                clickedpile.mouseClicked(e,pile[1]);
                repaint();
            }
            
            repaint();
        }
    }
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){
        if(e.getButton()==MouseEvent.BUTTON1){
            CardPile clickedpile=pileAt(e.getPoint());
            if(clickedpile!=null){
                clickedpile.mousePressed(e,mousePile);
                mouseHome=clickedpile;
            }
            repaint();
        }
    }
    public void mouseReleased(MouseEvent e){
        CardPile releasepile=pileAt(e.getPoint());
        if(releasepile!=null)
            releasepile.mouseReleased(e,mousePile);
        while(!mousePile.isEmpty())
            mouseHome.addCard(mousePile.drawBottom());
        repaint();
    }
    public void mouseDragged(MouseEvent e){
        mousePile.setLocation(e.getPoint());
        if(!mousePile.isEmpty())repaint();
    }
    public void mouseMoved(MouseEvent e){
        mousePile.setLocation(e.getPoint());
        if(!mousePile.isEmpty())repaint();
    }
}



final class MousePile extends CardPile {
    private Point translation;
    public MousePile(){
        super();
        setOverlap(1);
        setOffset(0,25);
        setName("mouse");
        translation =new Point(-50,-20);
    }
    public void setTrans(Point p){translation=(Point)p.clone();}
    public void setTrans(int x,int y){translation.x=x;translation.y=y;}
    public void setLocation(Point p){
        super.setLocation(translation.x+p.x,translation.y+p.y);
    }
    public void setLocation(int x,int y){
        super.setLocation(translation.x+x,translation.y+y);
    }
    
}
Java2html