package com.goafanti.portal.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.goafanti.activity.service.ActivityService;
import com.goafanti.banners.enums.BannersType;
import com.goafanti.banners.service.BannersService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseController;
import com.goafanti.common.model.Banners;
import com.goafanti.common.utils.StringUtils;

@Controller
public class PortalActivityController extends BaseController {
	@Resource
	private ActivityService	activityService;
	@Resource
	private BannersService	bannersService;

	/**
	 * 活动圈
	 */
	@RequestMapping(value = "/portal/activity/activityIndex", method = RequestMethod.GET)
	public ModelAndView portalActivityActivityIndex(HttpServletRequest request, ModelAndView modelview,
			String noCache) {
		modelview.setViewName("/portal/activity/activityIndex");
		if (StringUtils.equals(noCache, "clear")) {
			activityService.cleanActivityPortalList();
		}
		modelview.addObject("activityList", activityService.findPortalList(0, 3, null));
		handleBanners(modelview, BannersType.HUO_DONG_QUAN);
		return modelview;
	}

	/**
	 * 活动详情
	 */
	@RequestMapping(value = "/portal/activity/details", method = RequestMethod.GET)
	public ModelAndView portalActivityDetails(HttpServletRequest request, ModelAndView modelview) {
		modelview.setViewName("/portal/activity/details");
		handleBanners(modelview, BannersType.HUO_DONG_XIANG_QING);
		return modelview;
	}
	
	/**
	 * 活动详情列表
	 */
	@RequestMapping(value = "/portal/activity/activityList", method = RequestMethod.GET)
	public ModelAndView portalActivityList(HttpServletRequest request, ModelAndView modelview){
		modelview.setViewName("/portal/activity/activityList");
		return modelview;
	}
	
	/**
	 * 活动列表
	 */
	@RequestMapping(value = "/portal/activity/list", method = RequestMethod.GET)
	@ResponseBody
	public Result portalActivityList(String pageSize, String pageNo, Integer form, String noCache){
		Result res = new Result();
		if (StringUtils.equals(noCache, "clear")) {
			activityService.cleanActivityPortalList();
		}
		
		Integer pN = getPageNo(pageNo);
		Integer pS = getPageSize(pageSize);
		res.setData(activityService.findPortalList((pN - 1) * pS, pS, form));
		return res;
	}
	
	/**
	 * 活动详情info
	 */
	@RequestMapping(value = "/portal/activity/detailInfo", method = RequestMethod.GET)
	@ResponseBody
	public Result portalActivityDetail(String id){
		Result res = new Result();
		if (StringUtils.isBlank(id)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "活动ID"));
			return res;
		}
		if (!StringUtils.isNumeric(id)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "活动ID"));
			return res;
		}
		res.setData(activityService.findActivityDetail(Long.parseLong(id)));
		return res;
	}

	private void handleBanners(ModelAndView modelview, BannersType bannersType) {
		List<Banners> banners = bannersService.findPortalBanners(bannersType.getKey());
		if (!banners.isEmpty()) {
			modelview.addObject("banners", banners);
		}
	}
	
	private Integer getPageNo(String pageNo){
		if (StringUtils.isNumeric(pageNo)) {
			Integer pn = Integer.parseInt(pageNo);
			return pn < 1 ? 1 : pn;
		}
		return 1;
	}
	
	private Integer getPageSize(String pageSize){
		if (StringUtils.isNumeric(pageSize)) {
			Integer ps = Integer.parseInt(pageSize);
			return ps > 50 ? 50 : (ps < 5 ? 5 : ps);
		}
		return 5;
	}
}
