package com.realnet.Communication.Services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.realnet.config.EmailService;
import com.realnet.users.entity1.AppUser;
import com.realnet.users.service1.AppUserServiceImpl;
import com.realnet.utils.Port_Constant;

@Service
public class EmailNotificationService {

	@Autowired
	private EmailService emailService;

	@Autowired
	private AppUserServiceImpl userService;

	public void sendDirectEmail(String email, String subject, String message) throws JsonProcessingException {
		// Call the sendSimpleMessage method from EmailService
		emailService.sendSimpleMessage(null, email, subject, message);
	}

	public void sendmailViaSetu(String email, String Name, String type) {
		// Call the method from EmailCommunicationService

		switch (type) {
		case "TeamMember":
			sendEmailTeamMember(email, Name);
			break;

		case "WorkSpaceUser":
			sendEmailtoWorkUser(email, Name);
			break;
		case "AddProject":
			sendEmailAfterAddPrj(email, Name);
			break;

		case "CopyProject":
			sendEmailAfterCopyPrj(email, Name);
			break;

		case "CreateWireframe":
			sendEmailAfterCopyPrj(email, Name);
			break;

		default:
			break;
		}

	}

//	 for sending mail to team member
	public ResponseEntity<?> sendEmailTeamMember(String email, String FullName) {
		// Call the method from EmailCommunicationService

		String subject = "Team Added to Workspace";
		String message = "Dear " + FullName + ",\n\nYou have been added to the workspace.";
		String templateName = "addproject";
		String gatewayName = "ganesh";

		// Sending the email via Setu

		ResponseEntity<?> responseEntity = sendEmailViaSetu(email, message, templateName, gatewayName);
		return responseEntity;
	}

//	 for sending mail to Sec workspace User
	public ResponseEntity<?> sendEmailtoWorkUser(String email, String FullName) {
		// Call the method from EmailCommunicationService

		String subject = "Workspace Access Granted";
		String message = "Dear " + FullName + ",\n\nYou have been granted access to the workspace.";
		String templateName = "addproject"; // Replace with actual template name
		String gatewayName = "ganesh"; // Replace with actual gateway name

		// Sending the email via Setu
		return sendEmailViaSetu(email, message, templateName, gatewayName);
	}

//	 for sending mail After Add project
	public ResponseEntity<?> sendEmailAfterAddPrj(String email, String Name) {
		// Call the method from EmailCommunicationService

		String subject = "Add Project";
		String message = "Project " + Name + " has been created successfully.";
		String templateName = "addproject"; // Replace with actual template name
		String gatewayName = "ganesh"; // Replace with actual gateway name

		// Sending the email via Setu
		return sendEmailViaSetu(email, message, templateName, gatewayName);
	}

//	 for sending mail After Copy project
	public ResponseEntity<?> sendEmailAfterCopyPrj(String email, String Name) {
		// Call the method from EmailCommunicationService

		String subject = "Copy Project";
		String message = "Project " + Name + " has been Copied successfully.";
		String templateName = "addproject"; // Replace with actual template name
		String gatewayName = "ganesh"; // Replace with actual gateway name

		// Sending the email via Setu
		return sendEmailViaSetu(email, message, templateName, gatewayName);
	}

//	 for sending mail After create Wireframe
	public ResponseEntity<?> sendEmailAfterCreateWireframe(String email, String Name) {
		// Call the method from EmailCommunicationService

		String subject = "Create Wireframe";
		String message = "A new wireframe has been successfully added to your project.";

		String templateName = "addproject"; // Replace with actual template name
		String gatewayName = "ganesh"; // Replace with actual gateway name

		// Sending the email via Setu
		return sendEmailViaSetu(email, message, templateName, gatewayName);
	}

//	send mail via setu
	public ResponseEntity<?> sendEmailViaSetu(String email, String message, String templateName, String gatewayName)
			throws ResourceAccessException {

//		template name = notification_template, gateway name = email_gateway
		try {

			String jsonData = "{\r\n" + "  \"job_type\": \"Email\",\r\n" + "  \"send_to\": \"" + email.trim()
					+ "\",\r\n" + "  \"cc\": \"cc@example.com\",\r\n"
//					+ "  \"attachment\": \"sample-file.txt\",\r\n"
					+ "  \"gatewaydone\": \"N\",\r\n" + "  \"template_name\": \"" + templateName.trim() + "\",\r\n"
					+ "  \"replacement_string\": \"Hello, {name} " + message + "!\",\r\n" + "  \"gatewayName\": \""
					+ gatewayName.trim() + "\"\r\n" + "}\r\n";

			HttpHeaders headers = new HttpHeaders();
//			headers.setContentType(MediaType.MULTIPART_FORM_DATA);

			MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
			queryParams.add("data", jsonData);

			HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(queryParams, headers);

			String apiUrl2 = Port_Constant.SURE_SETU_DOMAIN
					+ "/token/Surecommunication/communication/jobtable/Com_jobTable"; // Replace with the
			// actual API URL

			RestTemplate restTemplate = new RestTemplate();

			ResponseEntity<String> responseEntity = restTemplate.postForEntity(apiUrl2, requestEntity, String.class);

			return ResponseEntity.ok(responseEntity.getBody());

		} catch (ResourceAccessException e) {
			throw new ResourceAccessException("communication server no start..." + e);
		}

		catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}

	}

}
