package io.renren.kubeSphere;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.renren.RenrenApplication;
import io.renren.common.utils.GsonUtil;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.kubesphere.service.ApplicationService;
import io.renren.modules.kubesphere.service.OauthTokenService;
import io.renren.modules.kubesphere.service.S2iService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Random;

/**
 * @author chenrj
 * @email 1412736935@qq.com
 * @since 2021/7/29
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RenrenApplication.class)
public class S2iBuilders {

    @Autowired
    S2iService s2IService;

    @Autowired
    ApplicationService applicationService;

    @Autowired
    OauthTokenService oauthTokenService;

    @Test
    public void s2iBuilder() {
        String jsonData = "";
        System.out.println(jsonData);
//        s2IService.s2iBuilder(jsonData);
    }

    @Test
    public void s2iBuilderCreateBinary() {
        String fileName = "SpringOne-0.0.1-SNAPSHOT";
        fileName = fileName.toLowerCase().replaceAll("[^(a-zA-Z0-9)]", "-") + randomlyGenerateCharacters(4);
        System.out.println(fileName);
        s2IService.s2iCreateBinary(fileName);
    }

    public String randomlyGenerateCharacters(int n) {
        Random random = new Random();
        String res = "";
        for (int i = 0; i < n; i++) {
            int rand = random.nextInt(36);
            if (rand < 10) {
                res += (char) ('0' + rand);
            } else {
                res += (char) ('a' + (rand - 10));
            }
        }
        return res;
    }

    @Test
    public void addEnvironmentVariable() {
        String s2i = "{\"apiVersion\":\"devops.kubesphere.io/v1alpha1\",\"kind\":\"S2iBuilder\",\"metadata\":{\"labels\":{\"controller-tools.k8s.io\":\"1.0\",\"s2i-type.kubesphere.io\":\"b2i\"},\"annotations\":{\"languageType\":\"%s\",\"kubesphere.io/file\": \"{\\\"name\\\":\\\"%s\\\",\\\"size\\\":%s,\\\"showProgress\\\":false,\\\"showFile\\\":true,\\\"percentage\\\":100,\\\"status\\\":\\\"active\\\"}\",\"kubesphere.io/repoUrl\":\"\",\"kubesphere.io/creator\":\"%s\"},\"name\":\"%s\",\"namespace\":\"%s\"},\"spec\":{\"config\":{\"replicas\":0,\"export\":true,\"outputBuildResult\":true,\"builderPullPolicy\":\"if-not-present\",\"isBinaryURL\":true,\"tag\":\"%s\",\"builderImage\":\"%s\",\"buildVolumes\":[\"s2i_java_cache:/tmp/artifacts\"],\"runtimeArtifacts\":[{\"source\":\"/deployments\"}],\"runtimeImage\":\"%s\",\"type\":\"%s\",\"imageName\":\"%s\",\"sourceUrl\":\"%s\",\"pushAuthentication\":{\"secretRef\":{\"name\":\"%s\"}},\"environment\":[%s]}}}";

        String jsonData = "{\"languageType\":\"jar\",\"fileName\":\"SpringOne-0.0.1-SNAPSHOT.jar\",\"fileSize\":\"17320808\",\"creator\":\"admin\",\"namespace\":\"mkcloud\",\"tag\":\"latest\",\"builderImage\":\"registry.cn-beijing.aliyuncs.com/kubesphereio/java-8-centos7:v2.1.0\",\"runtimeImage\":\"registry.cn-beijing.aliyuncs.com/kubesphereio/java-8-runtime:v2.1.0\",\"type\":\"java\",\"imageName\":\"djy\",\"warehouse\":\"harbor\",\"sourceUrl\":\"http://ks-apiserver.kubesphere-system.svc/kapis/devops.kubesphere.io/v1alpha2/namespaces/mkcloud/s2ibinaries/springone-0-0-1-snapshot-jarsjgr/file/SpringOne-0.0.1-SNAPSHOT.jar\",\"environment\":[{\"name\":\"JAVA_DEBUG_PORT\",\"value\":\"1\"},{\"name\":\"JAVA_DEBUG_PORT\",\"value\":\"b\"}]}";
        Map<String, Object> parameter = GsonUtil.convertJsonToMap(jsonData);
        String languageType = parameter.get("languageType").toString();
        String fileName = parameter.get("fileName").toString();
        String fileSize = parameter.get("fileSize").toString();
        String creator = parameter.get("creator").toString();
        String namespace = parameter.get("namespace").toString();
        String tag = parameter.get("tag").toString();
        String builderImage = parameter.get("builderImage").toString();
        String runtimeImage = parameter.get("runtimeImage").toString();
        String type = parameter.get("type").toString();
        String imageName = parameter.get("imageName").toString();
        String warehouse = parameter.get("warehouse").toString();
        String sourceUrl = parameter.get("sourceUrl").toString();
        String formatImageName = imageName;
        JsonArray jsonArray = GsonUtil.convertJsonArray(parameter.get("environment").toString());
        String environment = jsonArray.toString();
        System.out.println(environment);
        environment = environment.substring(1, environment.length() - 1);
        String res = String.format(s2i, languageType, fileName, fileSize, creator, formatImageName, namespace,
                tag, builderImage, runtimeImage, type, imageName, warehouse, sourceUrl, environment);
        System.out.println(res);

    }


    @Test
    public void createStoreVolume() {
        String jsonData = "{\"name\":\"qwe\",\"storage\":2,\"alias\":\"aa\",\"labels\":{\"a\":\"123\",\"b\":\"32\"},\"description\":\"hello\",\"creator\":\"admin\"}";
        System.out.println(jsonData);
        Map<String, Object> parameters = GsonUtil.convertJsonToMap(jsonData);
        String name = parameters.get("name").toString();
        String labels = parameters.get("labels").toString();
        String alias = parameters.get("alias").toString();
        String storage = parameters.get("storage").toString();

    }



    @Test
    public void analysisJsonData() {
        String jsonData = "{\"service\":[{\"index\":0,\"visible\":true,\"serviceData\":{\"name\":\"servicename\",\"version\":\"v1\",\"description\":\"\",\"replicas\":1,\"affinity\":\"\",\"container\":[{\"index\":0,\"visible\":true,\"containerData\":{\"imageName\":\"mysql\",\"name\":\"container-apnswn\",\"containerType\":\"containers\",\"ports\":[{\"firstName\":\"http-\",\"lastName\":\"qwe\",\"containerPort\":3306,\"servicePort\":3306,\"rules\":{\"protocol\":\"\",\"path\":\"\",\"host\":\"\"}}],\"command\":\"\",\"args\":\"\",\"env\":[],\"claimName\":\"shb\",\"mountMode\":\"true\",\"nodePort\":true,\"mountPath\":\"/guazai\"}}]}}],\"applicationName\":\"appname\",\"applicationVersion\":\"v1\",\"applicationDescription\":\"\"}";
        System.out.println("jsonData:" + jsonData);
        Map<String, Object> parameter = GsonUtil.convertJsonToMap(jsonData);
        String applicationName = parameter.get("applicationName").toString();
        String applicationVersion = parameter.get("applicationVersion").toString();
        String applicationDescription = parameter.get("applicationDescription").toString();
        System.out.println(parameter.get("service").toString());
    }

    @Test
    public void headTest() {
        UserEntity user = new UserEntity("admin","admin");
        oauthTokenService.oauthToken(user);
    }
}
