package consulo.webService.plugins;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
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.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.intellij.openapi.util.io.FileUtil;
import consulo.webService.PluginChannelsService;

/**
 * @author VISTALL
 * @since 24-Sep-16
 */
@RestController
public class PluginChannelRestController
{
	private final PluginChannelsService myPluginChannelsService;
	private final PluginDeployService myPluginDeployService;

	@Autowired
	public PluginChannelRestController(PluginChannelsService pluginChannelsService, PluginDeployService pluginDeployService)
	{
		myPluginChannelsService = pluginChannelsService;
		myPluginDeployService = pluginDeployService;
	}

	@RequestMapping("/api/plugins/download")
	public ResponseEntity<?> download(@RequestParam("channel") PluginChannel channel, @RequestParam("platformVersion") String platformVersion, @RequestParam("pluginId") String pluginId)
	{
		PluginChannelService channelService = myPluginChannelsService.getUpdateService(channel);

		PluginNode select = channelService.select(platformVersion, pluginId);
		if(select == null)
		{
			return ResponseEntity.notFound().build();
		}

		File targetFile = select.targetFile;
		assert targetFile != null;
		return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + targetFile.getName() + "\"").body(new FileSystemResource(targetFile));
	}

	@RequestMapping(value = "/api/plugins/platformDeploy", method = RequestMethod.POST)
	public PluginNode platformDeploy(@RequestParam("channel") PluginChannel channel,
			@RequestBody(required = true) MultipartFile file,
			@RequestParam("platformVersion") int platformVersion,
			@RequestHeader("Authorization") String authorization) throws IOException
	{
		String keyFromClient = authorization;
		String keyFromFs = loadDeployKey();
		//TODO [VISTALL] removed this hack later - use oauth
		if(!Objects.equals(keyFromClient, keyFromFs))
		{
			throw new IOException("bad auth");
		}

		return myPluginDeployService.deployPlatform(channel, platformVersion, file);
	}

	@RequestMapping(value = "/api/plugins/deploy", method = RequestMethod.POST)
	public PluginNode deploy(@RequestParam("channel") PluginChannel channel, @RequestBody(required = true) MultipartFile file, @RequestHeader("Authorization") String authorization) throws IOException
	{
		String keyFromClient = authorization;
		String keyFromFs = loadDeployKey();
		//TODO [VISTALL] removed this hack later - use oauth
		if(!Objects.equals(keyFromClient, keyFromFs))
		{
			throw new IOException("bad auth");
		}

		return myPluginDeployService.deployPlugin(channel, file::getInputStream);
	}

	@RequestMapping("/api/plugins/list")
	public PluginNode[] list(@RequestParam("channel") PluginChannel channel, @RequestParam("platformVersion") String platformVersion)
	{
		PluginChannelService channelService = myPluginChannelsService.getUpdateService(channel);

		return channelService.select(platformVersion);
	}

	private String loadDeployKey() throws IOException
	{
		File file = new File(myPluginChannelsService.getConsuloWebServiceHome(), "deploy.key");
		return file.exists() ? FileUtil.loadTextAndClose(new FileInputStream(file)) : null;
	}
}

@@ -1334,24 +1334,52 @@
 ervice;%0A%09%7D%0A%0A
+%09// deprecated api methods%0A%0A
 %09@RequestMap
@@ -1408,16 +1408,955 @@
 nload%22)%0A
+%09@Deprecated%0A%09public ResponseEntity%3C?%3E downloadDeprecated(@RequestParam(%22channel%22) PluginChannel channel, @RequestParam(%22platformVersion%22) String platformVersion, @RequestParam(%22pluginId%22) String pluginId)%0A%09%7B%0A%09%09return download(channel, platformVersion, pluginId);%0A%09%7D%0A%0A%09@RequestMapping(value = %22/api/plugins/deploy%22, method = RequestMethod.POST)%0A%09@Deprecated%0A%09public PluginNode pluginDeployDeprecated(@RequestParam(%22channel%22) PluginChannel channel,%0A%09%09%09@RequestBody(required = true) MultipartFile file,%0A%09%09%09@RequestHeader(%22Authorization%22) String authorization) throws IOException%0A%09%7B%0A%09%09return pluginDeploy(channel, file, authorization);%0A%09%7D%0A%0A%09@RequestMapping(%22/api/plugins/list%22)%0A%09@Deprecated%0A%09public PluginNode%5B%5D listDeprecated(@RequestParam(%22channel%22) PluginChannel channel, @RequestParam(%22platformVersion%22) String platformVersion)%0A%09%7B%0A%09%09return list(channel, platformVersion);%0A%09%7D%0A%0A%09// api methods%0A%0A%09@RequestMapping(%22/api/repository/download%22)%0A
 %09public 
@@ -3043,23 +3043,26 @@
 = %22/api/
-plugins
+repository
 /platfor
@@ -3709,33 +3709,42 @@
 lue = %22/api/
+repository/
 plugin
-s/d
+D
 eploy%22, meth
@@ -3787,17 +3787,23 @@
 ginNode 
-d
+pluginD
 eploy(@R
@@ -3839,33 +3839,36 @@
 Channel channel,
- 
+%0A%09%09%09
 @RequestBody(req
@@ -3900,17 +3900,20 @@
 le file,
- 
+%0A%09%09%09
 @Request
@@ -4303,39 +4303,42 @@
 stMapping(%22/api/
-plugins
+repository
 /list%22)%0A%09public 

change plugins -> repository