package com.goafanti.activity.controller;

import java.util.Arrays;
import java.util.Calendar;

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

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
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 com.goafanti.activity.bo.InputActivity;
import com.goafanti.activity.service.ActivityService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.enums.ActivityCacheKey;
import com.goafanti.common.enums.ActivityFields;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.model.Activity;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.JDBCIdGenerator;

/**
 * 活动圈
 */
@RestController
@RequestMapping(value = "/api/admin/activity")
public class AdminActivityApiController extends BaseApiController {
	@Resource
	private ActivityService	activityService;
	@Autowired
	JDBCIdGenerator			idGenerator;
	
	/**
	 * 活动报名列表
	 */
	@RequestMapping(value = "/signUpList", method = RequestMethod.GET)
	public Result signUpList(){
		Result res = new Result();
		return res;
	}

	/**
	 * 活动列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result list(String sTime, String eTime, String name, String host, Integer type, Integer form,
			String pageSize, String pageNo) {
		Result res = new Result();
		Integer pNo = 1;
		Integer pSize = 10;
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		res.setData(activityService.listActivity(sTime, eTime, name, host, type, form, pNo, pSize));
		return res;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result add(@Valid InputActivity ia, BindingResult bindingResult, String startTimeFormattedDate,
			String endTimeFormattedDate, String enrollDeadlineFormattedDate) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					ActivityFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		res = disposeInputActivity(res, ia, startTimeFormattedDate);
		if (!res.getError().isEmpty()) {
			return res;
		}

		activityService.save((Activity) res.getData(), startTimeFormattedDate, endTimeFormattedDate,
				enrollDeadlineFormattedDate);
		return res;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Result update(@Valid InputActivity ia, BindingResult bindingResult, String startTimeFormattedDate,
			String endTimeFormattedDate, String enrollDeadlineFormattedDate) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					ActivityFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		if (StringUtils.isBlank(ia.getId())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "活动ID"));
			return res;
		}

		res = disposeInputActivity(res, ia, startTimeFormattedDate);
		if (!res.getError().isEmpty()) {
			return res;
		}

		activityService.update((Activity) res.getData(), startTimeFormattedDate, endTimeFormattedDate,
				enrollDeadlineFormattedDate, Long.parseLong(ia.getId()));
		return res;
	}

	/**
	 * 详情
	 */
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public Result detail(Long id) {
		Result res = new Result();
		if (null == id) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "活动Id"));
			return res;
		}
		res.setData(activityService.findActivityDetail(id));
		return res;
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Result delete(@RequestParam(name = "ids[]", required = false) String[] ids) {
		Result res = new Result();
		if (ids == null || ids.length < 1) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
		} else {
			res.setData(activityService.batchDeleteByPrimaryKey(Arrays.asList(ids)));
		}
		return res;
	}

	/**
	 * 活动图片上传
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public Result upload(HttpServletRequest req, String sign) {
		Result res = new Result();
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.ACTIVITY_TITLE_PICTURE
				|| attachmentType == AttachmentType.ACTIVITY_CONTENT_PICTURE) {
			Calendar c = Calendar.getInstance();
			res.setData(handleFile(res, false, req, sign, String.valueOf(c.get(Calendar.YEAR)),
					String.valueOf(c.get(Calendar.MONTH) + 1), idGenerator.generateId().toString()));
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 清除缓存
	 */
	@RequestMapping(value = "/cleanCache", method = RequestMethod.POST)
	public Result cleanCache(String cacheKey) {
		Result res = new Result();
		if (StringUtils.isBlank(cacheKey)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "缓存标记"));
			return res;
		}
		if (ActivityCacheKey.ACTIVITY_PORTAL_LIST_CACHE_KEY.getCode().equals(cacheKey)) {
			activityService.cleanActivityPortalList(); // 清除门户端科技活动列表缓存
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "缓存标记"));
		}
		return res;
	}
	
	

	private Result disposeInputActivity(Result res, InputActivity ia, String startTimeFormattedDate) {
		if (StringUtils.isBlank(ia.getName())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "标题"));
			return res;
		}

		if (StringUtils.isBlank(startTimeFormattedDate)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "活动开始时间"));
			return res;
		}

		if (null == ia.getType()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "活动类型"));
			return res;
		}

		if (null == ia.getForm()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "活动形式"));
			return res;
		}

		if (StringUtils.isBlank(ia.getAddress())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "活动地点"));
			return res;
		}
		Activity a = new Activity();
		BeanUtils.copyProperties(ia, a);
		res.setData(a);
		return res;
	}
}
