package com.lerx.v6.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.support.ResourceBundleMessageSource;

import com.lerx.analyze.util.AnalyzeUtil;
import com.lerx.dao.iface.IAlbumDao;
import com.lerx.dao.iface.IAlbumPartnerDao;
import com.lerx.entities.Album;
import com.lerx.entities.AlbumPartner;
import com.lerx.portal.obj.FileEl;
import com.lerx.portal.obj.SimpleAlbum;
import com.lerx.sys.util.FileUtil;
import com.lerx.sys.util.HttpUtil;

public class AlbumUtil {

	public static SimpleAlbum cover(ResourceBundleMessageSource messageSource,HttpServletRequest request,Album album,boolean cp) {
		SimpleAlbum salbum=new SimpleAlbum();
		salbum.setGenre(album.getGenre().getName());
		salbum.setId(album.getId());
		salbum.setLeader(album.getLeader().getUsername());
		salbum.setUid(album.getLeader().getId());
		salbum.setName(album.getName());
		String href=getHref(messageSource,request,album,cp);
		href=HttpUtil.contextPathRebulid(href, request);
		salbum.setHref(href);
		salbum.setGenreHref(AlbgenreUtil.getHref(messageSource, request, album.getGenre(), cp));
		salbum.setLogo(album.getLogo());
		salbum.setTxt1(album.getTxt1());
		salbum.setTxt2(album.getTxt2());
		salbum.setAvatar(album.getLeader().getAvatarUrl());
		
		return salbum;
	}
	
	public static String getHref(ResourceBundleMessageSource messageSource,HttpServletRequest request,Album album,boolean cp) {
		String rootPath=messageSource.getMessage("album.file.static.root", null, "html/album", null);
		String indexFile=messageSource.getMessage("file.html.default", null, "index.html", null);
		String href=rootPath+"/"+album.getId()+"/"+indexFile;
		String realFile = FileUtil.appPath() + href;
		realFile=FileUtil.repairFilePath(realFile);
		if (realFile.substring(0,1).equals("\\")) {
			realFile=realFile.substring(1, realFile.length());
		}
		File f = new File(realFile);
		if (!f.exists()) {
			href="/show_album/index/"+album.getId();
		}
		if (cp) {
			href=HttpUtil.getSrvUrl(request, false)+"/"+request.getContextPath()+"/"+href;
		}else {
			href=request.getContextPath()+"/"+href;
		}
		href=HttpUtil.repairUrl(href);
		return href;
	}
	
	
	public static String hrefAnalyze(String html, Album album,ResourceBundleMessageSource messageSource,HttpServletRequest request,boolean cp) {
		String rootPath=messageSource.getMessage("album.file.static.root", null, "html/album", null);
		String indexFile=messageSource.getMessage("file.html.default", null, "index.html", null);
		String href=rootPath+"/"+album.getId()+"/"+indexFile;
		String realFile = FileUtil.appPath() + href;
		realFile=FileUtil.repairFilePath(realFile);
		if (realFile.substring(0,1).equals("\\")) {
			realFile=realFile.substring(1, realFile.length());
		}
		File f = new File(realFile);
		if (!f.exists()) {
			href="/show_album/index/"+album.getId();
		}
		if (cp) {
			href=HttpUtil.getSrvUrl(request, false)+"/"+request.getContextPath()+"/"+href;
		}else {
			href=request.getContextPath()+"/"+href;
		}
		href=HttpUtil.repairUrl(href);
		html=AnalyzeUtil.replace(html, "tag", "albhref", href);
		html=AnalyzeUtil.replace(html, "tag", "href", href);
		return html;
	}
	
	public static String fmt(String html, Album album) {
		html=AnalyzeUtil.replace(html, "tag", "albid", ""+album.getId());
		html=AnalyzeUtil.replace(html, "tag", "albname", album.getName());
		html=AnalyzeUtil.replace(html, "tag", "name", album.getName());
		html=AnalyzeUtil.replace(html, "tag", "gname", album.getGenre().getName());
		html=AnalyzeUtil.replace(html, "tag", "gid", ""+album.getGenre().getId());
		html=AnalyzeUtil.replace(html, "tag", "txt1", album.getTxt1());
		html=AnalyzeUtil.replace(html, "tag", "txt2", album.getTxt2());
		html=AnalyzeUtil.replace(html, "tag", "logo", album.getLogo());
		html=AnalyzeUtil.replace(html, "tag", "banner", album.getBanner());
		html=AnalyzeUtil.replace(html, "tag", "description", album.getDescription());
		html=AnalyzeUtil.replace(html, "tag", "leader", album.getLeader().getUsername());
		html=AnalyzeUtil.replace(html, "tag", "uid", ""+album.getLeader().getId());
		html=AnalyzeUtil.replace(html, "tag", "htmlOwn", album.getHtmlOwn());
		return html;
	}
	
	
	/*
	 * 构造静态文件
	 */
	public static FileEl feBuild(ResourceBundleMessageSource messageSource, Album album) {
		FileEl fe = new FileEl();
		String fmtStr = messageSource.getMessage("album.file.static.root", null, "html/album", null);
		String file;
		file = fmtStr + "/" +album.getId();
		
		String indexFile=messageSource.getMessage("file.html.default", null, "index.html", null);
		
		file += "/" +indexFile;

		String url=FileUtil.repairUrl(file);
		String realFile = FileUtil.appPath() + file;
		realFile=FileUtil.repairFilePath(realFile);
		fe.setUrl(url);
		fe.setRealPath(realFile);

		return fe;
	}
	
	
	public static void htmlCreate(Album album, ResourceBundleMessageSource messageSource,
			 String contextPath,boolean genreBuild) {
		// 更新专辑首页
		String strCreateDateTime = messageSource.getMessage("msg.datetime.html.static.create", null, "", null);
		FileEl fe = feBuild(messageSource, album);
		String charset = messageSource.getMessage("charset", null, "utf-8", null);
		String url = contextPath+"/show_album/index/"+album.getId();
		url=HttpUtil.repairUrl(url);
		FileUtil.htmlBySniff(url, fe.getRealPath(), strCreateDateTime, charset);
		//更新频道首页
		if (genreBuild) {
			AlbgenreUtil.htmlCreate(album.getGenre(), messageSource, contextPath);
		}
		
		
	}
	
	public static Album validate(ResourceBundleMessageSource messageSource,Album album) {
		String charset=messageSource.getMessage("charset", null, "UTF-8", null);
		String filterWords=ConfigUtil.configContentsByComma("filterWords", charset);
		String repTarget="";
		boolean rep=false;
		if (messageSource.getMessage("filter.replace", null, "true", null).trim().equals("true")) {
			rep=true;
			repTarget=messageSource.getMessage("filter.replace.target", null, "*", null);
		}
		album.setBanner(SysUtil.validateAss(album.getBanner(), filterWords, rep, repTarget,true));
		album.setBannerSmall(SysUtil.validateAss(album.getBannerSmall(), filterWords, rep, repTarget,true));
		album.setDescription(SysUtil.validateAss(album.getDescription(), filterWords, rep, repTarget,true));
		album.setLogo(SysUtil.validateAss(album.getLogo(), filterWords, rep, repTarget,true));
		album.setLogo8831(SysUtil.validateAss(album.getLogo8831(), filterWords, rep, repTarget,true));
		album.setLogoSmall(SysUtil.validateAss(album.getLogoSmall(), filterWords, rep, repTarget,true));
		album.setMail(SysUtil.validateAss(album.getMail(), filterWords, rep, repTarget,true));
		album.setName(SysUtil.validateAss(album.getName(), filterWords, rep, repTarget,true));
		album.setTxt1(SysUtil.validateAss(album.getTxt1(), filterWords, rep, repTarget,true));
		album.setTxt2(SysUtil.validateAss(album.getTxt2(), filterWords, rep, repTarget,true));
		return album;
	}
	
	public static List<SimpleAlbum> allByUid(long uid,long loginID,IAlbumDao albumDaoImpl,IAlbumPartnerDao albumPartnerDaoImpl,ResourceBundleMessageSource messageSource,HttpServletRequest request){
		Set<Album> albums=new HashSet<Album>();
		List<Album> list=albumDaoImpl.findByLeaderID(uid, 0L, 1);
		for (Album a:list) {
			albums.add(a);
		}
		List<AlbumPartner> list2=albumPartnerDaoImpl.findByUid(uid);
		for (AlbumPartner a:list2) {
			
			albums.add(a.getAlbum());
		}
		
		List<SimpleAlbum> listend= new ArrayList<SimpleAlbum>();
		for (Album a:albums) {
			SimpleAlbum sa=AlbumUtil.cover(messageSource, request, a, false);
			if (PartnerUtil.basicPowerChk(uid, a, albumPartnerDaoImpl)) {
				sa.setAdmin(true);
			}
			if (a.getLeader().getId()==uid) {
				sa.setOwn(true);
			}
			if (PartnerUtil.basicPowerChk(loginID, a, albumPartnerDaoImpl)) {
				sa.setMangerAbleCurr(true);
			}
			
			listend.add(sa);
		}
		
		/*
		 * 通过排序，将有spare1的排到最后
		 */
		Comparator<SimpleAlbum> comparator = new Comparator<SimpleAlbum>() {
			public int compare(SimpleAlbum a1, SimpleAlbum a2) {
				if (a1.getId() != a2.getId()) {
					return (int) (a1.getId() - a2.getId());
				} else {
					return (int) (a2.getId() - a1.getId());
				}
			}
		};
		Collections.sort(listend, comparator);
		return listend;
		
		
	}
	
}
