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.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.HotStarListBo;
import com.goafanti.star.service.StarService;
import com.goafanti.user.service.UserInterestService;

/**
 * 科技明星
 */
@Controller
public class PortalStarController extends BaseController {
	@Resource
	private StarService					starService;
	@Resource
	private UserInterestService			userInterestService;

	@Resource
	private BannersService				bannersService;

	@RequestMapping(value = "/portal/scienceTechnology/star", method = RequestMethod.GET)
	public ModelAndView star() {
		ModelAndView mv = new ModelAndView();
		List<HotStarListBo> starList = starService
				.listStarList(TokenManager.getToken() instanceof User ? TokenManager.getUserId() : null);
		if (null != starList && starList.size() > 0) {
			for (HotStarListBo 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.KE_JI_MING_XING);
		mv.setViewName("/portal/scienceTechnology/star");
		return mv;
	}

	/**
	 * 当前用户关注"科技明星"页面科技明星状态
	 */
	@RequestMapping(value = "/portal/scienceTechnology/star/interestStatus", method = RequestMethod.GET)
	@ResponseBody
	public Result interestStatus() {
		Result res = new Result();
		if (TokenManager.getToken() instanceof User) {
			res.setData(starService.findStarInterestStatus(TokenManager.getUserId()));
		}
		return res;
	}

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