/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.parallax.client.cloudsession;

import com.github.kevinsawicki.http.HttpRequest;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.parallax.client.cloudsession.exceptions.EmailAlreadyConfirmedException;
import com.parallax.client.cloudsession.exceptions.InsufficientBucketTokensException;
import com.parallax.client.cloudsession.exceptions.PasswordVerifyException;
import com.parallax.client.cloudsession.exceptions.UnknownUserException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Michel
 */
public class CloudSessionLocalUserService {

    private final String BASE_URL;
    private final String SERVER;

    public CloudSessionLocalUserService(String server, String baseUrl) {
        this.SERVER = server;
        this.BASE_URL = baseUrl;
    }

    public boolean doPasswordReset(String token, String email, String password, String passwordConfirm) throws UnknownUserException, PasswordVerifyException {
        Map<String, String> data = new HashMap<>();
        data.put("token", token);
        data.put("password", password);
        data.put("password-confirm", passwordConfirm);
        HttpRequest request = HttpRequest.post(getUrl("local/reset/" + email)).form(data);
//        int responseCode = request.code();
//        System.out.println("Response code: " + responseCode);
        String response = request.body();
//        System.out.println(response);
        JsonElement jelement = new JsonParser().parse(response);
        JsonObject responseObject = jelement.getAsJsonObject();
        if (responseObject.get("success").getAsBoolean()) {
            return true;
        } else {
            switch (responseObject.get("code").getAsInt()) {
                case 400:
                    throw new UnknownUserException(responseObject.get("data").getAsString());
                case 460:
                    throw new PasswordVerifyException();
            }
            return false;
        }
    }

    public boolean requestPasswordReset(String email) throws UnknownUserException, InsufficientBucketTokensException {
        HttpRequest request = HttpRequest.get(getUrl("local/reset/" + email)).header("server", SERVER);
//        int responseCode = request.code();
//        System.out.println("Response code: " + responseCode);
        String response = request.body();
//        System.out.println(response);
        JsonElement jelement = new JsonParser().parse(response);
        JsonObject responseObject = jelement.getAsJsonObject();
        if (responseObject.get("success").getAsBoolean()) {
            return true;
        } else {
            switch (responseObject.get("code").getAsInt()) {
                case 400:
                    throw new UnknownUserException(responseObject.get("data").getAsString());
                case 470:
                    throw new InsufficientBucketTokensException(responseObject.get("message").getAsString(), responseObject.get("data").getAsString());
            }
            return false;
        }
    }

    public boolean doConfirm(String email, String token) throws UnknownUserException {
        Map<String, String> data = new HashMap<>();
        data.put("email", email);
        data.put("token", token);
        HttpRequest request = HttpRequest.post(getUrl("local/confirm")).form(data);
//        int responseCode = request.code();
//        System.out.println("Response code: " + responseCode);
        String response = request.body();
//        System.out.println(response);
        JsonElement jelement = new JsonParser().parse(response);
        JsonObject responseObject = jelement.getAsJsonObject();
        if (responseObject.get("success").getAsBoolean()) {
            return true;
        } else {
            switch (responseObject.get("code").getAsInt()) {
                case 400:
                    throw new UnknownUserException(responseObject.get("data").getAsString());
                case 510:
                    return false;
            }
            return false;
        }
    }

    public boolean requestNewConfirmEmail(String email) throws UnknownUserException, InsufficientBucketTokensException, EmailAlreadyConfirmedException {
        HttpRequest request = HttpRequest.get(getUrl("local/confirm/" + email)).header("server", SERVER);
//        int responseCode = request.code();
//        System.out.println("Response code: " + responseCode);
        String response = request.body();
//        System.out.println(response);
        JsonElement jelement = new JsonParser().parse(response);
        JsonObject responseObject = jelement.getAsJsonObject();
        if (responseObject.get("success").getAsBoolean()) {
            return true;
        } else {
            switch (responseObject.get("code").getAsInt()) {
                case 400:
                    throw new UnknownUserException(responseObject.get("data").getAsString());
                case 470:
                    throw new InsufficientBucketTokensException(responseObject.get("message").getAsString(), responseObject.get("data").getAsString());
                case 520:
                    throw new EmailAlreadyConfirmedException(responseObject.get("message").getAsString());
            }
            return false;
        }
    }

    private String getUrl(String actionUrl) {
        return BASE_URL + actionUrl;
    }

}

@@ -690,24 +690,99 @@
 rException;%0A
+import com.parallax.client.cloudsession.exceptions.UnknownUserIdException;%0A
 import java.
@@ -5670,10 +5670,1269 @@
 %0A    %7D%0A%0A
+    public boolean changePassword(Long idUser, String oldPassword, String password, String confirmPassword) throws UnknownUserIdException, PasswordVerifyException %7B%0A        Map%3CString, String%3E data = new HashMap%3C%3E();%0A        data.put(%22old-password%22, oldPassword);%0A        data.put(%22password%22, password);%0A        data.put(%22password-confirm%22, confirmPassword);%0A        HttpRequest request = HttpRequest.post(getUrl(%22local/password/%22 + idUser)).form(data);%0A//        int responseCode = request.code();%0A//        System.out.println(%22Response code: %22 + responseCode);%0A        String response = request.body();%0A//        System.out.println(response);%0A        JsonElement jelement = new JsonParser().parse(response);%0A        JsonObject responseObject = jelement.getAsJsonObject();%0A        if (responseObject.get(%22success%22).getAsBoolean()) %7B%0A            return true;%0A        %7D else %7B%0A            switch (responseObject.get(%22code%22).getAsInt()) %7B%0A                case 400:%0A                    throw new UnknownUserIdException(responseObject.get(%22data%22).getAsString());%0A                case 460:%0A                    throw new PasswordVerifyException();%0A                case 510:%0A                    return false;%0A            %7D%0A            return false;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Change password service