package app;


import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import app.ui.ResourceManager;

import newEngine.UserInterface;

//import newEngine.UserInterface;


public class MainMenuList{

//	public MyPuzzleCanvas myPuzzleCanvas;
	
	String []itemList;
	Image[] images ;
	public int selectedItem = 0,imageHeight=0;;
	private String title=" ";
	private int noOfItem=0,globalYCord=0;
	int height,width;
	int fH=0,tY=0,xCord=0,yCord=0;
	int rectWidth,rectHeight=40;
//	private Image[] images;
	public int XX=-10,YY=-10;
	private Font font;
	private Image trans;
	private int addIndex=-1;
	private int topAddLocation,bottomAddLocation;
	public MainMenuList(){
		font=Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	}
//	private int xx,yy;
	public void CreateList(String t, String[] items,Image[] images,int wid,int hei,int addIndex)
	{
		title=t;
		this.images=images;
		itemList=items;
		width = wid;
		height = hei;
		this.addIndex=addIndex;
		rectWidth=(wid-50);
		try{	
		
		
		}
		catch(Exception e){
			System.out.println("exception found");
		}
			
		imageHeight=rectHeight+10;
			xCord=height/10;
		imageHeight=imageHeight+2;
		noOfItem=(height-xCord)/imageHeight;
		noOfItem=noOfItem-1;
		selectedItem=-1;
		yCord=0;//+imageHeight
		if(addIndex==-1){
			globalYCord=(height/2)-((imageHeight*(itemList.length))/2);
		}else if(addIndex>=0){
			String[] temp=new String[itemList.length+1];
			Image[] tempImage=new Image[itemList.length+1];
			for(int i=0;i<temp.length;i++){
				if(i<addIndex){
					temp[i]=itemList[i];
					tempImage[i]=images[i];
				}else if(i==addIndex){
					temp[addIndex]="------";
//					tempImage[addIndex]=null;
				}else if(i>addIndex){
					temp[i]=itemList[i-1];
					tempImage[i]=images[i-1];
				}
			}
			itemList=new String[temp.length];
			images=new Image[tempImage.length]; 
			this.itemList=temp;
			this.images=tempImage;
			globalYCord=(height/2)-((imageHeight*(itemList.length))/2);
		}
		trans=ImageLoader.scaleImageFast(ImageLoader.trans, rectWidth+50, ((imageHeight*(itemList.length))+40));
		if(globalYCord<xCord){
			globalYCord=xCord;
		}
		tY=0;
		XX=-10;
		YY=-10;
//		newYCord=0;
		//ImageLoader.selection1=null;
		//ImageLoader.selection2=null;
	}
private void drawHeader(Graphics g,String text){
		
		g.setColor(255,255,255);
		
		g.setColor(255,255,255);
		g.setFont(font);
		
	}
	public void paint(Graphics g){
		g.setFont(font);
			    topAddLocation=height/2-trans.getHeight()/2-UserInterface.getTopAdsImageHeight();
			    bottomAddLocation=height/2+trans.getHeight()/2+UserInterface.getBottomAdsImageHeight();
				UserInterface.setCordinates(width, height,topAddLocation ,bottomAddLocation);
				g.setClip(yCord, xCord, this.width,this.height);
				g.drawImage(trans, width/2,height/2, Graphics.HCENTER|Graphics.VCENTER);
				selectedItem=-1;
				for(int i=0;i<itemList.length;i++){
					g.setFont(font);
					int tx=width/2-(rectWidth/2);
					int ty=globalYCord+(imageHeight*i);
					int tH=rectHeight/3;
					if(XX>=tx && XX<tx+rectWidth && YY>=ty && YY<ty+rectHeight){
						
						selectedItem=i;
						g.setColor(255,255,255);
						g.setFont(font);
							String s="";
							s=itemList[i];
							g.setColor(255,165,0);
							g.fillRoundRect(tx, ty, rectWidth, rectHeight,10,10);
//							g.drawImage(sele,tx,ty, Graphics.TOP|Graphics.LEFT);
							if(i!=addIndex)
								g.drawImage(images[i],tx+8,ty+tH-8, Graphics.TOP|Graphics.LEFT);
							
							if(s.equals(UserInterface.getFreeAppButtonText()))
								g.setColor(0,0,255);
							else
								g.setColor(0);
							if(i==addIndex){
								if(UserInterface.getMiddleAdsImage()!=null){
									UserInterface.scaleMiddleAds(rectWidth, rectHeight);
									g.drawImage(UserInterface.getMiddleAdsImage(), width/2, ty+tH+5, Graphics.HCENTER|Graphics.VCENTER);
									}
								else if(UserInterface.getMiddleAdsString()!=null)
									g.drawString(UserInterface.getMiddleAdsString(),tx+(rectWidth/2),ty+tH-2,Graphics.TOP|Graphics.HCENTER);
							}
							else{ 
								g.drawString(s,tx+(rectWidth/2),ty+tH-2,Graphics.TOP|Graphics.HCENTER);
							}
						
//							g.drawString(itemList[i],tx+(sele1.getWidth()/2),ty+tH,Graphics.TOP|Graphics.HCENTER);
					}
					else{
						g.setColor(255,255,255);
						g.setFont(font);
						String s="";
						s=itemList[i];
						g.setColor(5,179,255);
						g.fillRoundRect(tx, ty, rectWidth, rectHeight,10,10);
//						g.drawImage(unsele,tx,ty, Graphics.TOP|Graphics.LEFT);
						if(i!=addIndex)
							g.drawImage(images[i],tx+8,ty+tH-8, Graphics.TOP|Graphics.LEFT);
						if(s.equals(UserInterface.getFreeAppButtonText()))
							g.setColor(0,0,255);
						else
							g.setColor(0);
						if(i==addIndex){
							if(UserInterface.getMiddleAdsImage()!=null){
								UserInterface.scaleMiddleAds(rectWidth, rectHeight);
								g.drawImage(UserInterface.getMiddleAdsImage(), width/2, ty+tH+5, Graphics.HCENTER|Graphics.VCENTER);
							}
							else if(UserInterface.getMiddleAdsString()!=null)
								g.drawString(UserInterface.getMiddleAdsString(),tx+(rectWidth/2),ty+tH-2,Graphics.TOP|Graphics.HCENTER);
						}
						else
							g.drawString(s,tx+(rectWidth/2),ty+tH-2,Graphics.TOP|Graphics.HCENTER);
						
					}
					
	
					
				}
				if(YY>topAddLocation-5 && YY< topAddLocation+UserInterface.getTopAdsImageHeight()){
					UserInterface.isTopAdsSelected(true);
					UserInterface.isBottomAdsSelected(false);
				}
				else if(YY> bottomAddLocation-UserInterface.getBottomAdsImageHeight()-5 && YY<bottomAddLocation){
					
					UserInterface.isTopAdsSelected(false);
					UserInterface.isBottomAdsSelected(true);
				}
				//Footer
				g.setFont(font);
				g.setClip(0, 0, width,height);
//			}
	}
	public void drawCommand(Graphics g,String left_cmd,String right_cmd){
		g.setColor(0);
		Font font=MainMenu.font.getDefaultFont();
		if(left_cmd.length()!=0)
			g.drawString(left_cmd, width/4-font.stringWidth(left_cmd)/2, height-20-font.getHeight()/2, 0);
		if(right_cmd.length()!=0)
			g.drawString(right_cmd, (width*3)/4-font.stringWidth(right_cmd)/2, height-20-font.getHeight()/2, 0);
	}

}
	