package com.yeejoin.amos.avic.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.activation.DataHandler;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.doc.TycloudResource;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

import com.yeejoin.amos.avic.face.model.AvicCustomPathModel;
import com.yeejoin.amos.avic.face.model.InputStreamDataSource;
import com.yeejoin.indicators.feign.IndicatorsManager;
import com.yeejoin.indicators.feign.request.DataRecordRequest;
import com.yeejoin.indicators.feign.workflow.WorkFlowFeign;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@TycloudResource(module = "avic", value = "FileFransfer")
@RequestMapping(value = "/v1/avic/WebServicesFileFransfer")
@Api(tags = "avic-")
public class WebServicesFileFransferResource {
	private final Logger logger = LogManager.getLogger(WebServicesFileFransferResource.class);
	@Value("${avic.webservice.path}")
	String webserviceUrl;
	
	@Autowired
	IndicatorsManager indicatorsManager;
	@Autowired
	WorkFlowFeign workFlowFeign;
	
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(value = "传输文件")
	@RequestMapping(value = "/file", method = RequestMethod.POST)
	public ResponseModel postfile(
			@RequestPart MultipartFile[] files, 
			@RequestParam String code,
			@RequestParam String path,
			@RequestParam String processDefinitionId,
			@RequestParam String taskId,
			@RequestParam String bizId) {
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient(webserviceUrl);
		try {
			if (ObjectUtils.isEmpty(code)) {
				for (MultipartFile file : files) {
					InputStream is = file.getInputStream();
					DataHandler handler = new DataHandler(new InputStreamDataSource(is));
					client.invoke("transferFile", handler, file.getOriginalFilename(), path);
				}
			} else {
				for (MultipartFile file : files) {
					InputStream is = file.getInputStream();
					DataHandler handler = new DataHandler(new InputStreamDataSource(is));
					client.invoke("useCodetransferFile", handler, file.getOriginalFilename(), path, code);
				}
			}

			List<DataRecordRequest> list = new ArrayList<>();
			DataRecordRequest dataRecordRequest = new DataRecordRequest();
			dataRecordRequest.put("avicCode", code);
			dataRecordRequest.put("targetPath", path);
			list.add(dataRecordRequest);
			indicatorsManager.indicatorClient.saveBizRecord(taskId, bizId, list);
			workFlowFeign.startFormByProcess(null, processDefinitionId);
		} catch (java.lang.Exception e) {
			e.printStackTrace();
		}
		return ResponseHelper.buildResponse(null);
	}


	@TycloudOperation(ApiLevel = UserType.AGENCY)
	@ApiOperation(value = "传输目录配置")
	@RequestMapping(value = "/config", method = RequestMethod.POST)
	public ResponseModel postConfig(@RequestBody AvicCustomPathModel model) {
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient(webserviceUrl);
		try {
			client.invoke("transferPathConfig", model);
		} catch (java.lang.Exception e) {
			e.printStackTrace();
		}
		return ResponseHelper.buildResponse(null);
	}
}
