package org.zhios.website.admin.model;

import java.awt.List;
import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PackageTree {
	private String PackagePath;
	private ConfFileOpt PackageConf;
	
	public PackageTree(){
		PackagePath=SysConfig.RootPath+"/package";
		PackageConf=new ConfFileOpt(PackagePath+"/package.xml");
	}
	
	public class ChildFolderList{
		public int foldercount;
		public List folderlist;

		public ChildFolderList(){
			foldercount=0;
			folderlist=new List();
		}
	}
	
	public String getPackageTree(){
		String packagetree=setFolderStr();
		
		return packagetree;
	}

	//Set all child folder and file of the folder into DIV style.
	//return: the pagetree html string of folder and file list.
	public String setFolderStr(){
		String result="";
		
		ChildFolderList childlist=new ChildFolderList();
		childlist=readChildList(PackagePath);
		
		int foldercount=childlist.foldercount;
		String [] foldername=new String[foldercount];
		String [] showname=new String[foldercount];
		for(int i=0;i<foldercount;i++){
			foldername[i]=childlist.folderlist.getItem(i);
		}

		showname=PackageConf.getNodesName(foldername);
		if(showname.equals(null)) showname=foldername;

		for(int i=0;i<foldercount;i++){
			result+="<div id='"+foldername[i]+"' style='margin-left:10px'>" +
					"<img id='"+foldername[i]+"_img' src='img/ptfolder.gif' alt=''>" +
					"<a href='javascript:PackageLink(\""+foldername[i]+"\")' class='pagetree'>"+showname[i]+"</a></div>";
		}
		
		return result;
	}

	//Get files and folders in special path.
	//Path: the path.
	//return: files and folders list.
	public ChildFolderList readChildList(String Path){
		File myPath = new File(Path);
		
		ChildFolderList childlist=new ChildFolderList();
		String tempstr;
		if(myPath.isDirectory()){ 
			File[] tempList=myPath.listFiles();
			for (int i=0;i<tempList.length;i++) {
				if (tempList[i].isDirectory()) {
					tempstr=tempList[i].getName();
					if(!tempstr.equals("css")&&!tempstr.equals("package")){
						childlist.folderlist.add(tempstr);
						childlist.foldercount++;
					}
				}
			}
		}
		
		return childlist;
	}

	//Add a folder to special path.
	//foldername: folder name.
	//return foldername of package
	public String addFolder(String packagename){
		if(packagename==null) return "";
		
        SimpleDateFormat sdf=new SimpleDateFormat("yyMMdd");
        String datestr=sdf.format(new Date());
        DecimalFormat df = new DecimalFormat("00");
        for(int i=1;i<100;i++){
        	String foldername=datestr+df.format(i);
    		String destpath=PackagePath+"/"+foldername;
			File destfile=new File(destpath);
        	if(!destfile.exists()){
				destfile.mkdir();

				if(PackageConf.addNode("main", "package", foldername, packagename))
					return foldername;
        	}
        }
		
		return "";
	}

	//Add a folder to special path.
	//foldername: folder name.
	//packagename: package name of news.
	public boolean uptFolder(String packagepath,String packagename){
		if((packagepath==null)||(packagename==null)) return false;

		return PackageConf.updateNodeName(packagepath,packagename);
	}

	//Delete a folder or a file.
	//destpath: the path of being delete.
	public boolean delFolder(String packagepath){
		if(packagepath==null) return false;
		
		String destpath=PackagePath+"/"+packagepath;
		
		File destfile=new File(destpath);
		if(destfile.exists()){
			if(destfile.delete())
				return PackageConf.deleteNodeById(packagepath);
		}
		
		return false;
	}
}

