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

import com.alibaba.fastjson.JSONObject;
import com.neusoft.smart.pos.dto.ota.request.API.DevicePostBaseRequest;
import com.neusoft.smart.pos.dto.ota.request.API.DevicePostRequest;
import com.neusoft.smart.pos.dto.ota.request.API.DevicePostRequestByXK;
import com.neusoft.smart.pos.dto.ota.request.ApkUpgradeRequest;
import com.neusoft.smart.pos.dto.ota.response.API.DevicePostBaseResponse;
import com.neusoft.smart.pos.dto.ssl.request.SSLIssueRequest;
import com.neusoft.smart.pos.dto.ssl.response.SSLFileResponse;
import com.neusoft.smart.pos.framework.utils.SpringUtils;
import com.neusoft.smart.pos.gateway.service.AdvertisementService;
import com.neusoft.smart.pos.gateway.service.DeviceService;
import com.neusoft.smart.pos.gateway.service.OTAService;
import com.neusoft.smart.pos.gateway.service.SSLService;
import com.neusoft.smart.pos.gateway.service.UpgradeService4XKImpl;
import com.neusoft.smart.pos.gateway.service.UpgradeServiceImpl;
import com.neusoft.smart.pos.gateway.service.UpgradeServiceInterface;
import com.neusoft.smart.pos.redis.bean.AppStrategy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
@Api(value = "老OTA的restful接口", description = "老OTA的restful接口")
public class OTAController4POS {
	
	@Autowired
	private AdvertisementService advertisementService;
	
	@Autowired
	private DeviceService deviceService;
	
	@Autowired
	private SSLService sslService;
	
	@Autowired
	private OTAService otaDevice;
	
	@PostMapping("/OTA/query")
	public JSONObject queryUpgrade(@RequestBody JSONObject devicePostRequest) {
		DevicePostBaseRequest request;
		UpgradeServiceInterface upgradeService;
		//只能改成json格式,当熙康的没有版本的时候 需要去掉下载地址变量
		if (devicePostRequest.containsKey("SN")) {
			request = devicePostRequest.toJavaObject(DevicePostRequestByXK.class);
			upgradeService = SpringUtils.getBean(UpgradeService4XKImpl.class);
			DevicePostBaseResponse response = upgradeService.solveDeviceRequest(request);
			JSONObject jsonObject = (JSONObject) JSONObject.toJSON(response);
			if (null == jsonObject.get("packageid")) {
				jsonObject.remove("download_url");
			}
			return jsonObject;
		} else {
			request = devicePostRequest.toJavaObject(DevicePostRequest.class);
			upgradeService = SpringUtils.getBean(UpgradeServiceImpl.class);
			return (JSONObject) JSONObject.toJSON(upgradeService.solveDeviceRequest(request));
		}
	}
	
	@GetMapping("/OTA/Ad/query")
	public JSONObject queryAd(String sn) {
		return advertisementService.queryAd(sn);
	}
	
	@GetMapping("/OTA/Config/query/{sn}")
	public JSONObject queryConfig(@PathVariable String sn) {
		return deviceService.queryConfig(sn);
	}
	
	@PostMapping("/OTA/issue/certificates")
	@ApiOperation(value = "发放密钥接口", notes = "发放密钥接口")
	public SSLFileResponse issueCertificates(@RequestBody SSLIssueRequest sslIssueRequest) {
		return sslService.issueCertificates(sslIssueRequest);
	}
	
	@PostMapping("/OTA/apk/checkUpdate")
	public AppStrategy check(@RequestBody ApkUpgradeRequest apkUpgradeRequest) {
		return otaDevice.check(apkUpgradeRequest);
	}
}
