package com.iformall.controller.device;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.WxDevice;
import com.iformall.domain.po.WxScreenAd;
import com.iformall.enums.EnumDeviceOnlineStatus;
import com.iformall.enums.EnumScreenAdStatus;
import com.iformall.enums.EnumScreenAdType;
import com.iformall.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wxDeviceScreenAd")
@Api(description="广告屏")
public class WxDeviceScreenAdController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private WxScreenAdService wxScreenAdService;

	@Autowired
	private WxMallService wxMallService;

	@Autowired
	private WxDeviceService wxDeviceService;

	@Autowired
	private WxCampaignService wxCampaignService;

	@Autowired
	private WxMerchantService wxMerchantService;

	@Autowired
	private WxCouponChannelService wxCouponChannelService;


	private WxDevice findDevice(String deviceId)
	{
		WxDevice wxDevice = new WxDevice();
		wxDevice.setDeviceId(deviceId);
		wxDevice.setStatus(EnumScreenAdStatus.VALID.getCode());
		List<WxDevice> list = wxDeviceService.findList(wxDevice);
		if (list.size() <= 0)
			return null;
		else
			return list.get(0);
	}


	@ApiOperation("广告列表")
	@GetMapping("list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "广告屏-列表")
	public Result list(@RequestParam String deviceId, Integer pageNum, Integer pageSize) {

		WxDevice wxDevice = findDevice(deviceId);
		if (wxDevice == null)
			return new ResultData(ErrorCode.DEVICE_NOT_FOUND);
		WxScreenAd wxScreenAd = new WxScreenAd();
		wxScreenAd.setTenantId(wxDevice.getTenantId());
		wxScreenAd.setStatus(EnumScreenAdStatus.VALID.getCode());
		PageInfo<WxScreenAd> page = wxScreenAdService.listAsPage(wxScreenAd,pageNum,pageSize);
		page.getList().stream().forEach(s->{
			Object target = null;
			if (s.getType().equals(EnumScreenAdType.COUPON.getCode()))
				target = wxCouponChannelService.findDetailVo(s.getTargetId());
			else if (s.getType().equals(EnumScreenAdType.CAMPAIGN.getCode()))
				target = wxCampaignService.getById(s.getTargetId());
			else if (s.getType().equals(EnumScreenAdType.MERCHANT.getCode()))
				target = wxMerchantService.getById(s.getTargetId());
			s.setTarget(target);
		});
		return new ResultData(page);
	}

	@ApiOperation("心跳")
	@PostMapping("heartbeat")
	public Result heartbeat(@RequestParam String deviceId,@RequestBody Map<String,String> paramMap) {
		WxDevice wxDevice = findDevice(deviceId);
		if (wxDevice == null)
			return new ResultData(ErrorCode.DEVICE_NOT_FOUND);
		WxDevice record = new WxDevice();
		record.setId(wxDevice.getId());
		if (wxDevice.getFirstHbTime() == null)
			record.setFirstHbTime(new Date());
		record.setLastHbTime(new Date());
		record.setOnlineStatus(EnumDeviceOnlineStatus.ONLINE.getCode());
		record.setVersion(paramMap.get("version"));
		wxDeviceService.saveOrUpdate(record);

		Map<String, String> result = new HashMap<>();
		result.put("config",wxDevice.getConfig());
		result.put("name", wxDevice.getName());
		result.put("location", wxDevice.getLocation());
		return new ResultData(result);
	}

	@ApiOperation("基础信息")
	@GetMapping("info")
    @SystemControllerLog(description = "广告屏-基础信息")
	public Result info(@RequestParam String deviceId) {
		WxDevice wxDevice = findDevice(deviceId);
		if (wxDevice == null)
			return new ResultData(ErrorCode.DEVICE_NOT_FOUND);
		return new ResultData(wxMallService.getByTenantId(wxDevice.getTenantId()));
	}

}
