package enea.lecservice.services;

import enea.lecservice.models.dto.BaseRequest;
import enea.lecservice.models.dto.CommunityDTO;
import enea.lecservice.models.dto.LocalUserDTO;
import enea.lecservice.security.dto.ActivateRequest;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;

@Log4j2
@Service
public class RegisterService implements IRegisterService {
    @Value("${data.api.baseurl}")
    private String baseUrl;

    @Override
    public String getCommunities(BaseRequest request) {
        String url = baseUrl + "community/listAllCommunities";
        var headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_LANGUAGE, request.getContentLanguage());

        var entity = new HttpEntity<>(headers);
        return new RestTemplate().getForObject(url, String.class, entity);
    }

    @Override
    public String register(LocalUserDTO model, String language) {

        String url = String.format("%s%s",
                baseUrl,
                "user/register"
        );

        var headers = new org.springframework.http.HttpHeaders();
        headers.add(HttpHeaders.CONTENT_LANGUAGE, language);

        var map = new HashMap<String, Object>();
        map.put("email", model.getEmail());
        map.put("name", model.getName());
        map.put("surname", model.getSurname());
        map.put("password", model.getPassword());
        map.put("organizzation", model.getOrganization());
        map.put("usercommunities", model.getUsercommunities());

        var entity = new HttpEntity<>(map, headers);

        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject(url, entity, String.class);
    }
    
    @Override
    public String update(LocalUserDTO model, String language, String token) {

        String url = String.format("%s%s",
                baseUrl,
                "user/update"
        );

        var headers = new org.springframework.http.HttpHeaders();
        headers.add(HttpHeaders.CONTENT_LANGUAGE, language);
        headers.add(HttpHeaders.AUTHORIZATION, token);

        var map = new HashMap<String, Object>();
        map.put("email", model.getEmail());
        map.put("name", model.getName());
        map.put("surname", model.getSurname());
        map.put("password", model.getPassword());
        map.put("organizzation", model.getOrganization());
        map.put("usercommunities", model.getUsercommunities());

        var entity = new HttpEntity<>(map, headers);

        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject(url, entity, String.class);
    }

    public String updatePassword(LocalUserDTO model,  String language, String token) {

        String url = String.format("%s%s",
                baseUrl,
                "user/updatepassword"
        );

        var headers = new org.springframework.http.HttpHeaders();
        headers.add(HttpHeaders.CONTENT_LANGUAGE, language);
        headers.add(HttpHeaders.AUTHORIZATION, token);

        var map = new HashMap<String, Object>();
        map.put("password", model.getPassword());
      
        var entity = new HttpEntity<>(map, headers);

        RestTemplate restTemplate = new RestTemplate();
        
        return restTemplate.postForObject(url, entity, String.class);
    }
    
    public String update(LocalUserDTO model,  String language) {
        String url = String.format("%s%s",
                baseUrl,
                "user/update"
        );

        var headers = new org.springframework.http.HttpHeaders();
        headers.add(HttpHeaders.CONTENT_LANGUAGE, language);

        var map = new HashMap<String, Object>();
        map.put("email", model.getEmail());
        map.put("name", model.getName());
        map.put("surname", model.getSurname());
        map.put("organizzation", model.getOrganization());
        map.put("usercommunities", model.getUsercommunities());

      
        var entity = new HttpEntity<>(map, headers);

        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject(url, entity, String.class);
    }

    
    @Override
    public String activate(ActivateRequest dto, String language) {
        String url = String.format("%s%s",
                baseUrl,
                "user/activate"
        );

        var headers = new org.springframework.http.HttpHeaders();
        headers.add(HttpHeaders.CONTENT_LANGUAGE, language);

        var map = new HashMap<String, Object>();
        map.put("subject", dto.getCode());

        var entity = new HttpEntity<>(map, headers);

        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject(url, entity, String.class);
    }
}
