package controllers;

import core.services.TopicService;

import java.util.HashMap;

import core.services.ApiService.AuthorizedHttpVerb;
import play.Logger;

public class UserNotLoggedController extends BaseController {

	public static void index() {
		renderText("BACK-END STARTED");
	}

	public static void login() {
		String jsonResponse = "{}";
		try {
			addOptions(AuthorizedHttpVerb.POST);
			if (isPost())
				jsonResponse = userService.login();
			else if (isOptions()) {
				System.out.println("options...");
				renderJSON(new HashMap<String, String>());
			}
			renderJSON(jsonResponse);
		} catch (Throwable thr) {
			Logger.error(thr, "Error...");
			jsonResponse = apiService.buildGenericErrorResponse();
		}
		renderJSON(jsonResponse);
	}

	public static void loginRecovery() {
		String jsonResponse = "{}";
		try {
			addOptions(AuthorizedHttpVerb.POST, AuthorizedHttpVerb.OPTIONS);
			if (isPost())
				jsonResponse = userService.loginRecovery();
			else if (isOptions()) {
				// continue
			}
			renderJSON(jsonResponse);
		} catch (Throwable thr) {
			Logger.error(thr, "Error...");
			jsonResponse = apiService.buildGenericErrorResponse();
		}
		renderJSON(jsonResponse);
	}

	public static void passwordReset() {
		String jsonResponse = "{}";
		try {
			addOptions(AuthorizedHttpVerb.POST, AuthorizedHttpVerb.OPTIONS);
			if (isPost())
				jsonResponse = userService.passwordReset();
			else if (isOptions()) {
				// continue
			}
			renderJSON(jsonResponse);
		} catch (Throwable thr) {
			Logger.error(thr, "Error...");
			jsonResponse = apiService.buildGenericErrorResponse();
		}
		renderJSON(jsonResponse);
	}
	
	public static void createTopics(String path, String filename){
		Logger.info("createTopics..." + path + " " + filename);
		TopicService.instance().createTopics(path, filename);
		
		Logger.info("Topics created");
	}

}
