package com.sabirjan.youxi;

import java.util.HashMap;
import java.util.Random;

import com.sabirjan.android.Oyun_1_Activity;
import com.sabirjan.android.R;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MyView extends View{

	
	
	private Paint strPaint;
	private String []strWords={" ﯔﯩﻧﺰﯩﺑ "," ﭖﻪﺘﻛﻪﻣ","ﻙﻪﺑ","ﻰﺸﺧﺎﻳ"};
	private Point []p;
	private Point []psel;
	private Point sel=new Point(-1,-1);
	int select=0;
	int qianmian=-1;
	HashMap hash=new HashMap();
	HashMap hashsel=new HashMap();
	int h=0;
	int w=0;
	int shijidaxiaow=0;
	int shijidaxiaoh=0;
	Bitmap []imgNumber=new Bitmap[10];
	Context con;
	public MyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		chushihua(context);// TODO Auto-generated constructor stub
	}
	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		chushihua(context);// TODO Auto-generated constructor stub
	
	}
	public MyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		chushihua(context);
	}
	
	private void chushihua(Context con)
	{

		this.con=con;
		Typeface type=Typeface.createFromAsset(con.getAssets(),"fonts/ALKATIPTorTom.TTF");
		
		Activity a=(Activity)con;
		
		imgNumber[0]=((BitmapDrawable) this.getResources().getDrawable(R.drawable.n0)).getBitmap();
		imgNumber[1]=((BitmapDrawable) this.getResources().getDrawable(R.drawable.n1)).getBitmap();
		imgNumber[2]=((BitmapDrawable) this.getResources().getDrawable(R.drawable.n2)).getBitmap();
		imgNumber[3]=((BitmapDrawable) this.getResources().getDrawable(R.drawable.n3)).getBitmap();
		imgNumber[4]=((BitmapDrawable) this.getResources().getDrawable(R.drawable.n4)).getBitmap();
		imgNumber[5]=((BitmapDrawable) this.getResources().getDrawable(R.drawable.n5)).getBitmap();
		imgNumber[6]=((BitmapDrawable) this.getResources().getDrawable(R.drawable.n6)).getBitmap();
		imgNumber[7]=((BitmapDrawable) this.getResources().getDrawable(R.drawable.n7)).getBitmap();
		imgNumber[8]=((BitmapDrawable) this.getResources().getDrawable(R.drawable.n8)).getBitmap();
		imgNumber[9]=((BitmapDrawable) this.getResources().getDrawable(R.drawable.n9)).getBitmap();
		strPaint=new Paint();
		strPaint.setTextSize(32);
		strPaint.setTypeface(type);
		int maxtextLen=0;
		
		for(int i=0;i<strWords.length;i++)
		{
			int textWidth =(int)strPaint.measureText(strWords[i]); 
			if(textWidth>maxtextLen)
			{
				maxtextLen=textWidth;
			}
		}
		shijidaxiaow=a.getWindow().getWindowManager().getDefaultDisplay().getWidth();
		w=a.getWindow().getWindowManager().getDefaultDisplay().getWidth()-maxtextLen;
		h=a.getWindowManager().getDefaultDisplay().getHeight()-150;
		shijidaxiaoh=a.getWindowManager().getDefaultDisplay().getHeight();
		strPaint.setColor(Color.WHITE);
		p=new Point[strWords.length];
		psel=new Point[p.length];
		shengchengPoint();
	}
	void shengchengPoint()
	{
		Random r=new Random();
		System.out.println("X="+w+"Y="+h);
		for(int i=0;i<p.length;i++)
		{
			int x=(int)r.nextInt(w);
			
			if(x<=15)
			{
				x=16;
			}
			else if(x>=w-strPaint.measureText(strWords[i]))
			{
				x=w-(int)strPaint.measureText(strWords[i])-5;
			}
			
			int y=r.nextInt(h);
			if(y<=100)
			{
				y=100;
			}
			boolean flg=false;
			for(int j=0;j<p.length;j++)
			{
				if(p[j]!=null)
				{
					int textWidth =(int)strPaint.measureText(strWords[j]); 
					if((x>=p[j].x-20&x<=p[j].x+textWidth+10))
					{
					
						if((y>=p[j].y-40&y<=p[j].y+40))
						{	
							x=(int)r.nextInt(w);
							y=r.nextInt(h);
							flg=false;
							break;
						}
					
					}
				}
				else
				{

					flg=true;
					break;
				}
				flg=true;
			}
			if(flg)
			{
				p[i]=new Point(x,y);
				System.out.println("XPoint="+x+"Ypoint="+y);
			}
			else
			{
				i--;
			}
			
			//p[i].set(r.nextInt(w), r.nextInt(h));
		}
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		
		Paint pcircle=new Paint();
		strPaint.setTextSize(25);
		canvas.drawText("我们学校很好",0,26,strPaint);
		strPaint.setTextSize(32);
		int strw=0;
		int ww=shijidaxiaow-60;
		for(int i=0;i<p.length;i++)
		{
			
		
			if(hash.containsValue(i))
			{
				canvas.drawBitmap(imgNumber[Integer.parseInt(String.valueOf(hashsel.get(i)))],p[i].x-40,p[i].y-35,pcircle);
			//	pcircle.setColor(Color.BLUE);
				//canvas.drawCircle( p[i].x-14, p[i].y-10,13, pcircle);
			//	pcircle.setColor(Color.GREEN);
				//canvas.drawText(String.valueOf(hashsel.get(i)),p[i].x-14,p[i].y-10,pcircle);
				
			}
			else
			{
				pcircle.setColor(Color.RED);
				canvas.drawCircle( p[i].x-11, p[i].y-10,10, pcircle);
			}
			canvas.drawText(strWords[i], p[i].x, p[i].y, strPaint);
		}
		
		String strtext="";
		strPaint.setTextSize(25);
		for(int j=0;j<hash.size();j++)
		{
			int index=Integer.parseInt(hash.get(j).toString());
			ww=ww-(int)strPaint.measureText(strWords[index])-3;
			canvas.drawText(strWords[index],ww,shijidaxiaoh-100, strPaint);
		}
		if(hash.size()==strWords.length)
		{
			//检查对还是错！
			boolean flg=false;
			for(int j=0;j<hash.size();j++)
			{
				int index=Integer.parseInt(hash.get(j).toString());
				if(index!=j)
				{
					flg=true;
				}
			}
			if(flg)
			{
				showErrorGameAlert();
				
			}
			else
			{
				showTrueGameAlert();
				
			}
			
		}
		
		//画出上一个和下一个按钮
		Bitmap  bmp_shangyige= BitmapFactory.decodeResource(getResources(),
                R.drawable.shangyige);//这两个图片要换
		 canvas.drawBitmap(bmp_shangyige,0, shijidaxiaoh-120, pcircle);
			Bitmap  bmp_xiayige= BitmapFactory.decodeResource(getResources(),
	                R.drawable.xiayige);//这两个图片要换
			 canvas.drawBitmap(bmp_xiayige,shijidaxiaow-50, shijidaxiaoh-120, pcircle);
			 
			 Bitmap  refresh= BitmapFactory.decodeResource(getResources(),
		                R.drawable.refresh);//这两个图片要换
				 canvas.drawBitmap(refresh,shijidaxiaow-100, 0, pcircle);
				 Bitmap  exit= BitmapFactory.decodeResource(getResources(),
			                R.drawable.oyun_exit);//这两个图片要换
					 canvas.drawBitmap(exit,shijidaxiaow-50, 0, pcircle);
					  
			 
		super.onDraw(canvas);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		int x=(int) event.getX();
		int y=(int) event.getY();
		System.out.println("X="+x+"Y="+y);
		 
		if(y>=0&y<=30)
		{
		if(x>shijidaxiaow-110&x<shijidaxiaow-80)//Refresh
		{
			shengchengPoint();
			hash.clear();
			hashsel.clear();
			select=0;
			invalidate();
			return super.onTouchEvent(event);
		}
		else if(x>shijidaxiaow-60&x<=shijidaxiaow)//Exit
		{
			try
			{
			Oyun_1_Activity activity=(Oyun_1_Activity)con;
			activity.finish();
			return super.onTouchEvent(event);
			}
			catch(Exception ex)
			{
				super.onTouchEvent(event);
			}
		}
	
		}
		
		for(int i=0;i<p.length;i++)
		{
			int textWidth =(int)strPaint.measureText(strWords[i]); 
			if(x>=p[i].x-10&x<=p[i].x+textWidth)
			{
				if(y>=p[i].y-20&y<=p[i].y+20)
				{
					if(hash.size()==0)
					{
						hashsel.put(i,select+1);
						hash.put(select, i);
						invalidate();
						break;
					}
					if(i==Integer.parseInt(hash.get(select).toString()))
					{
						
						hash.remove(select);
						hashsel.remove(i);
						if(select>0)
						select--;
						invalidate();
						break;
					}
					else if(!hash.containsValue(i))
					{
						select++;
						hash.put(select, i);
						hashsel.put(i,select+1);
					}
					invalidate();
					break;
				}
			}
		}
		
		return super.onTouchEvent(event);
	}

	private void showErrorGameAlert() {
		
		Oyun_1_Activity activity=(Oyun_1_Activity)con;
		final AlertDialog dlg = new AlertDialog.Builder(activity).create();
		dlg.show();
		Window window = dlg.getWindow();
	        // *** 主要就是在这里实现这种效果的.
	        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
		window.setContentView(R.layout.oyun1_hata_dialog);
	        // 为确认按钮添加事件,执行退出应用操作

	 
	        // 关闭alert对话框架
	        ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
	        cancel.setOnClickListener(new View.OnClickListener() {
		  public void onClick(View v) {
			  dlg.cancel();
			 
			}
		  });
	}
	private void showTrueGameAlert() {
		
		Oyun_1_Activity activity=(Oyun_1_Activity)con;
		final AlertDialog dlg = new AlertDialog.Builder(activity).create();
		dlg.show();
		Window window = dlg.getWindow();
	        // *** 主要就是在这里实现这种效果的.
	        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
		window.setContentView(R.layout.oyun1_togra_dialog);
	        // 为确认按钮添加事件,执行退出应用操作

		ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok_togra);
		ok.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				shengchengPoint();
				hash.clear();
				hashsel.clear();
				select=0;
				 dlg.cancel();
					invalidate();
			}
		});
	        // 关闭alert对话框架
	        ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel_togra);
	        cancel.setOnClickListener(new View.OnClickListener() {
		  public void onClick(View v) {
			  dlg.cancel();
			}
		  });
	}

}
