package io.renren.modules.kubesphere.module;

import io.renren.common.utils.GsonUtil;
import io.renren.modules.kubesphere.module.request.S2iBuilderCreateBinaryRequest;
import io.renren.modules.kubesphere.module.request.S2iBuildersRequest;

/**
 * kubeSphere请求体构建
 *
 * @author chenrj
 * @email 1412736935@qq.com
 * @since 2021/7/31
 */
public class KubeSphereRequestBuilder {

    private static class KubeSphere {
        public static final String S2iBuilder = "{\"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\\\":\\\"SpringOne-0.0.1-SNAPSHOT.jar\\\",\\\"size\\\":17320808,\\\"showProgress\\\":false,\\\"showFile\\\":true,\\\"percentage\\\":100,\\\"status\\\":\\\"active\\\"}\",\"kubesphere.io/repoUrl\":\"\",\"kubesphere.io/creator\":\"%s\"},\"name\":\"get-latest-a0s\",\"namespace\":\"%s\"},\"spec\":{\"config\":{\"replicas\":0,\"export\":true,\"outputBuildResult\":true,\"builderPullPolicy\":\"if-not-present\",\"isBinaryURL\":true,\"tag\":\"latest\",\"builderImage\":\"registry.cn-beijing.aliyuncs.com/kubesphereio/java-8-centos7:v2.1.0\",\"buildVolumes\":[\"s2i_java_cache:/tmp/artifacts\"],\"runtimeArtifacts\":[{\"source\":\"/deployments\"}],\"runtimeImage\":\"%s\",\"type\":\"java\",\"sourceUrl\":\"%s\",\"imageName\":\"%s\",\"pushAuthentication\":{\"secretRef\":{\"name\":\"%s\"}}}}}";
        public static final String S2iBuilderCreateBinary = "{\"apiVersion\":\"devops.kubesphere.io/v1alpha1\",\"kind\":\"S2iBinary\",\"metadata\":{\"annotations\":{\"kubesphere.io/creator\":\"admin\"},\"labels\":{\"controller-tools.k8s.io\":\"1.0\"},\"name\":\"%s\",\"namespace\":\"%s\"}}";
    }

    /**
     * 镜像制作
     */
    public static final String S2iBuilderCreateBinary = "{\"apiVersion\":\"devops.kubesphere.io/v1alpha1\",\"kind\":\"S2iBinary\",\"metadata\":{\"annotations\":{\"kubesphere.io/creator\":\"admin\"},\"labels\":{\"controller-tools.k8s.io\":\"1.0\"},\"name\":\"%s\",\"namespace\":\"mkcloud\"}}";
    public static final String S2iRun = "{\"apiVersion\":\"devops.kubesphere.io/v1alpha1\",\"kind\":\"S2iRun\",\"metadata\":{\"name\":\"%s\",\"cluster\":\"default\",\"namespace\":\"%s\",\"annotations\":{\"kubesphere.io/creator\":\"%s\"}},\"spec\":{\"builderName\":\"%s\"}}";
    public static final String S2iBuilder = "{\"apiVersion\":\"devops.kubesphere.io/v1alpha1\",\"kind\":\"S2iBuilder\",\"metadata\":{\"labels\":{\"controller-tools.k8s.io\":\"1.0\",\"s2i-type.kubesphere.io\":\"b2i\"},\"annotations\":{\"languageType\":\"binary\",\"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\",\"type\":\"binary\",\"sourceUrl\":\"%s\",\"imageName\":\"%s\",\"pushAuthentication\":{\"secretRef\":{\"name\":\"%s\"}}}}}";


    /**
     * 应用制作
     */
    public static final String DEPLOYMENT_DRY_RUN = "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"namespace\":\"%s\",\"labels\":{\"version\":\"v1\",\"app\":\"%s\",\"app.kubernetes.io/version\":\"v1\",\"app.kubernetes.io/name\":\"%s\"},\"name\":\"%s\",\"annotations\":{\"servicemesh.kubesphere.io/enabled\":\"false\",\"kubesphere.io/creator\":\"%s\"}},\"spec\":{\"replicas\":%d,\"selector\":{\"matchLabels\":{\"version\":\"v1\",\"app\":\"%s\",\"app.kubernetes.io/version\":\"v1\",\"app.kubernetes.io/name\":\"%s\"}},\"template\":{\"metadata\":{\"labels\":{\"version\":\"v1\",\"app\":\"%s\",\"app.kubernetes.io/version\":\"v1\",\"app.kubernetes.io/name\":\"%s\"},\"annotations\":{\"sidecar.istio.io/inject\":\"false\"}},\"spec\":{\"containers\":[{\"name\":\"%s\",\"imagePullPolicy\":\"IfNotPresent\",\"pullSecret\":\"harbor\",\"image\":\"%s\",\"resources\":{},\"ports\":[{\"name\":\"%s\",\"protocol\":\"TCP\",\"containerPort\":%d,\"servicePort\":%d}]}],\"serviceAccount\":\"default\",\"affinity\":{},\"initContainers\":[],\"volumes\":[],\"imagePullSecrets\":[{\"name\":\"harbor\"}]}},\"strategy\":{\"type\":\"RollingUpdate\",\"rollingUpdate\":{\"maxUnavailable\":\"25%%\",\"maxSurge\":\"25%%\"}}}}";
    public static final String APPLICATION_DRY_RUN = "{\"apiVersion\":\"app.k8s.io/v1beta1\",\"kind\":\"Application\",\"metadata\":{\"name\":\"%s\",\"namespace\":\"%s\",\"labels\":{\"app.kubernetes.io/version\":\"v1\",\"app.kubernetes.io/name\":\"%s\"},\"annotations\":{\"servicemesh.kubesphere.io/enabled\":\"false\",\"kubesphere.io/creator\":\"%s\"}},\"spec\":{\"selector\":{\"matchLabels\":{\"app.kubernetes.io/version\":\"v1\",\"app.kubernetes.io/name\":\"%s\"}},\"addOwnerRef\":true,\"componentKinds\":[{\"group\":\"\",\"kind\":\"Service\"},{\"group\":\"apps\",\"kind\":\"Deployment\"},{\"group\":\"apps\",\"kind\":\"StatefulSet\"},{\"group\":\"extensions\",\"kind\":\"Ingress\"},{\"group\":\"servicemesh.kubesphere.io\",\"kind\":\"Strategy\"},{\"group\":\"servicemesh.kubesphere.io\",\"kind\":\"ServicePolicy\"}]}}";
    public static final String SERVICE_DRY_RUN = "{ \"apiVersion\": \"v1\", \"kind\": \"Service\", \"metadata\": { \"namespace\": \"%s\", \"labels\": { \"version\": \"v1\", \"app\": \"%s\", \"app.kubernetes.io/version\": \"v1\", \"app.kubernetes.io/name\": \"%s\" }, \"annotations\": { \"kubesphere.io/serviceType\": \"statelessservice\", \"servicemesh.kubesphere.io/enabled\": \"false\", \"kubesphere.io/creator\": \"%s\" }, \"name\": \"%s\" }, \"spec\": { \"sessionAffinity\": \"None\", \"selector\": { \"app\": \"%s\", \"app.kubernetes.io/version\": \"v1\", \"app.kubernetes.io/name\": \"%s\" }, \"template\": { \"metadata\": { \"labels\": { \"version\": \"v1\", \"app\": \"%s\", \"app.kubernetes.io/version\": \"v1\", \"app.kubernetes.io/name\": \"%s\" } } }, \"ports\": [ { \"name\": \"%s\", \"protocol\": \"TCP\", \"port\": %d, \"targetPort\": %d } ], \"type\": \"NodePort\" } }";

    public static final String DEPLOYMENT = "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"namespace\":\"%s\",\"labels\":{\"version\":\"v1\",\"app\":\"%s\",\"app.kubernetes.io/version\":\"v1\",\"app.kubernetes.io/name\":\"%s\"},\"name\":\"%s\",\"annotations\":{\"servicemesh.kubesphere.io/enabled\":\"false\",\"kubesphere.io/creator\":\"%s\"}},\"spec\":{\"replicas\":%d,\"selector\":{\"matchLabels\":{\"version\":\"v1\",\"app\":\"%s\",\"app.kubernetes.io/version\":\"v1\",\"app.kubernetes.io/name\":\"%s\"}},\"template\":{\"metadata\":{\"labels\":{\"version\":\"v1\",\"app\":\"%s\",\"app.kubernetes.io/version\":\"v1\",\"app.kubernetes.io/name\":\"%s\"},\"annotations\":{\"sidecar.istio.io/inject\":\"false\"}},\"spec\":{\"containers\":[{\"name\":\"%s\",\"imagePullPolicy\":\"IfNotPresent\",\"pullSecret\":\"harbor\",\"image\":\"%s\",\"resources\":{},\"ports\":[{\"name\":\"%s\",\"protocol\":\"TCP\",\"containerPort\":%d,\"servicePort\":%d}]}],\"serviceAccount\":\"default\",\"affinity\":{},\"initContainers\":[],\"volumes\":[],\"imagePullSecrets\":[{\"name\":\"harbor\"}]}},\"strategy\":{\"type\":\"RollingUpdate\",\"rollingUpdate\":{\"maxUnavailable\":\"25%%\",\"maxSurge\":\"25%%\"}}}}";
    public static final String APPLICATION = "{\"apiVersion\":\"app.k8s.io/v1beta1\",\"kind\":\"Application\",\"metadata\":{\"name\":\"%s\",\"namespace\":\"%s\",\"labels\":{\"app.kubernetes.io/version\":\"v1\",\"app.kubernetes.io/name\":\"%s\"},\"annotations\":{\"servicemesh.kubesphere.io/enabled\":\"false\",\"kubesphere.io/creator\":\"%s\"}},\"spec\":{\"selector\":{\"matchLabels\":{\"app.kubernetes.io/version\":\"v1\",\"app.kubernetes.io/name\":\"%s\"}},\"addOwnerRef\":true,\"componentKinds\":[{\"group\":\"\",\"kind\":\"Service\"},{\"group\":\"apps\",\"kind\":\"Deployment\"},{\"group\":\"apps\",\"kind\":\"StatefulSet\"},{\"group\":\"extensions\",\"kind\":\"Ingress\"},{\"group\":\"servicemesh.kubesphere.io\",\"kind\":\"Strategy\"},{\"group\":\"servicemesh.kubesphere.io\",\"kind\":\"ServicePolicy\"}]}}";
    public static final String SERVICE = "{ \"apiVersion\": \"v1\", \"kind\": \"Service\", \"metadata\": { \"namespace\": \"%s\", \"labels\": { \"version\": \"v1\", \"app\": \"%s\", \"app.kubernetes.io/version\": \"v1\", \"app.kubernetes.io/name\": \"%s\" }, \"annotations\": { \"kubesphere.io/serviceType\": \"statelessservice\", \"servicemesh.kubesphere.io/enabled\": \"false\", \"kubesphere.io/creator\": \"%s\" }, \"name\": \"%s\" }, \"spec\": { \"sessionAffinity\": \"None\", \"selector\": { \"app\": \"%s\", \"app.kubernetes.io/version\": \"v1\", \"app.kubernetes.io/name\": \"%s\" }, \"template\": { \"metadata\": { \"labels\": { \"version\": \"v1\", \"app\": \"%s\", \"app.kubernetes.io/version\": \"v1\", \"app.kubernetes.io/name\": \"%s\" } } }, \"ports\": [ { \"name\": \"%s\", \"protocol\": \"TCP\", \"port\": %d, \"targetPort\": %d } ], \"type\": \"NodePort\" } }";


    /**
     * 构建S2iBuilderCreateBinary请求体
     */
    public static S2iBuilderCreateBinaryRequest buildS2iCreateBinaryRequest(String fileName) {
        return buildRequest(fileName, KubeSphere.S2iBuilderCreateBinary, S2iBuilderCreateBinaryRequest.class);
    }

    /**
     * 构建S2iBuilder请求体
     */
    public static S2iBuildersRequest buildS2iRequest(String sourceUrl) {
        return buildRequest(sourceUrl, KubeSphere.S2iBuilder, S2iBuildersRequest.class);
    }

    /**
     * 构造请求体对象
     *
     * @param arg  参数
     * @param json 请求体json
     */
    private static <T> T buildRequest(String arg, String json, Class<T> targetClass) {
        return GsonUtil.convertJson(String.format(json, arg), targetClass);
    }

}
