package com.lerx.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lerx.annotation.Token;
import com.lerx.dao.iface.IAlbumDao;
import com.lerx.dao.iface.IAlbumPartnerDao;
import com.lerx.dao.iface.ITeamDao;
import com.lerx.dao.iface.IUserDao;
import com.lerx.entities.Album;
import com.lerx.entities.AlbumPartner;
import com.lerx.entities.Team;
import com.lerx.entities.User;
import com.lerx.login.util.LoginUtil;
import com.lerx.portal.obj.SimplePartner;

@RequestMapping("/action_album_partner")
@Controller
public class AlbumPartnerHandler {
	
	@Autowired
	private IAlbumDao albumDaoImpl;
	
	@Autowired
	private IUserDao userDaoImpl;
	
	@Autowired
	private IAlbumPartnerDao albumPartnerDaoImpl;
	
	@Autowired
	private ITeamDao TeamDaoImpl;
	
	
	@Autowired
	private ResourceBundleMessageSource messageSource;
	
	private static final String LOGINPAGE = "jsp/user/login";
	private static final String FORBIDU = "jsp/result/forbidu";
	
	@RequestMapping(value = {"/list/{albid}","/list/{albid}/{lookOnly}"})
	@Token(ajax=false,login=true,failedPage=FORBIDU)
	public String list(@PathVariable(value = "albid", required = true) Long albid,@PathVariable(value = "lookOnly", required = false) Integer lookOnly,HttpServletRequest request, HttpSession session,Map<String, Object> map) {
		if (lookOnly==null) {
			lookOnly=0;
		}
		long uid = LoginUtil.uid(messageSource, request);
		Album album = albumDaoImpl.findByID(albid);
		if (album == null  || album.isDeled() || !album.isStatus()) {
			return FORBIDU;
		}
		if (album.getLeader().getId()==uid) {
			List<AlbumPartner> listAP=albumPartnerDaoImpl.queryByAid(albid);
			map.put("album", album);
			map.put("list", listAP);
			map.put("lookOnly", lookOnly);
			return "jsp/album/control/partner";
		}else {
			return FORBIDU ;
		}
		
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/add/{albid}/{uid}")
	@Token(ajax=true,csrf=true,contrast = true,login=true,failedPage=LOGINPAGE)
	public int add(@PathVariable(value = "albid", required = true) Long albid,@PathVariable(value = "uid", required = true) Long uid,HttpServletRequest request, HttpSession session,Map<String, Object> map) {
		long curruid = LoginUtil.uid(messageSource, request);
		Album album = albumDaoImpl.findByID(albid);
		if (album == null  || album.isDeled() || !album.isStatus()) {
			return -400;
		}
		if (album.getLeader().getId() != curruid) {
			return -200;
		}
		if (album.getLeader().getId()==uid) {
			return -502;
		}
		
		List<AlbumPartner> listAP=albumPartnerDaoImpl.queryByAid(albid);
		for (int i=0;i<listAP.size();i++) {
			AlbumPartner partner = listAP.get(i);
			if (partner.getUser().getId()==uid) {
				return -502;
			}
		}
		
		
		User user=userDaoImpl.findByID(uid);
		if (user==null) {
			return -405;
		}
		AlbumPartner partner =new AlbumPartner();
		
		partner.setAlbum(album);
		partner.setStatus(true);
		partner.setUser(user);
		partner.setApplyTime(System.currentTimeMillis());
		albumPartnerDaoImpl.add(partner);
		
		album.setTotalPartner(albumPartnerDaoImpl.countByAlbumID(album.getId()));
		albumDaoImpl.modify(album);
		teamUpdate(album);
		return 0;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/del/{pid}")
	@Token(ajax=true,csrf=true,contrast = true,login=true,failedPage=LOGINPAGE)
	public int del(@PathVariable(value = "pid", required = true) Long pid,HttpServletRequest request, HttpSession session,Map<String, Object> map) {
		AlbumPartner partner = albumPartnerDaoImpl.findByID(pid);
		if (partner==null) {
			return -404;
		}
		long curruid = LoginUtil.uid(messageSource, request);
		Album album = partner.getAlbum();
		if (album == null  || album.isDeled() || !album.isStatus()) {
			return -400;
		}
		if (album.getLeader().getId() != curruid) {
			return -200;
		}
		albumPartnerDaoImpl.delByID(pid);
		
		
		teamUpdate(album);
		
		album.setTotalPartner(albumPartnerDaoImpl.countByAlbumID(album.getId()));
		albumDaoImpl.modify(album);
		
		return 0;
	}
	
	private void teamUpdate(Album album) {
		List<AlbumPartner> listAP=albumPartnerDaoImpl.queryByAid(album.getId());
		String userteam=album.getLeader().getId()+",";
		for (int i=0;i<listAP.size();i++) {
			AlbumPartner p = listAP.get(i);
			userteam+=p.getUser().getId()+",";
		}
		
		Team team=album.getCb().getObjTeam();
		team.setUidstr(userteam);
		TeamDaoImpl.modify(team);
	}
	
	//自己退出
	@ResponseBody
	@RequestMapping(value = "/secede/{aid}")
	@Token(ajax=true,csrf=true,contrast = true,login=true,failedPage=LOGINPAGE)
	public int secede(@PathVariable(value = "aid", required = true) Long aid,HttpServletRequest request, HttpSession session,Map<String, Object> map) {
		Album album = albumDaoImpl.findByID(aid);
		if (album==null) {
			return -400;
		}
		long curruid = LoginUtil.uid(messageSource, request);
		if (curruid<=0) {
			return -201;
		}
		
		AlbumPartner partner =albumPartnerDaoImpl.findByAIDAndUid(aid, curruid);
		if (partner==null) {
			return -404;
		}
		if (partner.getUser().getId() != curruid) {
			return -406;
		}
		
		albumPartnerDaoImpl.delByID(partner.getId());
		
		album.setTotalPartner(albumPartnerDaoImpl.countByAlbumID(album.getId()));
		albumDaoImpl.modify(album);
		
		return 0;
	}
	
	
	
	//列出所有成员 0 是专辑主人
	@ResponseBody
	@RequestMapping(value = "/all/{aid}")
	@Token(ajax=true,csrf=true,login=true,failedPage=LOGINPAGE)
	public List<SimplePartner> simpleall(@PathVariable(value = "aid", required = true) Long aid,HttpServletRequest request, HttpSession session,Map<String, Object> map) {
		Album album = albumDaoImpl.findByID(aid);
		if (album == null  || album.isDeled() || !album.isStatus()) {
			return null;
		}
		
		List<SimplePartner> lr=new ArrayList<SimplePartner>();
		SimplePartner partner = new  SimplePartner();
		partner.setUid(album.getLeader().getId());
		partner.setUsername(album.getLeader().getUsername());
		partner.setTab(0);
		partner.setNickname(album.getLeader().getNickname());
		lr.add(partner);
		List<AlbumPartner> listAP=albumPartnerDaoImpl.queryByAid(aid);
		for (int i=0;i<listAP.size();i++) {
			AlbumPartner ap = listAP.get(i);
			partner = new  SimplePartner();
			partner.setUid(ap.getUser().getId());
			partner.setUsername(ap.getUser().getUsername());
			partner.setTab(1);
			partner.setNickname(ap.getUser().getNickname());
			lr.add(partner);
		}
		return lr;
		
	}
	
	

}
