package org.zhios.website.admin.model;

import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class PageTree {
	public String NewPagePath;
	
	public PageTree(){
		NewPagePath=SysConfig.SettingPath+"model/"+SysConfig.PageModel;
	}
	
	public class ChildList{
		public int foldercount;
		public List folderlist;
		public int filecount;
		public List filelist;

		public ChildList(){
			foldercount=0;
			folderlist=new List();
			filecount=0;
			filelist=new List();
		}
	}
	
	public String getPageTree(){
		String pagetree=setFolderStr("",0);
		
		return pagetree;
	}

	//Set all child folder and file of the folder into DIV style.
	//relativepath: path related of RootPath.
	//nodelevel: the level of node in pagetree. 
	//return: the pagetree html string of folder and file list.
	public String setFolderStr(String relativepath,int nodelevel){
		String result="";
		String folderpath=SysConfig.RootPath+"/"+relativepath;
		
		ChildList childlist=new ChildList();
		childlist=readChildList(folderpath);

		int lfile=childlist.filecount;
		for(int i=0;i<lfile;i++){
			String filename=childlist.filelist.getItem(i);
			String fileid=relativepath.isEmpty()?filename:(relativepath+"/"+filename);
			result+="<div id='"+fileid+"' style='margin-left:10px'><img id='"+fileid+"_img' src='img/ptpage.gif' alt=''>" +
					"<a href='javascript:PathLink(\""+fileid+"\")' class='pagetree'>"+filename+"</a></div>";
		}

		int foldercount=childlist.foldercount;
		for(int i=0;i<foldercount;i++){
			String foldername=childlist.folderlist.getItem(i);
			String folderid=relativepath.isEmpty()?foldername:(relativepath+"/"+foldername);
			result+="<div id='"+folderid+"' style='margin-left:10px;overflow:hidden'><div id='"+folderid+"_bar'>" +
					"<img id='"+folderid+"_img' src='img/ptfolderopen.gif' alt=''>" +
					"<a href='javascript:PathLink(\""+folderid+"\")' ondblclick='javascript:FolderView(\""+folderid+"\")' class='pagetree'>"+foldername+"</a></div>";
			result+=setFolderStr(folderid,++nodelevel);
			nodelevel--;
			result+="</div>";
		}
		
		return result;
	}

	//Get files and folders in special path.
	//Path: the path.
	//return: files and folders list.
	public ChildList readChildList(String Path){
		File myPath = new File(Path);
		
		ChildList childlist=new ChildList();
		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++;
					}
				}
				else {
					childlist.filelist.add(tempList[i].getName());
					childlist.filecount++;
				}
			}
		}
		
		return childlist;
	}
	

	//Add an page file to special path.
	//relativepath: the relative path of page file with root path.
	//filename: page file name.
	public boolean addPage(String relativepath, String filename){
		String destpath;
		if(relativepath.isEmpty()) destpath=SysConfig.RootPath+"/"+filename;
		else destpath=SysConfig.RootPath+"/"+relativepath+"/"+filename;
		
		try {
			File destfile=new File(destpath);
			if(!destfile.exists()){
				destfile.createNewFile();

				OutputStreamWriter owriter = new OutputStreamWriter(new FileOutputStream(destfile),"UTF-8");
				BufferedWriter buffwriter=new BufferedWriter(owriter); 
				
				File newpagemodel=new File(NewPagePath);
				InputStreamReader ireader = new InputStreamReader(new FileInputStream(newpagemodel),"UTF-8"); 
			    BufferedReader buffreader=new BufferedReader(ireader);
			    
			    String tempstr;
			    while((tempstr=buffreader.readLine())!=null){
			    	tempstr+="\r\n";
			    	buffwriter.write(tempstr);
			    }
			    buffwriter.flush();

			    buffreader.close();
			    ireader.close();
			    buffwriter.close();
			    owriter.close();
			}
			else{return false;}
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	//Add a folder to special path.
	//relativepath: the relative path of page file with root path.
	//foldername: folder name.
	public boolean addFolder(String relativepath, String foldername){
		String destpath;
		if(relativepath.isEmpty()) destpath=SysConfig.RootPath+"/"+foldername;
		else destpath=SysConfig.RootPath+"/"+relativepath+"/"+foldername;
		
		try {
			File destfile=new File(destpath);
			if(!destfile.exists()){
				destfile.mkdir();
			}
			else{return false;}
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	//Delete a folder or a file.
	//destpath: the path of being delete.
	public boolean DeletePath(String deletepath){
		String destpath=SysConfig.RootPath+"/"+deletepath;
		
		boolean result=false;
		try {
			File destfile=new File(destpath);
			if(destfile.exists()){
				result=destfile.delete();
				
				/*
				//delete record of package in this page.
				(new ConfFileOpt(SysConfig.RootPath+"/package/package.xml")).deleteNodeByName(deletepath);
				*/
			}
			else{return false;}
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return result;
	}

	//Delete a folder or a file.
	//oldpath: the path of being update.
	//newname: the new name after update.
	public boolean UpdatePath(String oldpath, String newname){
		boolean result=false;
		try {
			File oldfile=new File(SysConfig.RootPath+"/"+oldpath);
			if(oldfile.exists()){
				File newfile=new File(oldfile.getParent()+"/"+newname);
				result=oldfile.renameTo(newfile);

				/*
				//delete record of package in this page.
				(new ConfFileOpt(SysConfig.RootPath+"/package/package.xml")).deleteNodeByName(deletepath);
				*/
			}
			else{return false;}
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return result;
	}
}

