package org.example.tampoposerverdtram.resources;

import java.util.*;
import jakarta.ws.rs.*;
import jakarta.ws.rs.client.*;
import jakarta.ws.rs.core.*;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

@Path("/chatRequests")
@Component
public class ChatRequests {
	private Map<String, Map<String, Object>> value = new HashMap<>();
	private Client client = ClientBuilder.newClient();
	@Produces(MediaType.APPLICATION_JSON)
	@GET
	public Map<String, Map<String, Object>> getValue() {
		return new HashMap<>(this.value);
	}
	@DELETE
	public void deleteChatRequest(@FormParam("id") String id) throws JsonProcessingException {
		Map<String, Map<String, Object>> temp_if23;
		if (this.value.containsKey(id)) {
			this.value.remove(id);
			temp_if23 = this.value;
		} else {
			temp_if23 = this.value;
		}
		this.value = temp_if23;
		Form form = new Form();
		form.param("chatRequests", new ObjectMapper().writeValueAsString(this.value));
		Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		String result = client.target("http://localhost:8080").path("/chatReqId").request().post(entity, String.class);
	}
	@POST
	public void postChatRequest(@FormParam("receiverId") String receiverId, @FormParam("senderId") String senderId) throws JsonProcessingException {
		Map<String, Map<String, Object>> users_json = client.target("http://localhost:8080").path("/users").request().get(HashMap.class);
		Map<String, Map<String, Object>> users = new HashMap<>();
		users = users_json;
		String chatReqId = client.target("http://localhost:8080").path("/chatReqId").request().get(String.class);
		Map<String, Map<String, Object>> temp_if25;
		if (senderId.equals(receiverId)) {
			temp_if25 = this.value;
		} else {
			Map<String, Map<String, Object>> temp_if24;
		if ((users.containsKey(senderId)&&users.containsKey(receiverId))) {
			Map<String, Object> temp_json0 = new HashMap<>();
		temp_json0.put("senderId", senderId);
		temp_json0.put("receiverId", receiverId);
		this.value.put(chatReqId,temp_json0);
			temp_if24 = this.value;
		} else {
			temp_if24 = this.value;
		}
			temp_if25 = temp_if24;
		}
		this.value = temp_if25;
		Form form = new Form();
		form.param("chatRequests", new ObjectMapper().writeValueAsString(this.value));
		Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		String result = client.target("http://localhost:8080").path("/chatReqId").request().post(entity, String.class);
	}
}