/*
 * Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.uva.sne.drip.api.rest;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed;
import nl.uva.sne.drip.api.dao.CloudCredentialsDao;
import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.MessageParameter;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import nl.uva.sne.drip.api.rpc.DRIPCaller;
import nl.uva.sne.drip.api.rpc.DeployerCaller;
import nl.uva.sne.drip.api.service.ClusterCredentialService;
import nl.uva.sne.drip.api.service.ProvisionService;
import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.types.CloudCredentials;
import nl.uva.sne.drip.commons.types.ClusterCredentials;
import nl.uva.sne.drip.commons.types.DeployParameter;
import nl.uva.sne.drip.commons.types.LoginKey;
import nl.uva.sne.drip.commons.types.ProvisionInfo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author S. Koulouzis
 */
@RestController
@RequestMapping("/user/deployer")
@Component
public class DeployController {

    @Value("${message.broker.host}")
    private String messageBrokerHost;

    @Autowired
    private CloudCredentialsDao cloudCredentialsDao;

    @Autowired
    private ProvisionService provisionService;

    @Autowired
    private ClusterCredentialService clusterCredentialService;

    @RequestMapping(value = "/deploy/{id}/", method = RequestMethod.GET, params = {"cluster"})
    @RolesAllowed({UserService.USER, UserService.ADMIN})
    public @ResponseBody
    String deploy(@PathVariable("id") String provisionID, @RequestParam(value = "cluster") String clusterType) {
        checkClusterType(clusterType);
        try (DRIPCaller deployer = new DeployerCaller(messageBrokerHost);) {
            Message deployerInvokationMessage = buildDeployerMessage(provisionID, clusterType.toLowerCase());

            Message response = (deployer.call(deployerInvokationMessage));
//            Message response = generateFakeResponse();
            List<MessageParameter> params = response.getParameters();
            ClusterCredentials clusterCred = new ClusterCredentials();
            for (MessageParameter p : params) {
                String name = p.getName();
                if (name.equals("credential")) {
                    String value = p.getValue();
                    clusterCred.setContents(value);
                    clusterCredentialService.getDao().save(clusterCred);
                    return clusterCred.getId();
                }
            }

        } catch (IOException | TimeoutException | JSONException | InterruptedException ex) {
            Logger.getLogger(DeployController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @RolesAllowed({UserService.USER, UserService.ADMIN})
    public @ResponseBody
    ClusterCredentials get(@PathVariable("id") String id) {
        ClusterCredentials clusterC = clusterCredentialService.getDao().findOne(id);
        if (clusterC == null) {
            throw new NotFoundException();
        }
        return clusterC;
    }

    @RequestMapping(value = "/ids", method = RequestMethod.GET)
    @RolesAllowed({UserService.USER, UserService.ADMIN})
    public @ResponseBody
    List<String> getIds() {
        List<ClusterCredentials> all = clusterCredentialService.getDao().findAll();
        List<String> ids = new ArrayList<>(all.size());
        for (ClusterCredentials pi : all) {
            ids.add(pi.getId());
        }
        return ids;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @RolesAllowed({UserService.USER, UserService.ADMIN})
    public @ResponseBody
    String delete(@PathVariable("id") String id) {
        ClusterCredentials cred = clusterCredentialService.getDao().findOne(id);
        if (cred != null) {
            provisionService.getDao().delete(id);
            return "Deleted : " + id;
        }
        throw new NotFoundException();
    }

    private Message buildDeployerMessage(String provisionID, String clusterType) {
        ProvisionInfo pro = provisionService.getDao().findOne(provisionID);
        if (pro == null) {
            throw new NotFoundException();
        }
        String cloudConfID = pro.getCloudConfID();
        CloudCredentials cCred = cloudCredentialsDao.findOne(cloudConfID);
        List<LoginKey> loginKeys = cCred.getLoginKeys();
        List<DeployParameter> deployParams = pro.getDeployParameters();
        List<MessageParameter> parameters = new ArrayList<>();
        for (DeployParameter dp : deployParams) {
            String cName = dp.getCloudCertificateName();
            MessageParameter messageParameter = new MessageParameter();
            messageParameter.setName("credential");
            messageParameter.setEncoding("UTF-8");
            String key = null;
            for (LoginKey lk : loginKeys) {
                String lkName = lk.getName();
                if (lkName == null) {
                    lkName = lk.getAttributes().get("domain_name");
                }
                if (lkName.equals(cName)) {
                    key = lk.getKey();
                    break;
                }
            }
            messageParameter.setValue(key);
            Map<String, String> attributes = new HashMap<>();
            attributes.put("IP", dp.getIP());
            attributes.put("role", dp.getRole());
            attributes.put("user", dp.getUser());
            messageParameter.setAttributes(attributes);
            parameters.add(messageParameter);
        }
        MessageParameter clusterTypeParameter = new MessageParameter();
        clusterTypeParameter.setName("cluster");
        clusterTypeParameter.setEncoding("UTF-8");
        clusterTypeParameter.setValue(clusterType);
        parameters.add(clusterTypeParameter);
        Message deployInvokationMessage = new Message();

        deployInvokationMessage.setParameters(parameters);
        deployInvokationMessage.setCreationDate(System.currentTimeMillis());
        return deployInvokationMessage;
    }

    private Message generateFakeResponse() throws IOException, TimeoutException, InterruptedException, JSONException {
        String strResponse = "{'creationDate': 1488453891505, 'parameters': "
                + "[{'url': 'null', 'attributes': 'null', 'name': 'credential', "
                + "'value': 'apiVersion: v1\\nclusters:\\n- cluster:\\n    "
                + "certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tL"
                + "S0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBRE"
                + "FWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRFM01ETXdNakV"
                + "4TWpNek9Gb1hEVEkzTURJeU9ERXhNak16T0Zvd0ZURVRNQkVHQTFVRQpBeE1L"
                + "YTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBR"
                + "ENDQVFvQ2dnRUJBTGV4CitSS2p4SjR3WXJacTc5bHU1dExCc0JZbWtCTi9xZl"
                + "AzUXRUeXV6RW5nWmFYaHpQWnNYbGlGK3VLNVVROEFVNmYKdDdFNzVVQWVtYlR"
                + "MeXdjeDZqYmlYOXJuMEsxVnJBaktpdCsycGR1dHFZN1N0UDB5K2sxRXd4aWNR"
                + "aFZaeE8yNgpxbkM4QXNFL0Y3U2lBYXhVeVo2emVSVU1VTGZXMTk0UXQ1YS8xZ"
                + "nhkcERCUHZOOGlNOEVKUWk1RXJrbkN5S2piCm5Wa0lEU2VIQk9hUW5yYjBrM2"
                + "1OOStBdGNrM1pPNU1oRFJGajF3VWFZNFdKbmIzd01yZUhlWmJJL1Y5NWhjMFk"
                + "KREhlNGs2eW1MbFE4NmhmWjZyMDNQWTVGVC81eE55ZDRCN1p2YURqdW5zWXNB"
                + "NU11UWFIdUQwM3p3QkNxTlRNMQpyY3NRUkk1dVI0ejFYUVFheUVVQ0F3RUFBY"
                + "U1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU"
                + "1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFLYmFzMlRIa010YnVjZkJ"
                + "lS3FUVGVDaGdMOEcKN0JqZmwxSGtqRWFYZUtsS1BTZ3NYYlNLdXNiUjNHTjdM"
                + "SGtZd0NQa0FXT1hoSmV6andxUVFTdzVyMUdVbXAvRgpFaHBKOTByM2l3NUJ3M"
                + "nJxOUdRNytOb1lQNENQdXYwd0N3V0wrWXd3ejhFSzhvVi9vdGNxdWxYVWUwUUZ"
                + "lL2txCkN3VGJvS2JBRElXd2xoSDdDd09ZOHo3SnRjREZ2WEtRYnRlTGNyUTY5"
                + "MFYyTUQxeTZyeUdmUUhsaWtsRjA2Sk8KMElXSVhDam1NYXppaG1hNGVHQTRHO"
                + "XZ6R2lMMVlyZGIxVDdYbFd3Zm5iOTg1QmVUWEZvV1JjMUo3RjFvQ3BaVwp2OTN3cko5L01uUzZxN3p2NkdNaUE0Rm5VanZiUDB4OHhWVXpsYjVvVG5HenFycSt2MXJLQklGSlFXdz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=\\n    server: https://52.201.84.116:6443\\n  name: kubernetes\\ncontexts:\\n- context:\\n    cluster: kubernetes\\n    user: admin\\n  name: admin@kubernetes\\n- context:\\n    cluster: kubernetes\\n    user: kubelet\\n  name: kubelet@kubernetes\\ncurrent-context: admin@kubernetes\\nkind: Config\\npreferences: {}\\nusers:\\n- name: admin\\n  user:\\n    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM0ekNDQWN1Z0F3SUJBZ0lJWlR0eVhaRWNRUTh3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4TnpBek1ESXhNVEl6TXpoYUZ3MHhPREF6TURJeE1USXpOREJhTUJzeApHVEFYQmdOVkJBTVRFR3QxWW1WeWJtVjBaWE10WVdSdGFXNHdnZ0VpTUEwR0NTcUdTSWIzRFFFQkFRVUFBNElCCkR3QXdnZ0VLQW9JQkFRRFFkOWdJK0liTVhDWlB6d244cGZuK1Vxd0RldXRKZjY5YWFCNFI4ZjZsSHl0dVRyNTkKZWJoZm43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SmRkdjBSVWg2WW9ZMjJ5cnJnbWphMS9xcm92YmppOHZ5VFUxREt3CnZBODB4dnd0eUZGOHhlSStQdTk2bTc4Q2dZQnlteGpUZU9ENjkrSktaOUFmc3BnazU0aUFNL0JIWWcvNkMyc2YKUkZ2U0wvdUdEREp1WUtZTHZUUmtjZ0ptSlF5TjA5NFpyL015WlU1SHprVzRRQ1VvcXd3aUJqRHJwS0hPWG5RSApodzBzSXV6UVc0Tk1lUUNjS0luR2dEQ1F4bVZWL2Mvd1h0dXYySWVJeWlnUnRzZ1dtNmFScElFS1lXRXJlV2pkCmlPZ0JjUUtCZ0ZZeTJYMmRubHVnUUI5TDhDWVRYMDZ2Q3ZNM3BsOWIwZXN3MFcxTzljVGJEbmViYXRUdW1lUy8KMEtpUHh5b2tBS1pCM3NDTUl3ZC9ZbTdtbEdjRU9aNEhCLzJCWURHaTdUWFdyTjkwRmVGZFlPVVRXWEpQaHNwQwpQRXVET2hGSHBMZTN6Smxyck9ySG1Ebm5aOFdtWU4rdGZzNnVuVHVsSXZPQWFKd01xVlZ4Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==\\n', "
                + "'encoding': 'UTF-8'}]}";

        strResponse = strResponse.replaceAll("'null'", "null").replaceAll("\'", "\"").replaceAll(" ", "");

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

        return mapper.readValue(strResponse, Message.class);
    }

    private void checkClusterType(String clusterType) {
        switch (clusterType.toLowerCase()) {
            case "kubernetes":
                break;
            case "swarm":
                break;
            default:
                throw new BadRequestException("Cluster type not supported. May use kubernetes or swarm");
        }

    }

}
