import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class SpiderSolitia extends Applet implements MouseListener,MouseMotionListener,Runnable{
	private Dimension d;
	private Image offscreen;
	private Graphics grf;
	private MediaTracker mt;
	private Field field;
	private Color bgcolor;
	private Color areacolor;
	private Color mojicolor;
	private Image [] card = new Image[13];
	private Image uracard;
	private Image startbutton;
	private Image restartbutton;
	private AudioClip au1;
	private Thread thread = null;
	private Thread thread2 = null;
	private Thread thread3 = null;
	private Thread thread4 = null;
	private boolean start;
	private boolean push;
	private int mouse_x;
	private int mouse_y;
	private int xx[] = new int[10];
	public SpiderSolitia(){
		super();
		addMouseListener(this);
		addMouseMotionListener(this);
		start = false;
		push = false;
	}
	public void init(){
		d = getSize();
		offscreen = createImage(d.width,d.height);
		grf = offscreen.getGraphics();
		field = new Field(this);
		mt = new MediaTracker(this);
		for(int i = 1;i < 14;i++){
			if(i >= 10){
				card[i - 1] = getImage(getCodeBase(),i+".png");
				mt.addImage(card[i - 1],0);
			}else{
				card[i - 1] = getImage(getCodeBase(),"0"+i+".png");
				mt.addImage(card[i - 1],0);
			}
		}
		uracard = getImage(getCodeBase(),"ura.png");
		mt.addImage(uracard,0);
		startbutton = getImage(getCodeBase(),"start_button.png");
		mt.addImage(startbutton,0);
		restartbutton = getImage(getCodeBase(),"restart_button.png");
		mt.addImage(restartbutton,0);
		au1 = getAudioClip(getDocumentBase(),"shu.wav");
		bgcolor = new Color(208,255,208);
		areacolor = new Color(55,255,55);
		mojicolor = new Color(0,0,0);
		for(int i = 0;i < 10;i++){
			xx[i] = 15 + 55 * i;
		}
	}
	public void paint(Graphics g){
		update(g);
	}
	public void update(Graphics g){
		if(!mt.checkID(0)){
			g.clearRect(0,0,d.width,d.height);
			g.setColor(Color.black);
			g.drawString("Please wait....",d.width / 4,d.height / 2);
			return;
		}
		grf.setColor(bgcolor);
		grf.fillRect(0,0,d.width,d.height);
		grf.setColor(mojicolor);
		grf.setFont(new Font("Monospaced",Font.BOLD,24));
		grf.drawString("スパイダソリティア",180,30);
		grf.setFont(new Font("Monospaced",Font.BOLD,14));
		grf.drawString("タイム(秒)"+field.getTime(),430,40);
		for(int i = 0;i < 10;i++){
			grf.setColor(Color.RED);
			grf.fillRect(xx[i],50,50,70);
			grf.setColor(bgcolor);
			grf.fillRect(xx[i]+2,52,46,66);
			if(start){
				CardRow cr = field.getCardRow(i);
				for(int j = 0;j < cr.getSize();j++){
					FieldCard fc = cr.getCard(j);
					int x = fc.getIndex_x();
					int y = fc.getIndex_y();
					boolean front = fc.getFrontstate();
					int su = fc.getCard().getNumber() - 1;
					if(front){
						grf.drawImage(card[su],x,y,this);
					}else{
						grf.drawImage(uracard,x,y,this);
					}
				}
			}
		}
		if(push){
			FieldCard [] fc = field.getMoveFieldCard();
			int fc_cnt = field.getMoveFieldCardLength();
			for(int i = 0;i < fc_cnt;i++){
				int x = fc[i].getIndex_x();
				int y = fc[i].getIndex_y();
				boolean front = fc[i].getFrontstate();
				int su = fc[i].getCard().getNumber() - 1;
				if(front){
					grf.drawImage(card[su],x,y,this);
				}else{
					grf.drawImage(uracard,x,y,this);
				}
			}
		}
		if(!start){
			grf.drawImage(startbutton,401,515,this);
		}else{
			grf.drawImage(restartbutton,401,515,this);
		}
		int compsu = field.getCs().getCompleteCount();
		for(int i = 0;i < compsu;i++){
			grf.drawImage(card[12],15+15*i,485,this);
		}
		if(compsu == 8){
			grf.setColor(Color.RED);
			grf.setFont(new Font("Monospaced",Font.BOLD,18));
			grf.drawString("ＧＡＭＥ　ＣＬＥＡＲ",190,300);
		}
		grf.setColor(areacolor);
		grf.fillRect(227,485,120,80);
		grf.setColor(mojicolor);
		grf.setFont(new Font("Monospaced",Font.BOLD,14));
		grf.drawString("得点 ： "+field.getTokuten(),227,520);
		grf.drawString("回数 ： "+field.getKaisu(),227,540);
		AddCardSet acs = field.getAcs();
		for(int i = 0;i < 5;i++){
			if(!acs.getAddCardRow(i).getEnd()){
				grf.drawImage(uracard,510,475+5*i,this);
			}
		}
		g.drawImage(offscreen,0,0,this);
	}
	public void start(){
		if(thread == null){
			thread = new Thread(this);
			thread.start();
		}
	}
	public void stop(){
		if(thread != null){
			thread.stop();
			thread = null;
		}
	}
	public void run(){
		try{
			mt.waitForID(0);
		}catch(InterruptedException e){
			return;
		}
		repaint();
	}
	public void mouseClicked(MouseEvent arg0) {
		int ix = arg0.getX();
		int iy = arg0.getY();
		if(start){
			if(thread3 == null && thread4 == null){
				field.addCardSet(ix,iy);
			}
		}
		if(ix >= 401 && ix < 456 && iy >= 516 && iy < 541){
			start = true;
			init();
			timeThreadStart();
			repaint();
		}
		repaint();
	}
	public void timeThreadStart() {
		TimerCount tc = new TimerCount(this,field);
		thread2 = new Thread(tc);
		thread2.start();
	}
	public void addThreadStart(FieldCard [] fc){
		AddAnimation aa = new AddAnimation(this,field,fc);
		thread3 = new Thread(aa);
		thread3.start();
	}
	public void addThreadStop(){
		thread3 = null;
	}
	public void completeThreadStart(int x,FieldCard [] fc){
		CompleteAnimation ca = new CompleteAnimation(this,field,fc,x);
		thread4 = new Thread(ca);
		thread4.start();
	}
	public void completeThreadStop(){
		thread4 = null;
	}
	public void mousePressed(MouseEvent arg0) {
		if(start){
			if(thread3 == null && thread4 == null){
				int ix = arg0.getX();
				int iy = arg0.getY();
				field.movePrepare(ix,iy);
			}
		}
	}
	public int getXX(int n){
		return xx[n];
	}
	public boolean getPush(){
		return push;
	}
	public void setPush(boolean push){
		this.push = push;
	}
	public void setMouse_x(int mouse_x){
		this.mouse_x = mouse_x;
	}
	public void setMouse_y(int mouse_y){
		this.mouse_y = mouse_y;
	}
	public void mouseReleased(MouseEvent arg0) {
		if(push){
			int ix = arg0.getX();
			int iy = arg0.getY();
			for(int i = 0;i < 10;i++){
				System.out.print(field.getCardRow(i).getSize() + ",");
			}
			System.out.println("");
			boolean onsound = field.movemain(ix,iy);
			for(int i = 0;i < 10;i++){
				System.out.print(field.getCardRow(i).getSize() + ",");
			}
			System.out.println("");
			if(onsound){
				musicPlay();
			}
			repaint();
		}
	}
	public void mouseEntered(MouseEvent arg0) {
	}
	public void mouseExited(MouseEvent arg0) {
	}
	public void mouseDragged(MouseEvent arg0) {
		int ix = arg0.getX();
		int iy = arg0.getY();
		if(push){
			field.moveIndex(ix,iy,mouse_x,mouse_y);
			repaint();
		}
	}
	public void mouseMoved(MouseEvent arg0) {
	}
	public void musicPlay(){
		au1.play();
	}
}
