package com.realnet.OpenAi.Services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.realnet.fnd.response.EntityResponse;
import com.realnet.utils.PortConstant;

@Service
public class Script_Making {

	@Value("${projectPath}")
	private String projectPath;

	@Autowired
	private ScriptSrvice sureservice;

//	Creating files like yaml, shell SCript ,docker File

	public ResponseEntity<?> CreateFiles(Integer prj_id, String workflow_id, long Deployment_profile)
			throws IOException {

//		String table_name = tble.replaceAll(" ", "_").toLowerCase();

		String prj_url = PortConstant.BACKEND_PORTAL_DOMAIN + "/token/fnd1/callingsureops/getproject/" + prj_id;

		// get project
		ResponseEntity<Object> prj = GET(prj_url);
		Object prj_body = prj.getBody();
		List<String> lineList = callforproject(prj_body);

		// get workflowline
		String line_url = PortConstant.BACKEND_PORTAL_DOMAIN + "/token/fnd1/callingsureops/workflowline/" + workflow_id;
		ResponseEntity<Object> get = GET(line_url);
		Object body = get.getBody();
		String workflow_model = callforline(body);

		String PRJ_NAME = lineList.get(0);
		String gitea_url = lineList.get(1);

//		Parsing Json data 

		JsonParser parser1 = new JsonParser();
		JsonElement element1 = parser1.parse(workflow_model);
		JsonArray Array1 = element1.getAsJsonArray();

		int i = 1;
		for (JsonElement ar1 : Array1) {
			JsonObject obj1 = ar1.getAsJsonObject();
//			System.out.println(obj1);
			Set<Map.Entry<String, JsonElement>> entries = obj1.entrySet();
			for (Map.Entry<String, JsonElement> entry : entries) {
				String key = entry.getKey();
				String value1 = entry.getValue().getAsString();

				if (value1.equalsIgnoreCase("Shell Script") || value1.equalsIgnoreCase("Others")
						|| value1.equals("Yamel File") || (value1.equalsIgnoreCase("Docker File"))) {
					System.out.println("script data");
					sureservice.script(prj_id, workflow_model, PRJ_NAME, gitea_url, Deployment_profile);
					i++;
				}
			}
		}
		if (i == 1) {

			sureservice.Createonefile(prj_id, lineList, workflow_model, Deployment_profile);
		}

		return new ResponseEntity<>(new EntityResponse("script file created"), HttpStatus.CREATED);

	}

	public List<String> callforproject(Object object) throws JsonProcessingException {

		List<String> list = new ArrayList<>();
		ObjectMapper mapper = new ObjectMapper();
		String str = mapper.writeValueAsString(object);
//		System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object));// print
		JsonParser parser = new JsonParser();
		JsonElement element = parser.parse(str);

		JsonObject obj = element.getAsJsonObject();

		String prj_name = obj.get("projectName").getAsString();
		list.add(prj_name);

		String gitea_url = obj.get("gitea_url").getAsString();
		list.add(gitea_url);

		String prj_id = obj.get("id").getAsString();
		list.add(prj_id);

		return list;
	}

	public String callforline(Object object) throws JsonProcessingException {

		ObjectMapper mapper = new ObjectMapper();
		String str = mapper.writeValueAsString(object);
//		System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object));// print
		JsonParser parser = new JsonParser();
		JsonElement element = parser.parse(str);

		JsonObject obj = element.getAsJsonObject();
		JsonElement workflowid = obj.get("model");
//		System.out.println(workflowid);
		return workflowid.getAsString();
	}

	public String callfortable(Object object) throws JsonProcessingException {

		ObjectMapper mapper = new ObjectMapper();
		String str = mapper.writeValueAsString(object);
//		System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object));// print
		JsonParser parser = new JsonParser();
		JsonElement element = parser.parse(str);

		JsonObject obj = element.getAsJsonObject();
		JsonElement workflowid = obj.get("id");
//		System.out.println(workflowid);
		return workflowid.getAsString();
	}

	public ResponseEntity<Object> GET(String get) {
		RestTemplate restTemplate = new RestTemplate();

		ResponseEntity<Object> u = restTemplate.getForEntity(get, Object.class);

		return u;

	}

	private HttpHeaders getHeaders() {
		HttpHeaders headers = new HttpHeaders();
		headers.set("Content-Type", MediaType.APPLICATION_JSON_VALUE);
		headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
		return headers;
	}

}
