package com.ifish.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.ifish.dto.JsonResult;
import com.ifish.dto.PageingDto;
import com.ifish.entity.LiveBanner;
import com.ifish.entity.LiveMessage;
import com.ifish.enums.LiveRoomOrderTypeEnum;
import com.ifish.enums.ResultEnum;
import com.ifish.enums.SubDirectoryEnum;
import com.ifish.exception.IfishException;
import com.ifish.service.LiveRoomService;
import com.ifish.util.IfishFilePath;
import com.ifish.util.IfishFileUtils;
import com.ifish.util.IfishUtil;
import com.ifish.util.ResultUtil;
import com.ifish.validator.LiveRoomParam;
import com.ifish.validator.PageParam;

/**
 * 
 * @author ggw
 *
 */
@RestController
public class LiveRoomAction {
	@Autowired
	private LiveRoomService liveRoomService;
	
	/**
	 * 新增直播间
	 * @param liveRoom
	 * @param fileUpload
	 * @return
	 */
	@RequestMapping(value="/liveRoom/v3/addLiveRoom.do",method=RequestMethod.POST)
	public JsonResult<?> addLiveRoom(MultipartFile fileUpload,LiveRoomParam liveRoomParam,BindingResult result){
		//校验参数
		if(result.hasErrors() || fileUpload==null){
			throw new IfishException(ResultEnum.error401);
		}
		//上传封面不能大于1M
		if(fileUpload.getSize()>1048576){
			throw new IfishException(ResultEnum.warn206);
		}
		//新增直播间
		JSONObject json = liveRoomService.addLiveRoom(liveRoomParam);
		//图片名称
		String picName = json.getInteger("roomId")+".png";
		try {
			//上传文件
			IfishFileUtils.uploadFile(IfishFilePath.path_room_img, picName, fileUpload);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return ResultUtil.success(json);
	}
	/**
	 * 修改直播间
	 * @param liveRoom
	 * @param fileUpload
	 * @return
	 */
	@RequestMapping(value="/liveRoom/v3/updateLiveRoom.do",method=RequestMethod.POST)
	public JsonResult<?> updateLiveRoom(MultipartFile fileUpload,@RequestParam("roomId")Integer roomId,LiveRoomParam liveRoomParam,BindingResult result){
		//校验参数
		if(result.hasErrors()){
			throw new IfishException(ResultEnum.error401);
		}
		//上传的图片不能大于1M
		if(fileUpload!=null && fileUpload.getSize()>1048576){
			throw new IfishException(ResultEnum.warn206);
		}
		//更新
		JsonResult<?> jsonResult = liveRoomService.updateLiveRoom(roomId,liveRoomParam);
		//更新文件
		if(fileUpload!=null){
			//图片名称
			String picName = roomId+".png";
			try {
				//上传文件
				IfishFileUtils.uploadFile(IfishFilePath.path_room_img, picName, fileUpload);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return jsonResult;
	}
	
	/**
	 * 直播间人气加一
	 * @param roomId
	 * @return
	 */
	@RequestMapping(value="/liveRoom/v3/popularityValue.do",method=RequestMethod.POST)
	public Object popularityValue(Integer roomId,Integer userId){
		return liveRoomService.popularityValue(roomId,userId);
	}
	
	/**
	 * 获取直播间信息(修改直播间信息)
	 * @param roomId
	 * @return
	 */
	@RequestMapping(value="/liveRoom/v3/getLiveRoomInfo.do",method=RequestMethod.POST)
	public Object getLiveRoomInfo(Integer userId){
		return liveRoomService.getLiveRoomInfo(userId);
	}
	
	/**
	 * 获取直播列表
	 * @param firstResult
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/liveRoom/v3/getLiveRooms.do",method=RequestMethod.POST)
	public Object popularityValue(Integer firstResult,Integer pageSize,Integer userId,String orders){
		return liveRoomService.getLiveRoomsByPage(firstResult, pageSize,userId,orders);
	}
	
	/**
	 * 获取直播banner图
	 * @return
	 */
	@RequestMapping(value="/liveRoom/v3/getLiveBanners.do",method=RequestMethod.POST)
	public Object getLiveBanners(){
		List<LiveBanner> list = liveRoomService.getLiveBanners();
		if(list!=null){
			for (LiveBanner liveBanner : list) {
				liveBanner.setBannerImg(IfishFilePath.getPath(SubDirectoryEnum.banner,liveBanner.getBannerImg()));
			}
			return IfishUtil.toJson(ResultEnum.success.getKey(), list);
		}
		return IfishUtil.toJson(ResultEnum.fail101.getKey(), "");
	}
	
	/**
	 * 直播间留言
	 * @param liveMessage
	 * @return
	 */
	@RequestMapping(value="/liveRoom/v3/leaveMessage.do",method=RequestMethod.POST)
	public Object leaveMessage(LiveMessage liveMessage){
		return liveRoomService.leaveMessage(liveMessage);
	}
	
	/**
	 * 留言列表
	 * @param liveMessage
	 * @return
	 */
	@RequestMapping(value="/liveRoom/v3/getLiveMessage.do",method=RequestMethod.POST)
	public Object getLiveMessageByPage(Integer roomId,Integer firstResult,Integer pageSize){
		return liveRoomService.getLiveMessageByPage(firstResult, pageSize, roomId);
	}
	
	/**
	 * 按类型分页获取直播间信息
	 * @param pageParam
	 * @param result
	 * @return
	 */
	@RequestMapping(value="/liveRooms",method=RequestMethod.GET)
	public PageingDto<?> getLiveRoomByPate(@RequestParam("orderType") Integer orderType,@Validated PageParam pageParam,BindingResult result){
		//参数校验
		if(result.hasErrors()){
			throw new IfishException(ResultEnum.error401);
		}
		//按时间倒序
		if(LiveRoomOrderTypeEnum.one.getKey().equals(orderType)){
			return liveRoomService.getLiveRoomsByTime(pageParam);
		}
		//人气值倒序
		else if(LiveRoomOrderTypeEnum.two.getKey().equals(orderType)){
			return liveRoomService.getLiveRoomsByPopularityValue(pageParam);
		}
		//后台推荐顺序倒序
		else if(LiveRoomOrderTypeEnum.three.getKey().equals(orderType)){
			return liveRoomService.getLiveRoomsByTuijian(pageParam);
		}
		else{
			throw new IfishException(ResultEnum.error402);
		}
	}
	
	/**
	 * 直播间点赞
	 * @param roomId
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/liveRoomZan/{roomId}/{userId}",method=RequestMethod.POST)
	public JsonResult<?> liveRoomsZan(@PathVariable("roomId")Integer roomId,@PathVariable("userId")Integer userId){
		return liveRoomService.liveRoomsZan(roomId, userId);
	}
	
}
