package org.datavaultplatform.broker.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

import org.datavaultplatform.broker.services.UserKeyPairService;
import org.datavaultplatform.common.model.User;
import org.datavaultplatform.common.model.FileStore;

import org.jsondoc.core.annotation.ApiHeader;
import org.jsondoc.core.annotation.ApiHeaders;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiPathParam;
import org.jsondoc.core.pojo.ApiVerb;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import org.datavaultplatform.broker.services.FileStoreService;
import org.datavaultplatform.broker.services.UsersService;

@RestController
public class FileStoreController {
    
    private UsersService usersService;
    private FileStoreService fileStoreService;
    private UserKeyPairService userKeyPairService;
    private String host;
    private String port;
    private String rootPath;
    private String passphrase;
    
    public void setFileStoreService(FileStoreService fileStoreService) {
        this.fileStoreService = fileStoreService;
    }
    
    public void setUsersService(UsersService usersService) {
        this.usersService = usersService;
    }

    public void setUserKeyPairService(UserKeyPairService userKeyPairService) {
        this.userKeyPairService = userKeyPairService;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setRootPath(String rootPath) {
        this.rootPath = rootPath;
    }

    public void setPassphrase(String passphrase) {
        this.passphrase = passphrase;
    }

    @RequestMapping(value = "/filestores", method = RequestMethod.GET)
    public List<FileStore> getFileStores(@RequestHeader(value = "X-UserID", required = true) String userID) {
        User user = usersService.getUser(userID);
        
        List<FileStore> userStores = user.getFileStores();
        for (FileStore store : userStores) {
            // For now - strip out config information
            store.setProperties(null);
        }
        
        return userStores;
    }
    
    @RequestMapping(value = "/filestores", method = RequestMethod.POST)
    public FileStore addFileStore(@RequestHeader(value = "X-UserID", required = true) String userID,
                                  @RequestBody FileStore store) throws Exception {
        
        User user = usersService.getUser(userID);
        store.setUser(user);
        fileStoreService.addFileStore(store);
        return store;
    }

    @RequestMapping(value = "/filestores/keys", method = RequestMethod.POST)
    public String addKeyPair(@RequestHeader(value = "X-UserID", required = true) String userID) throws Exception {
        User user = usersService.getUser(userID);

        userKeyPairService.generateNewKeyPair();

        HashMap<String,String> storeProperties = new HashMap<String,String>();
        storeProperties.put("host", host);
        storeProperties.put("port", port);
        storeProperties.put("rootPath", rootPath);
        storeProperties.put("username", user.getID());
        storeProperties.put("password", "");
        storeProperties.put("publicKey", userKeyPairService.getPublicKey());
        storeProperties.put("privateKey", userKeyPairService.getPrivateKey());
        storeProperties.put("passphrase", passphrase);

        FileStore store = new FileStore("org.datavaultplatform.common.storage.impl.SFTPFileSystem", storeProperties, "SFTP filesystem");
        store.setUser(user);
        fileStoreService.addFileStore(store);

        return userKeyPairService.getPublicKey();
    }
}

@@ -2740,178 +2740,1323 @@
 hod.
-POST)%0A    public String addKeyPair(@RequestHeader(value = %22X-UserID%22, required = true) String userID) throws Exception %7B%0A        User user = usersService.getUser(userID);
+GET)%0A    public String getPublicKey(@RequestHeader(value = %22X-UserID%22, required = true) String userID) %7B%0A        User user = usersService.getUser(userID);%0A%0A        // Note: - There should only be one SFTP FileStore.%0A        String publicKey = null;%0A        List%3CFileStore%3E userStores = user.getFileStores();%0A        for (FileStore userStore : userStores) %7B%0A            if (userStore.getStorageClass().equals(%22org.datavaultplatform.common.storage.impl.SFTPFileSystem%22)) %7B%0A                publicKey = userStore.getProperties().get(%22publicKey%22);%0A                break;%0A            %7D%0A        %7D%0A%0A        return publicKey;%0A    %7D%0A%0A    @RequestMapping(value = %22/filestores/keys%22, method = RequestMethod.POST)%0A    public String addKeyPair(@RequestHeader(value = %22X-UserID%22, required = true) String userID) throws Exception %7B%0A        User user = usersService.getUser(userID);%0A%0A        // At the moment we can only cope with one SFTP FileStore, so if one already exists, throw an Exception.%0A        List%3CFileStore%3E userStores = user.getFileStores();%0A        for (FileStore userStore : userStores) %7B%0A            if (userStore.getStorageClass().equals(%22org.datavaultplatform.common.storage.impl.SFTPFileSystem%22)) %7B%0A                throw new Exception(%22Can't create SFTP FileStore as one already exists%22);%0A            %7D%0A        %7D
 %0A%0A  

Add a GET method for the SFTP public key