package com.goafanti.portal.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
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.banners.enums.BannersType;
import com.goafanti.banners.service.BannersService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.controller.BaseController;
import com.goafanti.common.model.Banners;
import com.goafanti.common.model.User;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.star.bo.BigShotStarListBo;
import com.goafanti.star.service.StarService;
import com.goafanti.user.service.UserInterestService;

/**
 * 大咖说
 */
@Controller
public class PortalBigShotController extends BaseController {
	@Resource
	private StarService					starService;
	@Resource
	private UserInterestService			userInterestService;
	@Resource
	private BannersService				bannersService;

	@RequestMapping(value = "/portal/scienceTechnology/bigShot", method = RequestMethod.GET)
	public ModelAndView bigShot() {
		ModelAndView mv = new ModelAndView();
		List<BigShotStarListBo> starList = starService.listBigShotStar(AFTConstants.BIG_SHOT_STAR_CACHE_KEY);
		if (null != starList && starList.size() > 0) {
			for (BigShotStarListBo bo : starList) {
				if (StringUtils.isNotBlank(bo.getUid())) {
					bo.setFansNum(userInterestService.countByToUid(bo.getUid()));
				}
				if (StringUtils.isNotBlank(bo.getEngagedField())) {
					String engagedField = "";
					String[] arr = bo.getEngagedField().split("，|,");
					if (null != arr && arr.length > 0) {
						for (String s : arr) {
							if (StringUtils.isNumeric(s)) {
								/*engagedField += engagedFieldGlossoryService.selectByPrimaryKey(Integer.parseInt(s))
										.getName();*/
							}
						}
					}
					bo.setEngagedField(engagedField);
				}
			}
			mv.addObject("starList", starList);
		}
		handleBanners(mv, BannersType.DA_KA_SHUO_1, "banners");
		handleBanners(mv, BannersType.DA_KA_SHUO_2, "starBanners");
		handleBanners(mv, BannersType.DA_KA_SHUO_3, "actBanners");
		mv.setViewName("/portal/scienceTechnology/bigShot");
		return mv;
	}

	/**
	 * 关注状态
	 */
	@RequestMapping(value = "/portal/scienceTechnology/bigShot/interestStatus", method = RequestMethod.GET)
	@ResponseBody
	public Result interestStatus() {
		Result res = new Result();
		if (TokenManager.getToken() instanceof User) {
			res.setData(starService.findBigShotInterestStatus(TokenManager.getUserId()));
		}
		return res;
	}

	private void handleBanners(ModelAndView modelview, BannersType bannersType, String name) {
		List<Banners> banners = bannersService.findPortalBanners(bannersType.getKey());
		if (!banners.isEmpty()) {
			modelview.addObject(name, banners);
		}
	}

}
