package com.neusoft.smart.pos.device.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.device.service.ConfigStrategyService;
import com.neusoft.smart.pos.dto.device.request.ConfigStrategyGroupRequest;
import com.neusoft.smart.pos.dto.device.request.ConfigStrategyQueryRequest;
import com.neusoft.smart.pos.dto.device.response.ConfigStrategyDetailResponse;
import com.neusoft.smart.pos.dto.device.response.ConfigStrategyGroupResponse;
import com.neusoft.smart.pos.dto.device.response.DeviceConfigResponse;
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.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(value = "设备配置管理restful接口", description = "设备配置管理restful接口")
@RestController
public class DeviceConfigController {
	
	@Autowired
	private ConfigStrategyService configStrategyService;
	
	@ApiOperation(value = "创建设备配置接口", notes = "创建设备配置接口")
	@PostMapping("/configStrategyGroup")
	public ResponseData<?> create(@Valid @RequestBody ConfigStrategyGroupRequest configStrategyGroupRequest, BindingResult bindingResult) {
		configStrategyService.create(configStrategyGroupRequest);
		return new ResponseData<>();
	}
	
	@ApiOperation(value = "分页查询设备配置接口", notes = "分页查询设备配置接口")
	@PostMapping("/configStrategyGroups")
	public ResponseData<PaginationExtendBean<ConfigStrategyGroupResponse>> groupList(@RequestBody ConfigStrategyQueryRequest configStrategyQueryRequest) {
		PaginationExtendBean<ConfigStrategyGroupResponse> paginationExtendBean = new PaginationExtendBean<>();
		paginationExtendBean.setRows(configStrategyService.getGroupList(configStrategyQueryRequest));
		paginationExtendBean.setTotal(configStrategyService.queryGroupCount(configStrategyQueryRequest));
		return new ResponseData<>(paginationExtendBean);
	}
	
	@ApiOperation(value = "查看设备配置详情接口", notes = "查看设备配置详情接口")
	@GetMapping("/configStrategy/{strategyId}")
	public ResponseData<ConfigStrategyDetailResponse> getDevice(@PathVariable Integer strategyId) {
		return new ResponseData<>(configStrategyService.getDetail(strategyId));
	}
	
	@ApiOperation(value = "老OTA系统的查询设备配置接口", notes = "老OTA系统的查询设备配置接口")
	@GetMapping("/OTA/Config/query/{sn}")
	public JSONObject queryConfig(@PathVariable 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);
			}
		});
		DeviceConfigResponse deviceConfigResponse = configStrategyService.queryConfig(sn);
		return JSON.parseObject(objectMapper.writeValueAsString(deviceConfigResponse));
	}
}
