package com.goafanti.banners.controller;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.goafanti.banners.enums.BannersType;
import com.goafanti.banners.service.BannersService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.model.Banners;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.JDBCIdGenerator;

@Controller
@RequestMapping(value = "/api/admin/banners")
public class AdminBannersApiController extends BaseApiController {
	@Resource
	BannersService	bannersService;
	@Autowired
	JDBCIdGenerator	idGenerator;

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public Result upload(String sign, HttpServletRequest req) {
		Assert.isTrue(StringUtils.isNotBlank(sign) && BannersType.containsType(sign), "广告图位置标示不正确。");
		Result res = res();
		Calendar c = Calendar.getInstance();
		res.data(handleFile(res, false, req, AttachmentType.BANNERS_PIC.getCode(), String.valueOf(c.get(Calendar.YEAR)),
				String.valueOf(c.get(Calendar.MONTH) + 1), idGenerator.generateId().toString()));
		return res;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Result save(@Valid Banners banners, BindingResult bindingResult) {
		Assert.notNull(banners, "广告图参数不正确");
		Assert.isTrue(StringUtils.isNotBlank(banners.getSign()) && BannersType.containsType(banners.getSign()),
				"广告图位置标示不正确。");
		Assert.hasText(banners.getImgurl(), "广告图url参数不正确");
		Assert.isTrue(!bindingResult.hasFieldErrors(),
				bindingResult.hasFieldErrors() ? bindingResult.getFieldError().getDefaultMessage() : "");
		Result res = res();
		if (banners.getId() == null) {
			banners.setId(idGenerator.generateId());
			res.data(bannersService.insertBanner(banners));
		} else {
			res.data(bannersService.updateBanner(banners));
		}
		return res;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result portalBannersList(String key, String pageNo, String pageSize) {
		Assert.isTrue(StringUtils.isBlank(key) || BannersType.containsType(key), "广告图位置标示不正确。");
		Integer pN = getPageNo(pageNo);
		Integer pS = getPageSize(pageSize);
		Map<String, Object> result = new HashMap<>();
		result.put("list", bannersService.findBannersList(key, (pN - 1) * pS, pS));
		result.put("total", bannersService.findBannersListCount(key));
		return res().data(result);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Result delete(String id) {
		Assert.isTrue(StringUtils.isNumeric(id), "广告图id不正确。");
		return res().data(bannersService.deleteBanner(Long.valueOf(id)));
	}

	@RequestMapping(value = "/clearCache", method = RequestMethod.GET)
	public Result clearCache(String key) {
		Assert.isTrue(StringUtils.isNotBlank(key) && BannersType.containsType(key), "广告图位置标示不正确。");
		bannersService.cleanList(key);
		return res().data(bannersService.findPortalBanners(key));
	}

	@RequestMapping(value = "/keys", method = RequestMethod.GET)
	public Result portalBannersKeys() {
		Map<String, String> types = new HashMap<>();
		for (BannersType type : BannersType.values()) {
			types.put(type.getKey(), type.getDesc());
		}
		return res().data(types);
	}

}
