package com.neusoft.smart.pos.advertisement.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.neusoft.smart.pos.advertisement.service.MediaResourceStrategyService;
import com.neusoft.smart.pos.dto.advertisement.request.MediaResourceStrategyQueryRequest;
import com.neusoft.smart.pos.dto.advertisement.request.MediaResourceStrategyRequest;
import com.neusoft.smart.pos.dto.advertisement.response.DeviceMediaResponse;
import com.neusoft.smart.pos.dto.advertisement.response.MediaResourceStrategyResponse;
import com.neusoft.smart.pos.dto.advertisement.response.MediaStrategyResponse;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Api(value = "广告策略管理restful接口", description = "广告策略管理restful接口")
@RestController
public class MediaResourceStrategyController {
	
	@Autowired
	private MediaResourceStrategyService mediaResourceStrategyService;
	
	
	@PostMapping("/mediaResourceStrategy")
	@ApiOperation(value = "广告策略添加接口", notes = "广告策略添加接口")
	public ResponseData<?> create(@Valid @RequestBody MediaResourceStrategyRequest mediaResourceStrategyRequest,
			BindingResult bindingResult) {
		mediaResourceStrategyService.create(mediaResourceStrategyRequest);
		return new ResponseData<>();
	}
	
	
	@PostMapping("/mediaResourceStrategies")
	@ApiOperation(value = "分页查询广告策略接口", notes = "分页查询广告策略接口")
	public ResponseData<PaginationExtendBean<MediaResourceStrategyResponse>> fetchList(@Valid @RequestBody MediaResourceStrategyQueryRequest mediaResourceStrategyQueryRequest) {
		PaginationExtendBean<MediaResourceStrategyResponse> paginationExtendBean = new PaginationExtendBean<>();
		paginationExtendBean.setRows(mediaResourceStrategyService.getGroupList(mediaResourceStrategyQueryRequest));
		paginationExtendBean.setTotal(mediaResourceStrategyService.getGroupCount(mediaResourceStrategyQueryRequest));
		return new ResponseData<>(paginationExtendBean);
	}
	
	
	@GetMapping("/mediaResourceStrategy/relation/{groupId}")
	@ApiOperation(value = "查询广告策略中资源和设备关系接口", notes = "查询广告策略中资源和设备关系接口")
	public ResponseData<List<MediaStrategyResponse>> getRelation(@PathVariable Integer groupId) {
		return new ResponseData<>(mediaResourceStrategyService.getList(groupId));
	}
	
	
	@PutMapping("/mediaResourceStrategy/cancel/{groupId}")
	@ApiOperation(value = "取消广告策略接口", notes = "取消广告策略接口")
	public ResponseData<?> cancel(@PathVariable Integer groupId) {
		mediaResourceStrategyService.cancel(groupId);
		return new ResponseData<>();
	}
	
	/**
	 * 检查广告策略-迁移过的固有OTA接口
	 *
	 * @param sn
	 * @return
	 */
	@GetMapping("/OTA/Ad/query")
	public JSONObject queryAd(String sn) throws JsonProcessingException {
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategy.PropertyNamingStrategyBase() {
			@Override
			public String translate(String input) {
				return Character.toUpperCase(input.charAt(0)) + input.substring(1);
			}
		});
		DeviceMediaResponse deviceMediaResponse = mediaResourceStrategyService.queryAd(sn);
		return JSON.parseObject(objectMapper.writeValueAsString(deviceMediaResponse));
	}
	
	
}
