package com.akjava.lib.common.form.page;

import java.util.HashMap;
import java.util.Map;

import com.akjava.lib.common.tag.Tag;

public class PageMenuHtmlMaker {
	private String pagesClass="menu_pages";
	private String currentClass="menu_current";
	private String pageKey="page";
	private String baseQuery;
	private String allDivClass="menu_all";
	private String pageSpanClass="menu_page";
	private String descriptionDivClass="menu_description";
	//TODO something message
	private String description="$start件目から$end件目を表示しています";
	private String baseUrl="";
	public PageMenuHtmlMaker(String baseUrl,String basicQuery){
		this.baseUrl=baseUrl;
		this.baseQuery=basicQuery;
	}
	
	public String createPageMenuHtml(PagingInfo info){
		Tag allDiv=new Tag("div");
		//Div allDiv=new Div();
		allDiv.setAttribute("class", allDivClass);
		
		
		int start=(info.getCurrentPageNumber()-1)*info.getShowSizeInPage();
		int end=start+info.getShowSizeInPage();
		start++;
		int maxPage=(int) (info.getCountItem()/info.getShowSizeInPage());
		if(info.getCountItem()%info.getShowSizeInPage()>0){
			maxPage++;
		}
		
		String des=description.replace("$start", ""+start);
		des=des.replace("$end", ""+Math.min(end,info.getCountItem()));
		Tag desc=new Tag("div");
		desc.setText(des);
		desc.setAttribute("class", descriptionDivClass);
		
		allDiv.addChild(desc);
		
		Tag numdiv=new Tag("div");
		numdiv.setAttribute("class",pagesClass);
		allDiv.addChild(numdiv);
		
		for(int i=1;i<=maxPage;i++){
			if(i==info.getCurrentPageNumber()){
				Tag span=new Tag("span");
				span.setAttribute("class",currentClass);
				span.setText("["+i+"]");
				numdiv.addChild(span);
			}else{
				Map<String,String> replaced=new HashMap<String, String>();
				replaced.put(pageKey,""+i+"");
				String query=QueryUtils.replace(baseQuery, replaced);
				Tag sp=new Tag("span");
				sp.setAttribute("class",pageSpanClass);
				Tag alink=new Tag("a");
				alink.setAttribute("href", baseUrl+"?"+query);
				
				alink.setText("["+i+"]");
				sp.addChild(alink);
				numdiv.addChild(sp);
			}
		}
		
		
		return allDiv.toString();
	}
}

@@ -535,24 +535,62 @@
 %E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%97%E3%81%A6%E3%81%84%E3%81%BE%E3%81%99%22;%0D%0A
+%09private String empty=%22%E8%A9%B2%E5%BD%93%E3%83%87%E3%83%BC%E3%82%BF%E3%81%AF%E3%81%82%E3%82%8A%E3%81%BE%E3%81%9B%E3%82%93%22;%0D%0A
 %09private Str
@@ -1219,24 +1219,63 @@
 %22%22+start);%0D%0A
+%09%09long countItem=info.getCountItem();%0D%0A
 %09%09des=des.re
@@ -1328,16 +1328,58 @@
 m()));%0D%0A
+%09%09if(countItem==0)%7B%0D%0A%09%09%09des=empty;%0D%0A%09%09%09%7D%0D%0A
 %09%09Tag de

improve page