package com.goafanti.portal.controller;

import java.util.List;

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

import org.apache.commons.lang3.time.DateFormatUtils;
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.app.bo.ProjectDetailBo;
import com.goafanti.banners.enums.BannersType;
import com.goafanti.banners.service.BannersService;
import com.goafanti.business.service.BusinessProjectService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseController;
import com.goafanti.common.model.Banners;
import com.goafanti.common.model.News;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.news.bo.NewsPortalList;
import com.goafanti.news.enums.NewsType;
import com.goafanti.news.service.NewsService;

@Controller
public class PortalNewsController extends BaseController {
	@Resource
	NewsService				newsService;
	@Resource
	private BannersService	bannersService;
	@Resource
	private BusinessProjectService	businessProjectService;
	/**
	 * 策源地
	 * 
	 * @param request
	 * @param modelview
	 * @return
	 */
	@RequestMapping(value = "/portal/news/newsIndex", method = RequestMethod.GET)
	public ModelAndView portalNewsNewsIndex(HttpServletRequest request, ModelAndView modelview, String noCache) {
		modelview.setViewName("/portal/news/newsIndex");
		if (StringUtils.equals(noCache, "clear")) {
			newsService.cleanList();
		}
		String domainName = request.getServerName();
		modelview.addObject("kjkx", newsService.findNewsList(0, NewsType.KJZX.getCode(), 2, domainName, false));
		modelview.addObject("jtdt", newsService.findNewsList(0, NewsType.JTDT.getCode(), 2, domainName, false));
		modelview.addObject("zfwl", newsService.findNewsList(0, NewsType.ZFWL.getCode(), 3, domainName, false));
		modelview.addObject("gjzc", newsService.findNewsList(0, NewsType.GJZC.getCode(), 3, domainName, false));
		modelview.addObject("dfzc", newsService.findNewsList(0, NewsType.DFZC.getCode(), 3, domainName, false));
		handleBanners(modelview, BannersType.CE_YUAN_DI);
		return modelview;
	}

	/**
	 * 新闻详情
	 */
	@RequestMapping(value = "/portal/news/newsDetails", method = RequestMethod.GET)
	public ModelAndView portalNewsNewsDetails(HttpServletRequest request, ModelAndView modelview) {
		modelview.setViewName("/portal/news/newsDetails");
		return modelview;
	}

	/**
	 * 新闻详情APP
	 */
	@RequestMapping(value = "/portal/news/newsDetailApp", method = RequestMethod.GET)
	public ModelAndView portalNewsNewsDetailApp(HttpServletRequest request, ModelAndView modelview) {
		modelview.setViewName("/portal/news/newsDetailApp");
		return modelview;
	}

	/**
	 * 新闻详情列表
	 */
	@RequestMapping(value = "/portal/news/list", method = RequestMethod.GET)
	@ResponseBody
	public Result portalNewsList(Integer type, String pageSize, String pageNo, Integer hot, Integer provinceId,
			String keyword) {
		Result res = new Result();
		Integer pNo = 1, pSize = 4;
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		res.setData(newsService.portalNewsList(pSize, pNo, type, hot, provinceId, keyword));
		return res;
	}

	/**
	 * 新闻详情
	 */
	@RequestMapping(value = "/portal/news/detailInfo", method = RequestMethod.GET)
	@ResponseBody
	public Result portalNewsDetail(Long id) {
		Result res = new Result();
		if (null == id) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻ID"));
			return res;
		}

		/* newsService.portalNewsList(6,1, type,hot,provinceId,keyword) */

		res.setData(newsService.findNewsDetail(id));
		return res;
	}

	@RequestMapping(value = "/portal/news/newsDetail", method = RequestMethod.GET)
	public ModelAndView portalHighTechEvaluateIprInfo(HttpServletRequest request, ModelAndView modelview, Long id) {
		modelview.setViewName("/portal/news/newsDetail");
		Pagination<NewsPortalList> newsList = newsService.portalNewsList(6, 1, 5, null, null, null);
		News newsDetail = newsService.findNewsDetail(id);
		Object news = newsList.getList().get(0);
		if (null != newsDetail) {
			newsDetail.setCreateTimeS(newsDetail.getCreateTime() != null
					? DateFormatUtils.format(newsDetail.getCreateTime(), AFTConstants.MMDDYYYY) : "");
			String summary = newsDetail.getSummary();
			modelview.addObject("hasHighTech",
					summary != null && (summary.indexOf("高新") > -1 || summary.indexOf("高企") > -1));
			modelview.addObject("hasPatent",
					summary != null && (summary.indexOf("专利") > -1 || summary.indexOf("知识产权") > -1));
			modelview.addObject("hasCopyright",
					summary != null && (summary.indexOf("软著") > -1 || summary.indexOf("知识产权") > -1));
			modelview.addObject("hasTechProj",
					summary != null && (summary.indexOf("科技") > -1 || summary.indexOf("项目") > -1));
		}
		@SuppressWarnings("unchecked")
		List<NewsPortalList> la = (List<NewsPortalList>) newsList.getList();
		for (NewsPortalList n : la) {
			String createTimeFormattedDate = n.getCreateTimeFormattedDate();
			n.setDataS(createTimeFormattedDate);
		}
		modelview.addObject("newsDetail", newsDetail);
		modelview.addObject("newsList", newsList.getList());
		modelview.addObject("news", news);
		return modelview;
	}

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

	/**
	 * 高新认定APP
	 */
	@RequestMapping(value = "/portal/news/appCognizance", method = RequestMethod.GET)
	public ModelAndView appCognizance(HttpServletRequest request, ModelAndView modelview,String id) {
		ProjectDetailBo bo = businessProjectService.orgProject(id);
		if (StringUtils.isNotBlank(bo.getIntroduce())) 
			bo.setIntroduce(bo.getIntroduce().replace("\n", "</br>"));
		if (StringUtils.isNotBlank(bo.getValueEffect())) 
		bo.setValueEffect(bo.getValueEffect().replace("\n", "</br>"));
		if (StringUtils.isNotBlank(bo.getClientSize())) 
		bo.setClientSize(bo.getClientSize().replace("\n", "</br>"));
		modelview.addObject("bo", bo);
		modelview.setViewName("/portal/news/appCognizance");
		return modelview;
	}
	
	/**
	 * APP注册协议
	 */
	@RequestMapping(value = "/portal/news/agreement", method = RequestMethod.GET)
	public ModelAndView agreement(HttpServletRequest request, ModelAndView modelview) {
		modelview.setViewName("/portal/news/agreement");
		return modelview;
	}
	
}